Java 断点续传代码
时间: 2023-08-31 14:29:19 浏览: 113
### 回答1:
Java 断点续传的代码实现可以分为以下几步:
1. 记录文件已经下载的长度
2. 设置 HTTP 请求头中 "Range" 字段,告诉服务器从文件的哪个位置开始下载
3. 使用 Java I/O 流下载文件
4. 保存文件已经下载的长度,以便下次继续下载
以下是一个简单的 Java 代码示例:
```
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class Downloader {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// 检查 HTTP 响应码
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// 检索文件名
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// 检索文件名
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// 打开本地文件输出流
OutputStream outputStream = new FileOutputStream(saveDir + File.separator + fileName);
// 打开网络输入流
InputStream inputStream = httpConn.getInputStream();
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0
### 回答2:
断点续传是指在文件传输过程中,当传输中断时,重新开始传输时能够从中断点继续传输,而不是重新开始。下面给出一个使用 Java 实现断点续传的简单示例代码。
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class ResumeDownload {
public static void main(String[] args) {
String fileUrl = "http://example.com/file.zip"; // 需要下载的文件地址
String savePath = "C:/Downloads/file.zip"; // 文件保存路径
try {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
File file = new File(savePath);
long downloaded = 0; // 已下载的文件大小
if (file.exists()) {
downloaded = file.length();
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.connect();
FileOutputStream outputStream = new FileOutputStream(file, true); // 追加写入模式
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[4096];
int bytesRead;
long totalDownloaded = downloaded;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalDownloaded += bytesRead;
}
outputStream.close();
inputStream.close();
System.out.println("文件下载完成!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
上述代码中,通过使用 HttpURLConnection 进行文件下载。首先检查文件是否存在,若存在则设置请求头 Range 属性为已下载文件的大小,从断点处开始下载。然后将文件输入流写入到文件输出流中,实现断点续传。
### 回答3:
Java的断点续传代码主要通过处理文件的读写来实现。下面是一个简单的实现示例:
```java
import java.io.*;
public class ResumableFileDownloader {
private static final int BUFFER_SIZE = 1024;
public static void resumeDownload(String url, String filePath, long resumePosition) throws IOException {
BufferedInputStream in = null;
RandomAccessFile file = null;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes=" + resumePosition + "-");
in = new BufferedInputStream(conn.getInputStream());
file = new RandomAccessFile(filePath, "rw");
file.seek(resumePosition);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
file.write(buffer, 0, bytesRead);
}
} finally {
if (in != null) {
in.close();
}
if (file != null) {
file.close();
}
}
}
public static void main(String[] args) {
try {
long resumePosition = 0; // 设置续传的起始位置
resumeDownload("http://example.com/file.pdf", "downloaded_file.pdf", resumePosition);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在该代码中,`resumeDownload`方法接收一个URL、文件保存路径和续传的起始位置作为参数。它首先从指定位置发起HTTP请求,设置`Range`头部以指定从该位置开始下载。然后通过`BufferedInputStream`读取数据,并通过`RandomAccessFile`写入文件,同时更新写入的位置。通过设置`RandomAccessFile`的`seek`方法,我们可以指定写入文件的位置。
在`main`方法中,我们可以设置续传的起始位置并调用`resumeDownload`方法来执行断点续传。例如,我们可以将文件下载到`downloaded_file.pdf`的位置,并从上一次未下载完成的位置继续下载。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.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)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)