How to get file properties without downloading

2020-07-27 03:09发布

How to get file properties (e.g. date) without downloading file if I have URL of the file.

I have to implement updates checker. For this purpose I'm going to compare dates of two files. One is local, the second is the same file but with latest date on server. I know URL to the second file.

Please, give me a peace of advice - how to check date of the second file without downloading it?

I have thought about comparing via hash, but I need to check whether file on server is latest version or not?

2条回答
神经病院院长
2楼-- · 2020-07-27 03:43

Checking properties of a remote file is not that easy. This behavior IS NOT specified in the URL specification.

So this should be supported by the target protocol. Note that not all protocols support this. Say HTTP does not support this, while FTP and CIFS does.

Once you are sure your target protocol does support this feature, you need to relay on the protocol's specification to do this. So if you are using FTP, look for a library like ftp4j so that you don't have implement a FTP client yourself.

UPDATE

This is not supported by HTTPS out of the box. You need to configure the remove web server to expose the needed versioning data. Say you can put (or write a script to do this) all versioning information in a separate text file. You can then fetch this file and check the version you need.

Best wishes!

查看更多
Summer. ? 凉城
3楼-- · 2020-07-27 03:47

To check the file date you can use this snippet of code

try {
  URL url = new URL(inputFile);
  URLConnection urlConnection = url.openConnection();
  System.out.println("Date= "+new Date(urlConnection.getLastModified()));
  System.out.println("Size= "+urlConnection.getContentLength());

} catch (MalformedURLException e1) {
  e1.printStackTrace();  //Todo change body of catch statement.
} catch (IOException e1) {
  e1.printStackTrace();  //Todo change body of catch statement.
}

with the attributes of date, size, name you can identify the resource.

查看更多
登录 后发表回答