用httpclient upload上传文件时,代码如下:
HttpPost httpPost = new HttpPost(uploadImg);
httpPost.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
httpPost.addHeader("Accept-Encoding", "gzip, deflate");
httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
httpPost.addHeader("Cache-Control", "max-age=0");
httpPost.addHeader("Connection", "keep-alive");
httpPost.addHeader("Content-Type", "multipart/form-data;"); File file = new File("/Users/xxx/Desktop/新建文件夹/1.jpg"); byte[] fileBytes = readInputStream(new FileInputStream(file));
byte[] imgBytes = Base64.encodeBase64(fileBytes);
String img = new String(imgBytes);
img = img.replace("\n", ""); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName());
HttpEntity entity = builder.build();
httpPost.setEntity(entity); HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpPost);
String responseStr = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
System.err.println(responseStr);
一直提示如下异常:
</pre></p><p><b>root cause</b> <pre>org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:990)
org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)
org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:892)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.shangou.trace.http.BraveServletFilterUtil.doFilter(BraveServletFilterUtil.java:54)
com.shandiangou.member.session.SdgSessionFilter.doFilter(SdgSessionFilter.java:130)
</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the xqs logs.</u></p><HR size="1" noshade="noshade"><h3>xqs</h3></body></html>
原因在于设定了contentType的值。当为boundary设定了contentType后,server不知道boundary如何处理。
因此,删掉contentType的设定即可。