java - 更新后只下载文件?

标签 java android

我正在使用下面的代码下载文件。但是,如果远程文件比本地存储的文件更新(如果有的话),我只想下载。我能以某种方式使用 if-modified-since http header 吗?如何更新我的代码以实现我的目标?

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 循环不应该运行,对吗?

最佳答案

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

关于java - 更新后只下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18529875/

相关文章:

java - 戈戈 : BundleException: An error occurred trying to read the bundle in OSGi with Equinox

java - 当它是具有相同 Action 监听器的 JButton 数组时,正在单击哪个 Jbutton

java - Spring单例被调用两次

java - Java中的正则表达式问题

android - Android-App 与本地 Python-App 之间的通信

java - 泛型构造函数接受类型为 T 的类对象,并返回 T,这次 T 是一个 List

android - 如何在 Android Studio 上查看 Gradle 警告?

Android ListView notifyDataSetChanged

android - Upi 超链接没有被调用 url

android - 如何在 Android 中创建带有简单分隔线或没有分隔线的按钮面板?