如何恢复中断的下载 [英] how to resume an interrupted download

查看:140
本文介绍了如何恢复中断的下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个大文件从我的雅虎的网站服务器,这显然是设置(不是我)断开下载,如果他们不是100秒内完成。该文件是小到足以通常成功传输。在场合,当数据速率很慢,下载断开连接,有没有办法恢复发生断线所在的URLConnection的文件偏移?这里的code:

  //设置连接。
网址URL =新的URL(strUrl [0]);
URLConnection的CX = url.openConnection();
cx.connect();

//设置流和缓冲器。
INT lengthFile = cx.getContentLength();
的InputStream输入=新的BufferedInputStream(url.openStream());
的OutputStream输出=新的FileOutputStream(strUrl [1]);
字节的数据[] =新的字节[1024];

// 下载文件。
为(总= 0;(计数= input.read(数据,0,1024))= -1;!总+ =计数){
    publishProgress((int)的(总* 100 / lengthFile));
    output.write(数据,0,计数);
    Log.d(AsyncDownloadFile,字节:+总);
}

//关闭流。
output.flush();
output.close();
input.close();
 

解决方案

尝试使用范围的请求头:

  //打开到URL连接。
    HttpURLConnection的连接=
            (HttpURLConnection类)url.openConnection();

    //指定哪些文件的一部分下载。
    connection.setRequestProperty(范围,字节=+下载+ - );
    //这里的下载的数据长度已经previously下载。

    //连接到服务器。
    connection.connect();
 

做完,你可以征求在给定的点(只是你的下载数据的长度面前,说 X ),并开始编写新下载的数据存在。一定要使用相同的值 X 的范围头。

了解 14.35.2范围检索请求

详细

更多细节及来源$ C ​​$ C可以发现<一href="http://www.java-tips.org/java-se-tips/javax.swing/how-to-create-a-download-manager-in-java.html">here

I'm trying to download a large file from my Yahoo! web site server which apparently is setup (not by me) to disconnect downloads if they are not completed within 100 seconds. The file is small enough to usually successfully transfer. On the occasions when the data rate is slow and the download gets disconnected, is there a way to resume the URLConnection at the file offset where the disconnection occurred? Here's the code:

// Setup connection.
URL url = new URL(strUrl[0]);
URLConnection cx = url.openConnection();
cx.connect();

// Setup streams and buffers.
int lengthFile = cx.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(strUrl[1]);
byte data[] = new byte[1024];

// Download file.
for (total=0; (count=input.read(data, 0, 1024)) != -1; total+=count) {
    publishProgress((int)(total*100/lengthFile));
    output.write(data, 0, count);
    Log.d("AsyncDownloadFile", "bytes: " + total);
}

// Close streams.
output.flush();
output.close();
input.close();

解决方案

Try using a "Range" request header:

    // Open connection to URL.
    HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();

    // Specify what portion of file to download.
    connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
    // here "downloaded" is the data length already previously downloaded.

    // Connect to server.
    connection.connect();

Having done that, you can seek at a given point (just before the length of your download data, say X) and start writing the newly downloaded data there. Be sure to use the same value X for the range header.

Details about 14.35.2 Range Retrieval Requests

More details and source code can be found here

这篇关于如何恢复中断的下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆