how to resume an interrupted download

后端 未结 2 1078
日久生厌
日久生厌 2020-12-02 06:49

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 second

相关标签:
2条回答
  • 2020-12-02 07:31

    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();
               }
           }
        } 
    }    
    
    0 讨论(0)
  • 2020-12-02 07:41

    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

    0 讨论(0)
提交回复
热议问题