【问题标题】:Only download file if updated?更新后才下载文件?
【发布时间】:2013-09-03 00:06:10
【问题描述】:

我正在使用下面的代码下载文件。但是,如果远程文件比本地存储的文件更新(如果有的话),我只想下载。我可以以某种方式使用 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 循环,对吗?

【问题讨论】:

  • 避免整个吞下异常;至少,将堆栈跟踪打印到控制台,并尽量不要捕获所有可能的异常,只捕获已检查的异常。
  • 是的,我的错。我已经相应地更新了代码。

标签: java android


【解决方案1】:

如果文件标有较新的日期但在二进制级别上是相同的怎么办?解决仅下载较新版本文件问题的更好方法可能是在服务器上保留文件的哈希值并首先下载哈希值,然后将其与本地文件的哈希值进行比较。如果不匹配,则从服务器获取完整文件。如果我假设同一个文件可以有不同(和更新)的修改日期成立,那么可能会为您的用户节省一些带宽。

【讨论】:

  • 这是个好主意。不幸的是,我无法访问服务器。
【解决方案2】:

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

【讨论】:

  • 或者设置If-Modified-Since头部,看看响应状态码是不是304,那么你就只发一个请求。
猜你喜欢
  • 2013-03-22
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多