When uploading a file with httpclient upload, the code is as follows:
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/new Folder/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);
Always prompt for the following exception:
</pre></p><p><b>root cause</b> <pre> Org.apache.commons.fileupload.FileUploadException:the request was rejected because no multipart boundary was Foundorg.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 are available in the Xqs logs.</u></p& GT;<HR size= "1" noshade= "NoShade" >
The reason is that the value of contenttype is set. When ContentType is set for boundary, the server does not know what to do with boundary.
Therefore, delete the ContentType setting.
HttpClient Upload File