如何从远程文件(Java)获取修改的日期?

如何从远程文件(Java)获取修改的日期?,java,stream,Java,Stream,我有一个从远程URL下载文件的功能(使用Java)。现在我想知道真正的修改日期,因为当我下载它时,我丢失了这些信息。提前谢谢 public void downloadFile(String remoteFile, String localFile) throws IOException { BufferedInputStream in; try { URL url = new URL(remoteFile); in = new

我有一个从远程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.");
    }
}

任何体面的Web服务器都会将此信息放在响应头中。您可以通过以下方式获得它。这里有一个例子

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 = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).parse(lastModified);