gpt4 book ai didi

java - 在 Java 中暂停下载?

转载 作者:行者123 更新时间:2023-12-01 19:13:36 26 4
gpt4 key购买 nike

我正在使用此代码下载文件,我想知道是否可以暂停下载并稍后恢复,如果可以的话如何?:

    URL url = new URL(URL);
URLConnection conexion = url.openConnection();

conexion.connect();
int lenghtOfFile = conexion.getContentLength();
lenghtOfFile /= 100;

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Path + FileName);

byte data[] = new byte[1024];
long total = 0;

int count = 0;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
total += count;
}

output.flush();
output.close();
input.close();

最佳答案

只有当服务器支持 HTTP 范围 header (在 HTTP 1.1 中引入)时,这才有可能

请参阅此问题以获取 Java 中的示例:how to use the HTTP range header in J2ME? .

“暂停”可能只是意味着读取一些流并将其写入磁盘。恢复时,您必须使用 header 来指定还需要下载的内容。

关于java - 在 Java 中暂停下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7619717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com