首页下载资源前端编写程序,客户端发送一个文件名给服务器,服务器查找本地磁盘,如果文件存在,则把文件内容发送个客户端,否则回答文件不存在

ZIP编写程序,客户端发送一个文件名给服务器,服务器查找本地磁盘,如果文件存在,则把文件内容发送个客户端,否则回答文件不存在

ikun_23339.16KB需要积分:1

资源文件列表:

1.zip 大约有18个文件
  1. 1/
  2. 1/.gitignore 344B
  3. 1/.idea/
  4. 1/.idea/.gitignore 50B
  5. 1/.idea/inspectionProfiles/
  6. 1/.idea/inspectionProfiles/Project_Default.xml 384B
  7. 1/.idea/misc.xml 278B
  8. 1/.idea/modules.xml 263B
  9. 1/.idea/workspace.xml 3.01KB
  10. 1/out/
  11. 1/out/production/
  12. 1/out/production/untitled/
  13. 1/out/production/untitled/SimplifiedClient.class 2.37KB
  14. 1/out/production/untitled/SimplifiedServer.class 3.67KB
  15. 1/src/
  16. 1/src/SimplifiedClient.java 1.03KB
  17. 1/src/SimplifiedServer.java 1.61KB
  18. 1/untitled.iml 433B

资源介绍:

编写程序,客户端发送一个文件名给服务器,服务器查找本地磁盘,如果文件存在,则把文件内容发送个客户端,否则回答文件不存在。
import java.io.*; import java.net.*; public class SimplifiedServer { private static final int PORT = 12345; public static void main(String[] args) { try (ServerSocket serverSocket = new ServerSocket(PORT)) { System.out.println("服务器在在接收指令" + PORT); while (true) { Socket socket = serverSocket.accept(); handleClient(socket); } } catch (IOException e) { e.printStackTrace(); } } private static void handleClient(Socket socket) { new Thread(() -> { try ( BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); ) { String fileName = in.readLine(); File file = new File(fileName); if (file.exists()) { try (BufferedReader fileReader = new BufferedReader(new FileReader(file))) { String line; while ((line = fileReader.readLine()) != null) { out.println(line); } } } else { out.println("文件未找到"); } } catch (IOException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { // Ignore } } }).start(); } }
100+评论
captcha