写一个大文件分片上传的方法
时间: 2024-01-01 15:05:33 浏览: 27
好的,以下是一个用 Java 实现的大文件分片上传的示例代码:
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
public class BigFileUploader {
private static final int BUFFER_SIZE = 4096;
private static final int CHUNK_SIZE = 1024 * 1024; // 分片大小,这里设置为 1MB
private static final String BOUNDARY = UUID.randomUUID().toString(); // 分隔符
public static void upload(String targetURL, File file) throws IOException {
HttpURLConnection connection = null;
BufferedInputStream inputStream = null;
OutputStream outputStream = null;
try {
// 创建连接
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(BUFFER_SIZE);
// 设置请求头
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
connection.setRequestProperty("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
// 上传文件
outputStream = connection.getOutputStream();
int bytesRead;
byte[] buffer = new byte[CHUNK_SIZE];
inputStream = new BufferedInputStream(new FileInputStream(file));
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write("--".getBytes());
outputStream.write(BOUNDARY.getBytes());
outputStream.write("\r\n".getBytes());
outputStream.write("Content-Type: application/octet-stream\r\n".getBytes());
outputStream.write(("Content-Range: bytes " + inputStream.available() + "-" + (inputStream.available() + bytesRead - 1) + "/" + file.length() + "\r\n").getBytes());
outputStream.write("\r\n".getBytes());
outputStream.write(buffer, 0, bytesRead);
outputStream.write("\r\n".getBytes());
}
// 结束上传
outputStream.write("--".getBytes());
outputStream.write(BOUNDARY.getBytes());
outputStream.write("--\r\n".getBytes());
// 检查响应码
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("File uploaded successfully.");
} else {
System.out.println("File upload failed. Response code: " + responseCode);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (connection != null) {
connection.disconnect();
}
}
}
}
```
使用方式:
```java
File file = new File("path/to/big/file");
String targetURL = "http://example.com/upload";
BigFileUploader.upload(targetURL, file);
```
在实际应用中,你需要把 `targetURL` 替换成你自己的上传接口地址。同时,你也可以根据具体需求修改代码中的一些参数,比如 `CHUNK_SIZE`、`BOUNDARY`、`Content-Type` 等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)