【发布时间】:2017-01-16 15:51:01
【问题描述】:
我正在使用 Webharvest 从网站下载文件并取其原始名称。
我正在使用的 Java 代码是:
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.methods.GetMethod;
HttpClient client = new HttpClient();
BufferedReader br = null;
StringBuffer result = new StringBuffer();
String attachName;
GetMethod method = new GetMethod(attachmentLink.toString());
int returnCode;
returnCode = client.executeMethod(method);
Header[] headers = method.getResponseHeader("Content-Disposition");
attachName = headers[0].getValue();
attachName = new String(attachName.getBytes());
webharvest 中的结果是:
附件; filename="Resoluci�n sobre Mesas de Contrataci�n.pdf"
我不能让它接受这封信
ó
在将标头 Content-Disposition 的值放入变量 attachName 后,我也尝试对其进行解码,但没有成功:
String attachNamef = URLEncoder.encode(attachName, "ISO-8859-1");
attachNamef = URLEncoder.decode(attachNamef, "UTF-8");
我能够确定响应字符集是:ISO-8859-1
method.getResponseCharSet()
附:当我在 Firefox Firebug 中看到标题时 - 值没问题: 内容处置
附件; filename="Resolución sobre Mesas de Contratación.pdf"
【问题讨论】:
-
请注意,响应字符集是指有效负载,而不是标头字段。另请注意,您似乎使用的是非常过时的 HTTP 组件版本。最后,服务器响应无效;此处不允许使用非 ASCII 字符;请参阅 RFC 6266。
标签: java http encoding response webharvest