如何恢复中断的下载

2022-09-01 23:13:17

我正在尝试从我的Yahoo!网站服务器下载一个大文件,该文件显然(不是由我设置的)如果下载未在100秒内完成,则断开连接。文件足够小,通常可以成功传输。在数据速率较慢且下载断开连接的情况下,有没有办法在发生断开连接的文件偏移处恢复 URL 连接?代码如下:

// 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();

答案 1

尝试使用“范围”请求标头:

// 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();

完成此操作后,您可以在给定的点(例如,在下载数据的长度之前)开始在那里写入新下载的数据。请确保对范围标头使用相同的值。seekXX

有关 14.35.2 范围检索请求的详细信息

更多细节和源代码可以在这里找到


答案 2

下面是一个可以使用的示例代码:

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();
           }
       }
    } 
}    

推荐