java– 如果更新,只下载文件?

java– 如果更新,只下载文件?,第1张

概述我正在使用下面的代码下载文件.但是我只想下载如果远程文件比较新,那么本地存储(如果有的话).我可以以某种方式使用if-modefied-sincehttp标头?如何更新我的代码以存档我的目标?privateclassDownloadFileextendsAsyncTask<String,Integer,String>{@Overrideprote

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

解决方法:

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

总结

以上是内存溢出为你收集整理的java – 如果更新,只下载文件?全部内容,希望文章能够帮你解决java – 如果更新,只下载文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1106875.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存