简体   繁体   English

如何恢复中断的下载

[英]how to resume an interrupted download

I'm trying to download a large file from my Yahoo! 我正试图从我的Yahoo!下载一个大文件 web site server which apparently is setup (not by me) to disconnect downloads if they are not completed within 100 seconds. 网站服务器显然是设置(不是由我)断开下载,如果他们没有在100秒内完成。 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? 在数据速率较慢且下载断开的情况下,有没有办法在发生断开连接的文件偏移处恢复URLConnection? 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. 完成后,您可以在给定点(在下载数据的长度之前,例如Xseek并开始在那里写入新下载的数据。 Be sure to use the same value X for the range header. 请务必使用相同的值X作为范围标题。

Details about 14.35.2 Range Retrieval Requests 有关14.35.2范围检索请求的详细信息

More details and source code can be found here 更多细节和源代码可以在这里找到

Here's an example code that you can use: 这是您可以使用的示例代码:

import java.io.*;
import java.net.*;


public class HttpUrlDownload {

    public static void main(String[] args) {
       String strUrl = "http://VRSDLSCEN001:80//DLS//lib//clics.jar";
       String DESTINATION_PATH = "clics.jar";

       int count = 0;
       while (true) {
          count++;
          if (download(strUrl, DESTINATION_PATH) == true || count > 20) {
             break;
          }         
       }
    }

    public static boolean download(String strUrl, String DESTINATION_PATH) {
        BufferedInputStream in = null;
        FileOutputStream fos = null;
        BufferedOutputStream bout = null;
        URLConnection connection = null;

        int downloaded = 0;

        try {
           System.out.println("mark ... download start");
           URL url = new URL(strUrl);

           connection = url.openConnection();

           File file=new File(DESTINATION_PATH);
           if(file.exists()){
               downloaded = (int) file.length();
           }
           if (downloaded == 0) {
               connection.connect();
           }
           else {
               connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
               connection.connect();
           }

           try {
               in = new BufferedInputStream(connection.getInputStream());
           } catch (IOException e) {
               int responseCode = 0;
               try {
                   responseCode = ((HttpURLConnection)connection).getResponseCode();
               } catch (IOException e1) {
                 e1.printStackTrace();
               }

               if (responseCode == 416) {         
                   return true;
               } else {
                   e.printStackTrace();
                   return false;
               }
           }

           fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
           bout = new BufferedOutputStream(fos, 1024);

           byte[] data = new byte[1024];
           int x = 0;
           while ((x = in.read(data, 0, 1024)) >= 0) {
              bout.write(data, 0, x);
           }

           in.close();
           bout.flush();
           bout.close();
           return false;

       } catch (IOException e) {
           e.printStackTrace();
           return false;
       } finally {
           if (in != null) {
               try {
                   in.close();
               } catch (IOException e) {
               }
           }
           if (fos != null) {
               try {
                   fos.close();
               } catch (IOException e) {
               }
           }
           if (bout != null) {
               try {
                   bout.close();
               } catch (IOException e) {
               }
           }

           if (connection != null) {
              ((HttpURLConnection)connection).disconnect();
           }
       }
    } 
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM