How to get file properties without downloading

纵饮孤独 提交于 2019-12-08 09:37:13

问题


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?


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/12250258/how-to-get-file-properties-without-downloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!