ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
接着上一章节 我们要修改服务器端代码 ~~~ public class TCPThreadServer { public static void main(String[] args) throws IOException{ ServerSocket server = new ServerSocket(8000); while(true){ Socket socket = server.accept(); new Thread( new Upload(socket) ).start(); } } } ~~~ 就是把监听端口和客户端连接提取出来,其他放到线程的run方法里面 或者这种 ~~~ /* * 文件上传多线程版本, 服务器端 */ public class TCPServer { public static void main(String[] args) throws IOException { //1,创建服务器,等待客户端连接 ServerSocket serverSocket = new ServerSocket(6666); //实现多个客户端连接服务器的操作 while(true){ final Socket clientSocket = serverSocket.accept(); //启动线程,完成与当前客户端的数据交互过程 new Thread(){ public void run() { try{ //显示哪个客户端Socket连接上了服务器 InetAddress ipObject = clientSocket.getInetAddress();//得到IP地址对象 String ip = ipObject.getHostAddress(); //得到IP地址字符串 System.out.println("小样,抓到你了,连接我!!" + "IP:" + ip); //7,获取Socket的输入流 InputStream in = clientSocket.getInputStream(); //8,创建目的地的字节输出流 D:\\upload\\192.168.74.58(1).jpg BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream("D:\\upload\\"+ip+"("+System.currentTimeMillis()+").jpg")); //9,把Socket输入流中的数据,写入目的地的字节输出流中 byte[] buffer = new byte[1024]; int len = -1; while((len = in.read(buffer)) != -1){ //写入目的地的字节输出流中 fileOut.write(buffer, 0, len); } //-----------------反馈信息--------------------- //10,获取Socket的输出流, 作用:写反馈信息给客户端 OutputStream out = clientSocket.getOutputStream(); //11,写反馈信息给客户端 out.write("图片上传成功".getBytes()); out.close(); fileOut.close(); in.close(); clientSocket.close(); } catch(IOException e){ e.printStackTrace(); } }; }.start(); } //serverSocket.close(); } } ~~~