在一个项目中,上传文件采用httpclient来post文件,在测试中发现 如果文件是中文名称,上传的文件是乱码
经过跟踪发现,原来在httpclient中进行了编码,为ASCII,所以为乱码
org/apache/commons/httpclient/util包下EncodingUtil.java
/**
* Converts the specified string to byte array of ASCII characters.
*
* @param data the string to be encoded
* @return The string as a byte array.
*
* @since 3.0
*/
public static byte[] getAsciiBytes(final String data) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
return data.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
throw new HttpClientError("HttpClient requires ASCII support");
}
}
解决方法:
1 在接收端处理ascii数据
2 重新编译httpclient包,即更改上面的方法 ,改为iso8859-1或utf-8
这样可以解决中文的问题
采用httpclient是一个很好的方法,可以给你其他的系统post数据,好像delphi中的indy控件,
HttpClient上传中文文件名文件乱码问题解决
项目中用HttpClient上传文件,若文件名是中文会出现乱码。经跟踪发现,HttpClient中编码为ASCII导致此问题。解决方法一是在接收端处理ASCII数据,二是重新编译HttpClient包,将编码改为iso8859 - 1或utf - 8。
4022

被折叠的 条评论
为什么被折叠?



