[英]Http Client POST upload file - MultipartException: Curren t request is not a multipart request
I need to test file upload using httpclient 4.5 我需要使用httpclient 4.5测试文件上传
Below method is being used to upload a file: 正在使用以下方法上传文件:
public Response postwithFile(String url, File file) {
HttpPost postMethod = new HttpPost(PropertyUtil.loadEnvironment().getBaseUrl() + url);
postMethod.setHeader("Content-Type","multipart/form-data");
FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
//_addAuthHeader(postMethod);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// fileParamName should be replaced with parameter name your REST API expect.
builder.addPart("upfile", fileBody);
HttpEntity entity = builder.build();
postMethod.setEntity(entity) ;
return execute(postMethod);
}
The file do not have any extension but the content of the file is JSON. 该文件没有任何扩展名,但文件的内容为JSON。
On calling above method I receive 500 error with below exception in server logs: 在调用上述方法时,我在服务器日志中收到500错误和以下异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Curren
t request is not a multipart request
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
Can anyone please help where I am doing wrong? 谁能帮我在哪里做错了?
Use following 使用以下
builder.addBinaryBody(
"upfile",
new FileInputStream(file),
ContentType.APPLICATION_OCTET_STREAM,
file.getName()
);
instead of 代替
builder.addPart("upfile", fileBody);
Also following is no longer required as it is also deprecated:- 也不再需要遵循以下条件,因为它也已弃用:-
FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.