如何在java中实现TCP服务器和TCP客户端来传输文件

我已经实现了简单的TCP服务器和TCP客户端类,可以将消息从客户端发送到服务器,消息将在服务器端转换为大写,但是如何实现从服务器到客户端的传输文件以及从客户端上传文件到服务器。 以下代码是我所拥有的。

TCPClient.java

 import java.io.*; import java.net.*; class TCPClient { public static void main(String args[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("127.0.0.1", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + "\n"); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } 

TCPServer.java

 import java.io.*; import java.net.*; class TCPServer { public static void main(String args[]) throws Exception { int firsttime = 1; while (true) { String clientSentence; String capitalizedSentence=""; ServerSocket welcomeSocket = new ServerSocket(3248); Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); //System.out.println(clientSentence); if (clientSentence.equals("set")) { outToClient.writeBytes("connection is "); System.out.println("running here"); //welcomeSocket.close(); //outToClient.writeBytes(capitalizedSentence); } capitalizedSentence = clientSentence.toUpperCase() + "\n"; //if(!clientSentence.equals("quit")) outToClient.writeBytes(capitalizedSentence+"enter the message or command: "); System.out.println("passed"); //outToClient.writeBytes("enter the message or command: "); welcomeSocket.close(); System.out.println("connection terminated"); } } } 

因此, TCPServer.java将首先执行,然后执行TCPClient.java ,我尝试使用TCPServer.java的if子句来测试用户的输入,现在我真的想实现如何从中传输文件双方(下载和上传)。谢谢。

因此,假设在服务器端您已收到文件名和文件路径。 这段代码应该给你一些想法。

服务器

 PrintStream out = new PrintStream(socket.getOutputStream(), true); FileInputStream requestedfile = new FileInputStream(completeFilePath); byte[] buffer = new byte[1]; out.println("Content-Length: "+new File(completeFilePath).length()); // for the client to receive file while((requestedfile.read(buffer)!=-1)){ out.write(buffer); out.flush(); out.close(); } requestedfile.close(); 

客户

 DataInputStream in = new DataInputStream(socket.getInputStream()); int size = Integer.parseInt(in.readLine().split(": ")[1]); byte[] item = new byte[size]; for(int i = 0; i < size; i++) item[i] = in.readByte(); FileOutputStream requestedfile = new FileOutputStream(new File(fileName)); BufferedOutputStream bos = new BufferedOutputStream(requestedfile); bos.write(item); bos.close(); fos.close(); 

假设您想继续支持发送消息以及来回发送文件……

就像现在一样,您正在使用writeBytes将数据从客户端发送到服务器。

您可以使用它来发送任何内容,例如文件的内容……

但是您需要在客户端和服务器之间定义协议,以便他们知道何时传输文件而不是聊天消息。

例如,您可以在将文件发送到服务器之前发送消息/字符串“ FILECOMING ”,然后它会知道期望文件的字节。 同样,你也需要一种方法来标记文件的结尾……

或者,您可以在每条消息之前发送消息类型。

更高效/响应的解决方案是在单独的线程/套接字上进行文件传输 – 这意味着传输不会阻止聊天消息。 每当需要文件传输时,就会为此创建一个新的线程/套接字连接。

〜克里斯

 import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws IOException { String sentence; String modifiedSentence; Socket clientSocket = new Socket("*localhost*", *portnum*); // new Socket("192.168.1.100", 80); System.out.println("Enter your ASCII code here"); BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); sentence = inFromUser.readLine(); // System.out.println(sentence); while(!(sentence.isEmpty())) { DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); outToServer.writeBytes(sentence); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); modifiedSentence = inFromServer.readLine(); while(!(modifiedSentence.isEmpty())) { System.out.println("FROM SERVER: " + modifiedSentence); break; } System.out.println("Enter your ASCII code here"); inFromUser = new BufferedReader( new InputStreamReader(System.in)); sentence = inFromUser.readLine(); } System.out.println("socket connection going to be close"); clientSocket.close(); } }