我正在使用 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()
PS 当我在 Firefox Firebug 中看到标题时 - 值没问题:Content-Disposition
附件; filename="Resolución sobre Mesas de Contratación.pdf"