java基于TCP/IP的用于传输任意大小的文件
时间: 2024-02-05 14:14:23 浏览: 34
Java基于TCP/IP的传输可以使用Socket和ServerSocket类来实现。下面是一个简单的示例代码,可以用于传输任意大小的文件:
```
import java.io.*;
import java.net.*;
public class FileTransfer {
public static void main(String[] args) {
try {
int fileSize = 1024; // 每次传输的文件大小
byte[] buffer = new byte[fileSize];
int bytesRead = 0;
// 创建Socket连接
Socket socket = new Socket("localhost", 8888);
// 读取本地文件
File file = new File("example.txt");
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
// 发送文件大小给服务器
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeInt((int) file.length());
dataOutputStream.flush();
// 发送文件内容
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
outputStream.flush();
}
// 关闭连接
socket.close();
fileInputStream.close();
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
服务器端代码如下:
```
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
int fileSize = 1024; // 每次传输的文件大小
byte[] buffer = new byte[fileSize];
int bytesRead = 0;
// 创建ServerSocket
ServerSocket serverSocket = new ServerSocket(8888);
// 等待客户端连接
System.out.println("等待客户端连接...");
Socket socket = serverSocket.accept();
System.out.println("客户端已连接!");
// 接收文件大小
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
int fileSizeToReceive = dataInputStream.readInt();
System.out.println("待接收文件大小为:" + fileSizeToReceive + "字节");
// 接收文件内容
FileOutputStream fileOutputStream = new FileOutputStream("example-received.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int totalBytesReceived = 0;
while (totalBytesReceived < fileSizeToReceive) {
bytesRead = inputStream.read(buffer);
bufferedOutputStream.write(buffer, 0, bytesRead);
bufferedOutputStream.flush();
totalBytesReceived += bytesRead;
}
// 关闭连接
socket.close();
serverSocket.close();
fileOutputStream.close();
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上代码仅供参考,实际使用时需要根据具体需求进行修改。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)