1、使用TCP网络编程,实现图片的下载。
客户端:
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost",8081);
InputStream inputStream = socket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("src\\ParcticeDiwuzhou\\lx0811\\lx01\\fileload\\two.jpg");
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
while (read!=-1){
fileOutputStream .write(bytes,0,read);
read = inputStream.read(bytes);
}
fileOutputStream.close();
inputStream.close();
socket.close();
}
}
服务端
package ParcticeDiwuzhou.lx0811.lx01.fileload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8081);
Socket accept = serverSocket.accept();
FileInputStream fileInputStream = new FileInputStream("src\\ParcticeDiwuzhou\\lx0811\\lx01\\fileload\\one.jpg");
OutputStream outputStream = accept.getOutputStream();
byte[] bytes = new byte[1024];
int i = fileInputStream.read(bytes);
while (i!=-1){
outputStream.write(bytes,0,i);
i=fileInputStream.read(bytes);
}
outputStream.close();
fileInputStream.close();
accept.close();
serverSocket.close();
}
}
2、使用URL编程,下载一个网络图片
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Urlxl {
public static void main(String[] args) throws IOException {
URL url = new URL("http://localhost:8080/mypicture/a.jpg");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
FileOutputStream fileOutputStream =
new FileOutputStream("src\\ParcticeDiwuzhou\\lx0811\\lx02\\d.jpg");
InputStream inputStream = httpURLConnection.getInputStream();
byte[] bytes = new byte[1024];
int read = inputStream.read(bytes);
while (read != -1) {
fileOutputStream.write(bytes, 0, read);
read = inputStream.read(bytes);
}
fileOutputStream.close();
inputStream.close();
httpURLConnection.disconnect();
}
}
3、使用TCP网络编程,完成注册功能
客户端: 填写用户信息 发送信息给服务器
服务器: 接收信息 保存新用户到user.txt中
用户信息类
import com.sun.org.apache.xml.internal.serialize.Serializer;
import java.io.Serializable;
public class User implements Serializable {
public User() {
}
public User(String name, int id) {
this.name = name;
this.id = id;
}
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
客户端
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException, ClassNotFoundException {
while (true) {
System.out.println("添加");
tcpIo();
}
}
public static User addUser() {
System.out.println("请输入用户名");
String next = scanner.next();
System.out.println("请输入ID");
int id = scanner.nextInt();
User user = new User(next, id);
return user;
}
public static void tcpIo() throws IOException, ClassNotFoundException {
Socket socket = new Socket("localhost", 8820);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(addUser());
objectOutputStream.close();
outputStream.close();
socket.close();
}
}
服务端
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Service {
public static void main(String[] args) throws IOException, ClassNotFoundException {
while (true) {
ServerSocket serverSocket = new ServerSocket(8820);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
FileOutputStream fileOutputStream =
new FileOutputStream("src\\ParcticeDiwuzhou\\lx0811\\lx03\\User.txt", true);
User user=(User) objectInputStream.readObject();
while (user !=null){
fileOutputStream.write(user.toString().getBytes());
user=(User) objectInputStream.readObject();
}
fileOutputStream.close();
objectInputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
}
}
}
|