The request sent by the client was syntactically incorrect.

本文介绍了解决使用SpringMVC通过HttpClient上传文件时出现的“Required request part 'is not present’”错误的方法。作者从尝试使用InputStream代替File进行上传开始,最终通过手动指定文件名解决了问题,并成功实现了文件上传进度的跟踪。

客户端使用httpclient上传文件到服务端由spring mvc接收

本来用addBinaryBody传入File参数是正常运作的

然后为了实现进度条跟踪而改为传入InputStream参数就报错了

错误:

Required request part '' is not present
The request sent by the client was syntactically incorrect.

由于使用了Commons.io的CountingInputStream,还一度认为是inputstream不兼容的问题

后来甚至尝试直接覆写InputStreamBody的write方法来手动跟踪进度,结果还是同样的错误,
在阅读源码的时候才注意到
    public MultipartEntityBuilder addBinaryBody(final String name, final InputStream stream) {
        return addBinaryBody(name, stream, ContentType.DEFAULT_BINARY, null);
    }
最后那个参数直接传入了空
而原来传入File的方法,则根据文件获取了名字
    public MultipartEntityBuilder addBinaryBody(
            final String name, final File file) {
        return addBinaryBody(name, file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
    }

改为使用public MultipartEntityBuilder addBinaryBody(
            final String name, final InputStream stream, final ContentType contentType,
            final String filename)
方法手动添加filename
加上名字之后果然就没有报错了,顺利上传并跟踪更新了进度条。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值