我有一个从远程URL(使用Java)下载文件的功能。现在我想知道真正的修改日期,因为当我下载它时我丢失了这个信息。提前谢谢。
public void downloadFile(String remoteFile, String localFile)
throws IOException {
BufferedInputStream in;
try {
URL url = new URL(remoteFile);
in = new BufferedInputStream(url.openStream());
FileOutputStream fos = new FileOutputStream(localFile);
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte data[] = new byte[1024];
int count = 0;
while ((count = in.read(data, 0, 1024)) > 0) {
bout.write(data, 0, count);
}
bout.close();
in.close();
log.write(remoteFile + " - Download Successful.");
//System.out.println(remoteFile + " - Download Successful.");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
log.write("The file " + remoteFile + " doesn't exist.");
//System.out.println("The file " + remoteFile + " doesn't exist.");
}
}
答案 0 :(得分:11)
任何体面的网络服务器都会将此信息放在Last-Modified
响应标头中。您可以通过URLConnection#getHeaderField()
获取它。这是一个例子
URLConnection connection = new URL("http://sstatic.net/so/img/logo.png").openConnection();
String lastModified = connection.getHeaderField("Last-Modified");
System.out.println(lastModified);
现在打印
Sun, 17 Jan 2010 18:29:31 GMT可轻松将其转换为
Date
个对象
Date date = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).parse(lastModified);
答案 1 :(得分:1)
在下载之前将文件夹与文件一起压缩,然后下载zip文件夹并将其解压缩到那里。这将保留修改和创建的日期。我不知道这个答案是否适合您。