实现客户端将一张图片上传到服务器端,上传成功后,服务器通知客户端,上传成功。
(写的过程有点复杂,希望多多指点!)
解题思路:先建一个客服端和一个服务端,在客服端需要开一个线程(这个线程接收服务器的通知)
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class PictureTrans { //客服端
public static void main(String[] args) throws UnknownHostException, IOException {
File file1 = new File("E:\\360downloads\\294604.jpg");
System.out.println("我是客服端");
Socket socket = new Socket("10.48.0.6", 10002);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
String s = file1.getName() + "*" + file1.length() + "*";
os.write(s.g