Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause/Resume http connection download

I want to be able to open a http connection to a given file in Android and start downloading it. I also have to be able to pause the download at some point and resume it later. How is this achieved in Android? I don't want to start the download all over again.

like image 675
gop Avatar asked Jan 20 '13 09:01

gop


People also ask

Can you resume paused downloads?

3. Click on the "Resume" button next to the stopped download to start downloading it again. The download resumes at the point where it left off, so you don't need to download the file from the beginning again.

How do you resume a download after a lost connection?

Alternatively, you can press Ctrl+J on Windows or Command+J on macOS. In the list of downloads, find the failed item and click “Resume”. If everything goes right, your download will resume from where it left off before you were disconnected.

Can I pause Windows 10 download?

This is a built-in option available in all Windows 10 editions, as long as you're on a modern version. To pause Windows Update, go to Settings > Update & Security > Windows Update. You can click Pause updates for 7 days to block updates for a week; it's also possible to extend this time by clicking again later.


1 Answers

Such a downloader has been posted here:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
        File file=new File(DESTINATION_PATH);
        if(file.exists()){
             downloaded = (int) file.length();
             connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
        }
    }else{
        connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    progressBar.setMax(connection.getContentLength());
     in = new BufferedInputStream(connection.getInputStream());
     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);
         downloaded += x;
         progressBar.setProgress(downloaded);
    }
like image 90
laxonline Avatar answered Oct 05 '22 20:10

laxonline