0

What's the simplest java way to download a file over HTTPS, preserving timestamps and using the content-disposition for the filename? Is there any java library at a higher level than apache-httpclient?

Currently I've got:

    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse = httpClient.execute(new HttpGet(parser.sourceUrl));
    Header cd = httpResponse.getLastHeader("Content-Disposition");
    String filename = cd.getValue().split(";")[1].split("=")[1]; // TODO(jayen): unhack
    HttpEntity httpEntity = httpResponse.getEntity();
    System.out.println("Saving " + filename);
    httpEntity.writeTo(new FileOutputStream(folder.getCanonicalPath() + File.separator + filename));
    if (httpResponse.containsHeader("Last-Modified")) {
        System.err.println("Please implement timestamping");
    } else {
        System.out.println("No timestamp available!");
    }
4

2 回答 2

0

Neither the HTTP protocol or the multipart content-type specs provide any way of encoding arbitrary source file metadata. The HTTP spec defines a Last-Modified header that can be set by the server, but it is not required to do this. But more importantly, typical browsers do not preserve this timestamp. (Some command line tools do ... but that's a different matter.) The Content-Disposition header is not part of the HTTP 1.1 spec, but a lot of servers support it anyway.

Options:

  • If you are using a Java library to do the fetching, then you should be able to get the "modified" timestamp and content disposition from the Response headers. Refer to the relevant client library's tutorial information.

  • If you can use wget or curl or equivalent, they should be able to preserve the timestamp.

  • You may be able to install a (trusted) plugin that will preserve timestamps; e.g. this one for Firefox: https://addons.mozilla.org/en-us/firefox/addon/preserve-download-modification/. However, I think that it is unlikely that you can do this in untrusted Javascript ... for security reasons.

  • You could change the server to package the file in archive format that can represent the metadata you want to include, and download the file as an archive. Then use the relevant archive extractor command to extract the file. This will allow you to transfer other metadata such as the original owner, access control details and so on.

Alternatively, you could use the Linux / Unix "scp" or "rsync" commands to do the file transfer.

于 2013-08-13T03:42:19.637 回答
0

检查此答案以使用 HttpClient 在 Java 11 中下载文件

此答案中保留的元数据是文件名,因为文件名是从Content-Disposition响应标头中获取的。

于 2021-05-11T17:39:41.167 回答