java基于Socket实现TCP协议的文件上传和下载,上传客户端从本地路径上传文件,下载文件要下载上传客户端上传的文件内容并显示到另一个路径当中把三个端口代码写出来,需要上传下载共用一个服务器端,上传一个客户端,下载一个客户端
时间: 2024-10-19 13:14:26 浏览: 14
在Java中,我们可以利用Socket编程来实现TCP协议的文件上传和下载功能。以下是简化的客户端和服务端代码示例,这两个端点共享一个服务器:
**服务端(Server.java):**
```java
import java.io.*;
import java.net.*;
public class Server {
private static final int PORT = 9000; // 设置服务器端口
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started on port " + PORT);
while (true) { // 循环等待连接
Socket clientSocket = serverSocket.accept();
String request = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())).readLine();
if ("upload".equals(request)) { // 接收上传请求
File fileToUpload = new File("/path/to/upload/file"); // 替换为你实际的文件路径
uploadFile(clientSocket, fileToUpload);
} else if ("download".equals(request)) { // 接收下载请求
downloadFile(clientSocket); // 下载文件并发送到指定路径
}
clientSocket.close(); // 关闭连接
}
}
private static void uploadFile(Socket socket, File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
OutputStream out = socket.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
fis.close();
out.flush();
out.close();
}
private static void downloadFile(Socket socket) throws IOException {
InputStream in = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/path/to/download/path"); // 替换为你实际的下载文件路径
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
}
}
```
**上传客户端(UploaderClient.java):**
```java
import java.io.*;
import java.net.*;
public class UploaderClient {
private static final String SERVER_IP = "localhost"; // 服务器IP地址
private static final int UPLOAD_PORT = 9000;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_IP, UPLOAD_PORT);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String filePath = "/path/to/local/file"; // 本地文件路径
writer.println("upload");
Files.copy(Paths.get(filePath), socket.getOutputStream()); // 发送文件内容
String response = reader.readLine();
System.out.println("Upload response: " + response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
**下载客户端(DownloaderClient.java):**
```java
import java.io.*;
import java.net.*;
public class DownloaderClient {
private static final String SERVER_IP = "localhost";
private static final int DOWNLOAD_PORT = 9000;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_IP, DOWNLOAD_PORT);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {
writer.println("download");
String remoteFilePath = "/path/to/received/file"; // 服务器上接收的文件路径
String response = reader.readLine();
System.out.println("Download response: " + response);
FileOutputStream fos = new FileOutputStream(remoteFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = socket.getInputStream().read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文