用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的更多相关文章

  1. Angular HttpClient upload file with FormData

    从sof上找到一个example:https://stackoverflow.com/questions/46206643/asp-net-core-2-0-and-angular-4-3-file- ...

  2. 页面无刷新Upload File

    页面无刷新Upload File. 利用jquery.form.js的ajaxForm提交文件. 具体参考以下代码: 前台html <%@ Page Language="C#" ...

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

    jQuery文件上传插件jQuery Upload File 有上传进度条 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...

  4. Upload file

    <h3>Upload File</h3> <form action="@Url.Action("Upload","UploadCo ...

  5. Express web框架 upload file

    哈哈,敢开源,还是要有两把刷子的啊 今天,看看node.js 的web框架 Express的实际应用 //demo1 upload file <html><head><t ...

  6. apache php upload file

    /********************************************************************************* * apache php uplo ...

  7. fetch API & upload file

    fetch API & upload file https://github.com/github/fetch/issues/89 https://stackoverflow.com/ques ...

  8. 通过iframe 实现upload file无刷新

    <html>    <head> </head> <body> <form encType="multipart/form-data&q ...

  9. post upload file & application/x-www-form-urlencoded & multipart/form-data

    post upload file application/x-www-form-urlencoded & multipart/form-data https://stackoverflow.c ...

随机推荐

  1. ABAP术语-Lock Mode

    Lock Mode 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/29/1085732.html Status that determine ...

  2. ABAP术语-EDI (Electronic Data Interchange)

    EDI (Electronic Data Interchange) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/29/1057386.ht ...

  3. SI - 硬件 - 服务器 - 知识科普

    服务器对每个从事IT工作的人来说并不陌生,但是服务器所涉及的各种知识细节,并非大家都十分清楚,为了让大家深入了解服务器的关键知识点,笔者特意抽时间总结了这篇科普文章,旨在帮助读者全面了解服务器.今天内 ...

  4. 【mysql学习-1】

    part-1: #use mysql;/*show tables;select * from user;use mysql;show databases;#create database db1; # ...

  5. 发布django项目

    supervisor需要用到的技术 1. nginx反向代理 2. nginx负载均衡 3. uwsgi 4. supervisor 5. virtualenv 安装nginx 详情参考 https: ...

  6. Django模板语言(DTL)基础

    ## 模板变量 - 普通变量 {{ name }} - 对象变量(使用点号访问对象属性和方法,方法不加括号) {{ person.name }} ## 常用模板标签 # if标签,支持and,or,n ...

  7. 【tp5.1】composer安装PHPExcel以及导入\导出Excel

    一.安装PHPExcel 1.下载:PHPExcel  https://github.com/PHPOffice/PHPExcel 2.解压后:Classes文件夹改名为PHPExcel 3.把文件夹 ...

  8. mysql帐号不允许从远程登陆

    默认情况下,mysql帐号不允许从远程登陆,只能在localhost登录.本文提供了二种方法设置mysql可以通过远程主机进行连接. 一.改表法 在localhost登入mysql后,更改 “mysq ...

  9. MySQL的边角料

    //1查询对应数据库所有的表 SELECT * FROM information_schema.`TABLES` WHERE TABLE_SCHEMA ="数据库名" 2 查询数据 ...

  10. 3.从print到I/O

    为何对双引号念念不忘? >>> print("hello, world!") hello, world!   平x而论,既然在意双引号的去掉,为何不在意括号的去掉 ...