Implementing resume for download files via internet

前端 未结 2 1144
温柔的废话
温柔的废话 2021-02-05 17:22

my below code for downloading file work fine when don\'t implementing resume for that, after read more solution to implementing that and resolve problem i know i mu

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 17:55

    1- You are getting null for httpURLConnection because you try to invoke it before being initialized,

    i.e, this line

    httpURLConnection = (HttpURLConnection) url.openConnection();
    

    should come before this line:

    String lastModified = httpURLConnection.getHeaderField("Last-Modified");
    

    2- you can set the header before calling connect() on httpURLConnection so you need to set whatever you want, then connect(). this way you should not get the error (android Cannot set request property after connection is made)

    3- The 206 is perfectly right, it's what you should expect when using Range and it means Partial Content Success and that's what you are doing, you are getting part of the content, if you are getting full content you would get 200.

    so to sum this up, your code can look like this: Note: follow the //*** to see changes required.

    EDIT: it all came to this line

    httpURLConnection.setRequestProperty("If-Range", lastModified);
    

    the error get thrown when you set that Property,

    Anyways, when you look at this, it's meaningless, you are asking if last-modified is equal to the value that you just got from the connection!, if you want to do this you need to store lastModified in your system, then compare it with the one you got from the URLConn, and compare it to your file length (already downloaded) then proceed with full download or resume download.

    find new code below:

    public void run() {
        myLastModified = getLastModified(mFile.getName()); // get last stored value for this file (use file name or other key)
        int total =0;
    
        final URL         url;
        HttpURLConnection httpURLConnection = null;
        try {
            try {
                url = new URL(mUrl);
    
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoInput(true);
    
                httpURLConnection.setConnectTimeout(30000);
                httpURLConnection.setReadTimeout(30000);
                httpURLConnection.setRequestMethod("GET");
    
                //*** new way to handle download process
                total = httpURLConnection.getContentLength();
                if(mFile.exists()){
                    if(mFile.length() == total){
                        //we are done, return.
                        return;
                    }else{
                        //file was not completly donwloaded, now check lastModified:
                        long lastModified = httpURLConnection.getLastModified();//this gets the header "Last-Modified" and convert to long
                        if (lastModified == myLastModified) { //myLastModified should be retrived on each download and stored locally on ur system
                            downloadedLength = mFile.length();
                            Log.e("downloadedLength ", downloadedLength + "");
                            httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setDoInput(true);
    
                            httpURLConnection.setConnectTimeout(30000);
                            httpURLConnection.setReadTimeout(30000);
                            httpURLConnection.setRequestMethod("GET");
    
                            httpURLConnection.setRequestProperty("Range", "bytes=" + downloadedLength + "-"+ total); //add + total (TO)
    
                            //append mode
                            fileOutputStream = new FileOutputStream(mFile, true);
                        }else{
                            //file was modified after 1st uncompleted-download:
                            storeLastModified(lastModified, mFile.getName()); // use file name as key. can store in db or file ...
    
                            //don't set ant Range ... we want full download, with a fresh file
                            fileOutputStream = new FileOutputStream(mFile);
                        }//last mod IF
    
                    }//Length check
                }else{
                    //file not exist at all, create new file, set no Range we want full download...
                    mFile.createNewFile();
                    fileOutputStream = new FileOutputStream(mFile);
                }//file exists.
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            final int responseCode;
    
            try {
                responseCode = httpURLConnection.getResponseCode();
    
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("ER UPDATE ", e.getMessage());
            }
    
            //*****
            if (responseCode == 200 || responseCode == 206) {
                try {
                    inputStream = httpURLConnection.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("IOException ", e.getMessage());
                }
                final byte[] buffer   = new byte[4 * 1024];
                int          length   = -1;
                int          finished = 0;
                long         start    = System.currentTimeMillis();
                try {
                    while ((length = inputStream.read(buffer)) != -1) {
                        if (!isDownloading()) {
                            throw new CanceledException("canceled");
                        }
                        fileOutputStream.write(buffer, 0, length);
                        finished += length;
                        if (System.currentTimeMillis() - start > 1000) {
                            onDownloadProgressing(finished, total);
                            start = System.currentTimeMillis();
                        }
                    }
                    onDownloadCompleted();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("ER UPDATE ", e.getMessage());
                }
            } else {
                Log.e("responseCode ", responseCode + "");
            }
        } catch (DownloadException e) {
            e.printStackTrace();
            Log.e("ER UPDATE ", e.getMessage());
        } catch (CanceledException e) {
            e.printStackTrace();
            Log.e("ER UPDATE ", e.getMessage());
        }
    }
    

提交回复
热议问题