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的设定即可。

  

时间: 2024-10-24 15:13:09

httpclient upload file的相关文章

jQuery文件上传插件jQuery Upload File 有上传进度条

jQuery文件上传插件jQuery Upload File 有上传进度条 2015年05月15日 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度条显示.标准HTML form文件上传,也就是说,只要服务端支持接收multipart/form-data格式数据就能使用此上传插件. 本站web端文件文件提交即使用此插件,效果如下: 浏览器支持 浏览器支持:IE 8.0,IE 9.0,IE 10.0,Firefox,Saf

Upload file

<h3>Upload File</h3> <form action="@Url.Action("Upload","UploadController")" method="post" id="uploadForm" enctype="multipart/form-data"> <div class="file-box">

ubuntu curl upload file to apache2 server

ubuntu curl upload file to apache2 server Table of Contents 1. install 2. get web info 3. set php upload conditions 3.1. ref 3.2. upload_max_fileszie 3.3. post_max_size 3.4. max_execution_time cfg 3.5. restart after cfg 4. config upload directory 5.

MVC-前台调用后台action 传递upload file 参数问题

在后台获取upload file 数量的时候发现count一直为0,经检查发现了问题 ,代码如下: 前台: var data = $("#DetailForm").serialize(); $.ajax({ url: '@Url.Action("SaveRequest", "RegistrationRequest")', type: "POST", dataType: "JSON", data: data,

Express web框架 upload file

哈哈,敢开源,还是要有两把刷子的啊 今天,看看node.js 的web框架 Express的实际应用 //demo1 upload file <html><head><title>文件上传表单</title></head><body><h3>文件上传:</h3>选择一个文件上传: <br /><form action="/file_upload" method="

html 5 drag and drop upload file

compatible: chrome firefox ie 11 , not supported demo: http://demo.tutorialzine.com/2011/09/html5-file-upload-jquery-php/ http://www.sitepoint.com/author/craig-buckler/page/23/ http://www.sitepoint.com/html5-file-drag-and-drop/ http://www.sitepoint.c

[Selenium] Upload File on Remote Node Machines

Problem: In web UI automation testing, we always need to upload files, it will work well on local machines, but doesn't work on remote node machines when using Seleniuim Grid to run. Solution: No Description Recommendation Index 1 1.Use PathUtils to

Using AFNetWorking 2.0 upload file to php web service server based on Slim

Recently i am developing the IOS app, a feature is needed to upload image to the webservice server. I am using the AFNetworking library. The function is as follow. -(void)SendImage:(NSData *)imgData { NSDictionary *parameters = @{@"ShopID": [NSS

ABAP upload file(*.txt *.csv *.xls)

转自:http://blog.csdn.net/jy00873757/article/details/8534492 在SAP我们经常会用到*.txt, *.csv, *.xls三种文件格式 *.TXT 1. 一般用GUI_UPLOAD来处理, 请看下面的例子: [plain] view plain copy TYPES: BEGIN OF ty_luke, key1 TYPE zluke-key1, year1 TYPE zluke-year1, month1 TYPE zluke-month