解决java上传中文文件名乱码

commons-httpclient-3.0.1.jar
类:org.apache.commons.httpclient.methods.multipart.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);
}
}

类:org.apache.commons.httpclient.methods.multipart.Part
...
protected void sendDispositionHeader(OutputStream out) throws IOException {
LOG.trace("enter sendDispositionHeader(OutputStream out)");
out.write(CONTENT_DISPOSITION_BYTES);
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getAsciiBytes(getName()));//同上
out.write(QUOTE_BYTES);
}

如上所示,文件名不进行特殊处理,则官方默认使用Ascii编码,对英文以外的编码是个挑战,
所以需重写sendDispositionHeader方法,且不继承父类实现,方可实现中文文件名的正常上传,如下:

@Override
protected void sendDispositionHeader(OutputStream out) throws IOException {
// 实现基类Part方法
out.write(CONTENT_DISPOSITION_BYTES);
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getBytes(getName(), "gbk"));//OK,通过
out.write(QUOTE_BYTES);
// 实现父类FilePart方法
String fileName = getSource().getFileName();
if (fileName != null) {
out.write(EncodingUtil.getAsciiBytes(FILE_NAME));
out.write(QUOTE_BYTES);
out.write(EncodingUtil.getBytes(fileName, "gbk"));//OK,通过
out.write(QUOTE_BYTES);
}

以上为3.0.1版本的操作,故寻至新版4.3.1,该头部实现已经修改,且无编码区别,官方实现如下:
protected void generateContentDisp(ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append(getName());
buffer.append("\"");
if (body.getFilename() != null) {
buffer.append("; filename=\"");
buffer.append(body.getFilename());
buffer.append("\"");
}
addField("Content-Disposition", buffer.toString());
}
故建议旧版切换即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值