httpclient upload file
Posted 飞昂之雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient upload file相关的知识,希望对你有一定的参考价值。
用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的设定即可。
以上是关于httpclient upload file的主要内容,如果未能解决你的问题,请参考以下文章
如何在 C# 中将 HttpClient PostAsync() 与线程池一起使用?
Cordova/Phonegap FileTransfer.upload() 错误代码 = 1 (FILE_NOT_FOUND_ERR)
pikachu File Inclusion, Unsafe File Download & Unsafe File Upload
为啥我会像在 ng-file-upload 示例使用代码中那样在 AngularJS $timeout 服务中包装一个函数,而不会延迟?