TCP传输文件
1.服务端
package shapes; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class ServerSocketDemo { public static void main(String[] args) { // TODO Auto-generated method stub try { // 创建一个ServerSocket ServerSocket ss = new ServerSocket(2000); while(true) { // 侦听客户端Socket Socket s = ss.accept(); // 为每一个客户端开启一个线程 new Thread(new ServerThread(s)).start(); } // // 获取输入流 // BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); // // 把数据写入文本文件 // BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\Java\\Shapes\\s.txt")); // String line; // while((line = br.readLine()) != null) { //等待读取数据 // System.out.println("服务端接收:" + line); // //获取输出流,写数据 // bw.write("服务端接收:" + line); // bw.newLine(); // bw.flush(); // // //反馈(消息) //// OutputStream os = s.getOutputStream(); //// os.write("数据已经收到".getBytes()); // } // // // 上传成功反馈 // BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); // bwServer.write("文件上传成功"); // bwServer.newLine(); // bwServer.flush(); // // //释放资源 // bw.close(); // s.close(); // ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.多线程帮助类
package shapes; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class ServerThread implements Runnable { private Socket s; public ServerThread(Socket s) { this.s = s; } @Override public void run() { // TODO Auto-generated method stub // 接收数据写到文本文件 try { // 获取输入流 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); // 处理重名问题 int count = 0; File file = new File("E:\\Java\\Shapes\\s[" + count + "].txt"); while(file.exists()) { count++; file = new File("E:\\Java\\Shapes\\s[" + count + "].txt"); } // 把数据写入文本文件 BufferedWriter bw = new BufferedWriter(new FileWriter(file)); String line; while ((line = br.readLine()) != null) { // 等待读取数据 System.out.println("服务端接收:" + line); // 获取输出流,写数据 bw.write("服务端接收:" + line); bw.newLine(); bw.flush(); // 反馈(消息) // OutputStream os = s.getOutputStream(); // os.write("数据已经收到".getBytes()); } // 上传成功反馈 BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); bwServer.write("文件上传成功"); bwServer.newLine(); bwServer.flush(); // 释放资源 bw.close(); s.close(); } catch (IOException e) { } } }
3.客户端
package shapes; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub try { // 创建Socket Socket s = new Socket("192.168.90.114", 2000); // 输入数据 //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 输出流对象封装 //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); // 获取文件 BufferedReader br = new BufferedReader(new FileReader("E:\\Java\\Shapes\\a.txt")); // 上传文件 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line; while ((line = br.readLine()) != null) { if(line.equals("886")) { break; } //获取输出流,写数据 bw.write(line); bw.newLine(); bw.flush(); // 接受服务器反馈(消息) // InputStream is = s.getInputStream(); // byte[] bys = new byte[1024]; // int len = is.read(bys); // String data = new String(bys,0,len); // System.out.print("客户端:" + data); } // 自定义结束标志 // bw.write("end"); // bw.newLine(); // bw.flush(); s.shutdownOutput(); // 接收上传文件反馈 BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream())); String data = brClient.readLine(); //等待读取数据 System.out.println("服务器的反馈:" + data); //释放资源 br.close(); bw.close(); s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
4.结果:
注:这里如果部署到服务器的话,可能需要注意一下文件夹权限什么的
参阅:https://www.bilibili.com/video/BV1vk4y117fU?p=355