在一个项目中,上传文件采用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控件,

|