【发布时间】:2013-08-14 12:02:51
【问题描述】:
通过 HTTPS 下载文件、保留时间戳并使用文件名的内容处置的最简单的 java 方法是什么?有没有比apache-httpclient级别更高的java库?
目前我有:
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!");
}
【问题讨论】:
-
您对 Content-Disposition 的解析仅适用于最简单的情况。您可能需要查阅规范 (RFC 6266)。
标签: java apache-httpclient-4.x last-modified content-disposition