HttpClient Upload File

Source: Internet
Author: User
Tags stack trace

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;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.