Java – Only download files after updating?

Only download files after updating?… here is a solution to the problem.

Only download files after updating?

I’m downloading the file using the code below. However, if the remote file is newer (if any) than the locally stored file, I just want to download it. Can I somehow use the if-modified-since http header? How do I update my code to achieve my goals?

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

I’ve updated my code to look like this….

    @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();
...

Any good ideas on how to actually test it? If the file isn’t updated, the while loop shouldn’t run, right?

Solution

That is precisely the purpose of the title. You need to make an HTTP HEAD request to get only the header, and then compare the timestamp in the header with the last modified timestamp on the file. If the server’s copy is newer, issue a GET to download the new copy.

Related Problems and Solutions