当前位置: 代码迷 >> Web前端 >> 于httpclient上传文件是中文名称的处置
  详细解决方案

于httpclient上传文件是中文名称的处置

热度:384   发布时间:2013-11-25 13:22:27.0
于httpclient上传文件是中文名称的处理
在一个项目中,上传文件采用httpclient来post文件,在测试中发现如果文件是中文名称,上传的文件是乱码
经过跟踪发现,原来在httpclient中进行了编码,为ASCII,所以为乱码
org\apache\commons\httpclient\util包下EncodingUtil.Java

/**
*ConvertsthespecifiedstringtobytearrayofASCIIcharacters.
*
*@paramdatathestringtobeencoded
*@returnThestringasabytearray.
*
*@since3.0
*/
publicstaticbyte[]getAsciiBytes(finalStringdata){

if(data==null){
thrownewIllegalArgumentException("Parametermaynotbenull");
}

try{
returndata.getBytes("US-ASCII");
}catch(UnsupportedEncodingExceptione){
thrownewHttpClientError("HttpClientrequiresASCIIsupport");
}
}

解决方法:
1在接收端处理ascii数据
2重新编译httpclient包,即更改上面的方法,改为iso8859-1或utf-8
这样可以解决中文的问题

50校招生网 http://www.50xiao.com
  相关解决方案