【问题标题】:Implementing resume for download files via internet实现通过互联网下载文件的简历
【发布时间】:2017-07-26 10:25:16
【问题描述】:

当不为此实现resume 时,我用于下载文件的以下代码工作正常,在阅读了更多实现该解决方案并解决问题后,我知道我必须检查Last-Modified 标头并将其设置为连接,

但我不能这样做,因为我得到了诸如android Cannot set request property after connection is made 之类的错误,或者我得到null for httpURLConnection

我正在使用这个reference

getHeaderField heaser 返回:

{
  null=[HTTP/1.1 200 OK], 
  Cache-Control=[public], 
  Connection=[keep-alive], 
  Content-Length=[8037404], 
  Content-Md5=[VEqXHCc/Off7a6D0gRFpiQ==], 
  Content-Type=[image/jpeg], 
  Date=[Tue, 19 Jan 2016 07:24:36 GMT], 
  Etag=["544a971c273f39f7fb6ba0f481116989"], 
  Expires=[Sat, 29 Jul 2017 10:07:00 GMT], 
  Last-Modified=[Thu, 18 Dec 2014 08:44:34 GMT], 
  Server=[bws], 
  X-Android-Received-Millis=[1501063623576], 
  X-Android-Response-Source=[NETWORK 200], 
  X-Android-Selected-Protocol=[http/1.1], 
  X-Android-Sent-Millis=[1501063623532]
}

现在我如何设置它以恢复下载文件?

GitHub Link

public void run() {
    final URL         url;
    HttpURLConnection httpURLConnection = null;
    try {
        try {
            url = new URL(mUrl);
            String lastModified = httpURLConnection.getHeaderField("Last-Modified");
            if (!lastModified.isEmpty()) {
                httpURLConnection.setRequestProperty("If-Range", lastModified);
            }
            httpURLConnection = (HttpURLConnection) url.openConnection();

            if (mFile.exists()) {
                downloadedLength = mFile.length();
                Log.e("downloadedLength ", downloadedLength + "");
                httpURLConnection.setRequestProperty("Range", "bytes=" + downloadedLength + "-");
                fileOutputStream = new FileOutputStream(mFile, true);
            } else {
                fileOutputStream = new FileOutputStream(mFile);
            }
            httpURLConnection.setConnectTimeout(30000);
            httpURLConnection.setReadTimeout(30000);
            httpURLConnection.setRequestMethod("GET");
        } catch (IOException e) {
        }
        final int responseCode;
        final int total;
        try {
            responseCode = httpURLConnection.getResponseCode();
            total = httpURLConnection.getContentLength();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("ER UPDATE ", e.getMessage());
        }
        if (responseCode == 200) {
            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());
    }
}

我也得到206 响应代码而不是200

【问题讨论】:

  • 为什么你不使用DownloadManager,它“在后台下载,处理HTTP交互并在失败或跨连接更改和系统重启后重试下载”并且已经实现了“恢复”?
  • @AndriiOmelchenko 对于这个实现我找不到任何好的文档
  • DownloadManager 正是用于下载文件,它工作正常。试试this 例子。

标签: java android download


【解决方案1】:

1- 你得到了 httpURLConnection 的空值,因为你试图在初始化之前调用它,

即这条线

httpURLConnection = (HttpURLConnection) url.openConnection();

应该在这一行之前:

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

2- 你可以在httpURLConnection 上调用connect() 之前设置标题 所以你需要设置任何你想要的,然后连接()。这样你就不应该得到错误 (android Cannot set request property after connection is made)

3- 206perfectly right,这是您在使用 Range 时的 should expect,这意味着 部分内容成功,这就是您正在做的事情,您将获得部分内容,如果您获得完整内容,您将获得 200

总而言之,您的代码如下所示: 注意:按照//***查看所需的更改。

编辑:都到了这一行

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

设置该属性时会引发错误,

无论如何,当你看到这个时,它是没有意义的,你在问 last-modified 是否等于你刚刚从连接中获得的值!, 如果你想这样做,你需要在你的系统中存储 lastModified, 然后将其与您从 URLConn 获得的进行比较,并将其与您的文件长度(已下载)进行比较 然后继续完整下载或继续下载。

在下面找到新代码:

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

【讨论】:

  • @Mahdi.Pishguy ,是的,你是对的,我没有调查你的代码(逻辑明智)现在我做了,我发现“if-range”没有多大意义,所以检查我的新代码,看看 EDIT
  • 我收到此错误java.lang.IllegalStateException: Already connected 用于此行httpURLConnection.setDoInput(true);。我评论了httpURLConnection.connect();,git 上的存储库已更新
  • @Mahdi.Pishguy 删除它,我为您发表了评论,如果它引起问题,请删除它。
  • 嗨,伙计,你能帮我上this topic吗?谢谢
【解决方案2】:

看看thisPOMATu的回答。 但无论如何,如果您通过HTTP 协议下载文件,您可以使用DownloadManager - 一种系统服务(从API 级别9 开始)在后台进行长时间运行的下载。它处理HTTP 连接、连接更改、重新启动,并确保每次下载成功完成。而且它已经支持恢复以及进度通知。

你可以在thisthat 等例子中找到许多教程,在stackoverflow 上通过 标签找到许多解决方案。

【讨论】:

  • 也许你是对的,但它不能回答我的问题
  • 这取决于你 :) 是的 - 这不是一个答案:只是关于你的问题的其他方法的通知。可能对某人有用。
猜你喜欢
  • 2013-11-30
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多