commons.httpclient上传文件的文件名的中文问题
2009-07-01 15:17
267 查看
在利用httpclient向服务器post数据时,有两种中文问题;
1.filed字段值的中文
2.file名的中文
对于第一种,参看StringPart;其源代码有这样一段:
private byte[] getContent() {
if (content == null) {
content = EncodingUtil.getBytes(value, getCharSet());
}
return content;
}
protected void sendData(OutputStream out) throws IOException {
LOG.trace("enter sendData(OutputStream)");
out.write(getContent());
}
可以看出在发送数据时其调用了EncodingUtil的getBytes方法(利用了你通过setCharSet设置的编码)
因此,只要你在代码中这样:
StringPart part = new StringPart(name, value);
part.setCharSet("GBK");
中文就没有问题了
对于第二种,参看FilePart;其源代码中有这样一段:
protected void sendDispositionHeader(OutputStream out)
throws IOException {
LOG.trace("enter sendDispositionHeader(OutputStream out)");
super.sendDispositionHeader(out);
String filename = this.source.getFileName();
if (filename != null) {
out.write(FILE_NAME_BYTES);
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getAsciiBytes(filename));
out.write(QUOTE_BYTES);
}
}
可以看出在转换文件名时,用的方法是EncodingUtil.getAsciiBytes(),查看这个方法源码为data.getBytes("US-ASCII"),因此中文文件名必定乱码,不管你是否调用了setCharSet("GBK")方法。
解决很简单:
out.write(EncodingUtil.getBytes(filename, getCharSet()));
看了网上好多文章,好多都说改EncodingUtil类,其实我觉得改FilePart更好一些
1.filed字段值的中文
2.file名的中文
对于第一种,参看StringPart;其源代码有这样一段:
private byte[] getContent() {
if (content == null) {
content = EncodingUtil.getBytes(value, getCharSet());
}
return content;
}
protected void sendData(OutputStream out) throws IOException {
LOG.trace("enter sendData(OutputStream)");
out.write(getContent());
}
可以看出在发送数据时其调用了EncodingUtil的getBytes方法(利用了你通过setCharSet设置的编码)
因此,只要你在代码中这样:
StringPart part = new StringPart(name, value);
part.setCharSet("GBK");
中文就没有问题了
对于第二种,参看FilePart;其源代码中有这样一段:
protected void sendDispositionHeader(OutputStream out)
throws IOException {
LOG.trace("enter sendDispositionHeader(OutputStream out)");
super.sendDispositionHeader(out);
String filename = this.source.getFileName();
if (filename != null) {
out.write(FILE_NAME_BYTES);
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getAsciiBytes(filename));
out.write(QUOTE_BYTES);
}
}
可以看出在转换文件名时,用的方法是EncodingUtil.getAsciiBytes(),查看这个方法源码为data.getBytes("US-ASCII"),因此中文文件名必定乱码,不管你是否调用了setCharSet("GBK")方法。
解决很简单:
out.write(EncodingUtil.getBytes(filename, getCharSet()));
看了网上好多文章,好多都说改EncodingUtil类,其实我觉得改FilePart更好一些
相关文章推荐
- httpclient上传文件及文件名中文乱码问题
- 解决httpclient上传文件的时候中文文件名乱码的问题
- 解决HttpClient的FilePart上传文件中使用中文名称文件名乱码问题
- HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决
- 解决HttpClient的FilePart上传文件中使用中文名称文件名乱码问题
- 解决上传文件时服务端中文文件名乱码问题
- 使用Springmvc和commons-fileupload-1.2.2.jar实现上传文件时,中文文件名乱码
- commons-FileUpload文件上传下载,及中文乱码、IE7路径错误问题解决
- 【java】[文件上传jar包]commons-fileUpload组件解决文件上传(文件名)乱码问题
- HttpClient4.4.1 关于文件上传 中文文件名编码问题
- 使用edtftpnet上传文件到Serv-U出现乱码的问题,并且不能创建中文文件名或文件夹。
- 关于LINUX下JSP文件上传出现中文文件名 引起的问题
- Commons-fileupload组件上传中文文件名乱码问题
- 中文乱码问题(使用Servlet3.0新特性实现文件上传——上传文件名中文乱码问题)
- Commons-fileupload组件上传中文文件名乱码问题
- php上传中文文件文件名乱码问题
- 中文名文件上传到linux服务器文件名乱码问题(转)
- 解决上传文件时服务端中文文件名乱码问题
- PHP文件上传并解决中文文件名乱码问题
- httpclient 应用 文件上传 中文编码问题