# 2 使用TCP协议完成上传文件操作(要求同桌相互测试):实现文件上传和文件夹的上传 从本地读文件然后将文件内容发送到接收方,同时接收方收到数据后将内容写入服务器端的新文件 [封装NIO的读写文件方法]
时间: 2024-05-01 07:19:09 浏览: 52
以下是使用TCP协议完成上传文件操作的代码实现,包括文件上传和文件夹的上传:
1. 文件上传
服务端代码:
```java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
Socket socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("D:\\temp\\test.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
System.out.println("文件上传成功!");
}
}
```
客户端代码:
```java
import java.io.*;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream("D:\\temp\\test.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
fis.close();
os.close();
socket.close();
System.out.println("文件上传成功!");
}
}
```
2. 文件夹上传
服务端代码:
```java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
Socket socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());
InputStream is = socket.getInputStream();
File file = new File("D:\\temp\\" + UUID.randomUUID().toString().replace("-", ""));
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
System.out.println("文件夹上传成功,保存路径为:" + file.getAbsolutePath());
}
}
```
客户端代码:
```java
import java.io.*;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
String folderPath = "D:\\temp\\test";
zipFolder(folderPath, os);
os.close();
socket.close();
System.out.println("文件夹上传成功!");
}
private static void zipFolder(String folderPath, OutputStream os) throws IOException {
File folder = new File(folderPath);
File[] files = folder.listFiles();
byte[] buffer = new byte[1024];
for (File file : files) {
if (file.isDirectory()) {
zipFolder(file.getAbsolutePath(), os);
} else {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int len;
while ((len = bis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
bis.close();
fis.close();
}
}
}
}
```
其中,zipFolder()方法用于将文件夹压缩为一个文件,然后将该文件上传到服务器。在服务端接收到文件后,可以将其解压缩为原来的文件夹。
阅读全文