仅在有更新时下载文件?

3
我正在使用下面的代码下载文件。然而,我只想在远程文件比本地存储的文件更新时(如果有的话)才进行下载。我能否以某种方式使用 if-modefied-since HTTP 标头?如何更新我的代码来实现这个目标?
private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(path);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        }
        catch(MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        mProgressDialog.dismiss();

        // TODO: here file is downloaded and we are ready to process it.
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }
}

我已更新我的代码,看起来像这样...

    @Override
    protected String doInBackground(String... sUrl) {
        long lastModified = new File(path).lastModified();
        try
        {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            if(lastModified != 0)
            {
                connection.setIfModifiedSince(lastModified);
            }
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();
...

有没有关于如何实际测试这个的好主意?如果文件不是更新的,那么while循环就不应该运行,对吗?

1
避免整个吞噬异常;至少将堆栈跟踪打印到控制台,并尝试不要捕获所有可能的异常,只捕获已检查的异常。 - chrylis -cautiouslyoptimistic-
是的,我的错。我已经相应地更新了代码。 - user672009
https://dev59.com/nWgv5IYBdhLWcg3wI9Sg - Robert Rowntree
2个回答

3

这正是该标头的目的。您需要发出HTTP HEAD请求,仅获取标题,然后将标头中的时间戳与文件上的最后修改时间戳进行比较。如果服务器上的副本更新,则发出GET以下载新副本。


5
或者设置If-Modified-Since头部并检查响应状态码是否为304。这样你只需要发起一次请求。 - David Snabel-Caunt

2
如果文件标记了更新的日期,但在二进制级别上相同怎么办?也许更好的解决方案是在服务器上保留文件的哈希值,先下载哈希值,然后将其与本地文件的哈希值进行比较。如果不匹配,则从服务器获取完整文件。如果我的假设成立,即同一文件可能具有不同(且更新的)修改日期,则可以为用户节省一些带宽。

这是一个非常好的想法。不幸的是,我没有访问服务器的权限。 - user672009

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接