1 后台

(1)java

@RequestMapping(value = "/showapi", method = RequestMethod.POST)
public ResponseEntity showapi(@RequestParam("file") MultipartFile multipartFile) {byte[] byteImg = multipartFile.getBytes();
}

普通的接口,只不过接收参数类型为MultipartFile

(2)spring配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="100000" /><property name="maxInMemorySize" value="100000" />
</bean>
dispatcherServlet是没有处理multipart/form-data,需要单独配置一个resolver,id="multipartResolver"默认如此设置

(3)maven依赖

<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.1</version> <!-- makesure correct version here -->
</dependency>

2 客户端

public static String decaptcha(File file) {CloseableHttpClient client = HttpClients.createDefault();HttpPost post = new HttpPost(ReadProperties.read("captcha.url"));RequestConfig config = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(20000).build();post.setConfig(config);
    FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);MultipartEntityBuilder builder = MultipartEntityBuilder.create();builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart("file", fileBody);
    // 相当于 <input type="file" class="file" name="file">,匹配@RequestParam("file")// .addPart()可以设置模拟浏览器<input/>的表单提交
    HttpEntity entity = builder.build();post.setEntity(entity);String result = "";try {CloseableHttpResponse e = client.execute(post);HttpEntity resEntity = e.getEntity();if(entity != null) {result = EntityUtils.toString(resEntity);System.out.println("response content:" + result);}} catch (IOException var10) {logger.error("请求解析验证码io异常",var10);var10.printStackTrace();}return result;
}

附录(浏览器请求数据格式):

------WebKitFormBoundaryqgkaBn8IHJCuNmiW

Content-Disposition: form-data; name="firstName"Charles

------WebKitFormBoundaryqgkaBn8IHJCuNmiW

Content-Disposition: form-data; name="lastName"Xavier

------WebKitFormBoundaryqgkaBn8IHJCuNmiW

Content-Disposition: form-data; name="email"charles@xmen.com

------WebKitFormBoundaryqgkaBn8IHJCuNmiW

Content-Disposition: form-data; name="username"professorx

------WebKitFormBoundaryqgkaBn8IHJCuNmiW

Content-Disposition: form-data; name="password"letmein01

------WebKitFormBoundaryqgkaBn8IHJCuNmiW

Content-Disposition: form-data; name="profilePicture"; filename="me.jpg"Content-Type: image/jpeg

[[ Binary image data goes here ]]

------WebKitFormBoundaryqgkaBn8IHJCuNmiW--

这些name就是映射到@RequestParam(name)

CloseableHttpClient 和 MultipartFile 配合上传文件相关推荐

  1. SpringBoot MultipartFile 监控上传文件进度

    # SpringBoot MultipartFile监控上传文件进度>引用块内容 在一次项目中需要监控文件上传的进度.将进度监控到之后计算百分比,存入session中session需要配置实时生 ...

  2. java的springboot项目操作阿里云OSS下载文件、查看文件内容、上传文件,自定义工具类

    因为要从oss下载.查看.上传工具类,所以对这几个方法做了一个封装,已经经过测试,可以直接使用 1.yml添加上阿里云配置.添加maven配置 注意这里的objectName: xxx/xxx/,前面 ...

  3. Springboot实现文件上传文件压缩和前端展示

    最近公司要写一个文件上传和下载的需求,之前写过很多次了,今天做个记录,希望能帮助到大家*Controller 代码* @RestController @RequestMapping("/up ...

  4. HttpClient上传文件传入MultipartFile类型

    通常我们在使用httpclient的时候,一把都是使用get或者postd的方式传输一些数据.在近期的项目中有这样的一个需求,我需要通过httpclient去调用一个写好的文件上传的接口,接口中是使用 ...

  5. 解决使用Spring Boot、Multipartfile实现上传提示无法找到文件的问题

    解决使用Spring Boot.Multipartfile实现上传提示无法找到文件的问题 参考文章: (1)解决使用Spring Boot.Multipartfile实现上传提示无法找到文件的问题 ( ...

  6. springBoot上传文件时MultipartFile报空问题解决方法

    springBoot上传文件时MultipartFile报空问题解决方法 参考文章: (1)springBoot上传文件时MultipartFile报空问题解决方法 (2)https://www.cn ...

  7. SpringMVC MultipartFile 异步批量上传文件

    使用MultipartFile,前端通过异步方式提交,以上传图片做个简例,只介绍前端及处理层代码 需求简述: 1.spring配置 <bean id="multipartResolve ...

  8. 我使用Feign上传文件踩的坑,MultipartFile文件死活传不过去

    我使用Feign上传文件踩的坑,MultipartFile文件死活传不过去 Feign上传文件 基本信息:通过后台管理,更改APP端的头像.我是用feign来调用app端的修改头像接口.两个参数,st ...

  9. 亿速云服务器 如何上传文件,使用MultipartFile怎么实现一个文件上传功能

    使用MultipartFile怎么实现一个文件上传功能 发布时间:2021-01-20 16:43:15 来源:亿速云 阅读:139 作者:Leah 使用MultipartFile怎么实现一个文件上传 ...

最新文章

  1. 【MATLAB】进阶绘图 ( colormap 颜色图矩阵分析 | 自定义 colormap 颜色图 | 生成 64 x 3 的 colormap 颜色图矩阵 )
  2. SAP BW数据源增强管理
  3. express使用JWT和httpOnly cookie进行安全验证
  4. 一文读懂Python 装饰器函数
  5. spark 查看yarn日志_spark周边项目之Livy
  6. SAP License:PS模块WBS预算更改
  7. android 开机小企鹅_手机root是好是坏 小编来帮你分析
  8. 程序员都在用的电脑小技巧,一遍就学会,每天早下班一小时
  9. python 教程 w3 school_Python 模块 | w3cschool菜鸟教程
  10. javaw和java,java,javaw和javaws之间有什么区别?
  11. 收藏 | 江苏省各地教师公务员等实际工资爆料
  12. 用Kali进行ARP断网攻击
  13. Docker搭建ElasticSearch
  14. 【热门】女孩取名:女孩名字带歆什么寓意
  15. nginx静态文件缓存
  16. Regulator相关GPIO控制使用流程简析
  17. 费马小定理看了等于没看证明
  18. 各大高校自曝状态一览 排名不分先后
  19. 佳能c3020维修模式 白电平调整_佳能相机拍摄人脸偏红,怎么设置?
  20. 【UIL框架】Universal-Image-Loader全然解析(一)之介绍与使用具体解释

热门文章

  1. StudyNotes_MachineLearning_2(吴恩达机器学习公开课)
  2. python gui tkinter_Python GUI tkinter 学习笔记(一)
  3. 二叉树 红黑树 B树 B+树的优缺点
  4. 为什么要早点进入IT行业?
  5. 联想微型计算机620S,小巧、精致!联想ideacenter 620s远不止如此
  6. spring boot 集成框架事例
  7. 前端面试技巧和注意事项_面试Web前端需要注意什么?会面试哪些问题?
  8. CUDA安装失败-Nsight compute安装失败-如何测试CUDA是否安装成功?Reason: VS** was not found
  9. STF安装与使用(windows)
  10. sci期刊发表文章注意事项