使用MultipartEntityBuilder实现文件上传

参考:【1】

原理
  1. 从httpclient 4.3开始,使用MultipartEntityBuilder实现文件上传
  2. 文件或者文件流可以封装成ContentBody,进而被封装到MultipartEntityBuilder中,再创建出multipartEntity,作为HTTP请求的HttpEntity,最终被发送到服务端。整个对象封装流程如下:File/Stream>>ContentBody>>MultipartEntityBuilder>>multipartEntity>>HttpEntity
准备工作
  1. 引入httpclient依赖

    <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5.8</version>
    </dependency>
    
使用addPart+File方法上传

使用addPart(String name, ContentBody contentBody)方法完成上传,其中ContentBody的实现类是FileBody。支持一次传多个文件。

HttpPost uploadPost = new HttpPost(uri);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart(TENANT_ID, new StringBody(tenantId, ContentType.TEXT_PLAIN)).addPart("path", new StringBody(path, ContentType.TEXT_PLAIN));
for (int i = 0; i < files.length; i++) {FileBody fileBody = new FileBody(files[i]);builder.addPart("file", fileBody);
}
HttpEntity multipartEntity = builder.build();
uploadPost.setEntity(multipartEntity);
CloseableHttpResponse response = httpClient.execute(uploadPost)
使用addPart+Stream方法上传

使用addPart(String name, ContentBody contentBody)方法完成上传,其中ContentBody的实现类是InputStreamBody。支持一次传多个文件。

HttpPost uploadPost = new HttpPost(uri);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart(TENANT_ID, new StringBody(tenantId, ContentType.TEXT_PLAIN)).addPart("path", new StringBody(path, ContentType.TEXT_PLAIN));
for (int i = 0; i < files.length; i++) {InputStreamBody inputStreamBody = new InputStreamBody(inputs[i], ContentType.APPLICATION_OCTET_STREAM, fileNames[i]);builder.addPart("file", inputStreamBody);
}
HttpEntity multipartEntity = builder.build();
uploadPost.setEntity(multipartEntity);
CloseableHttpResponse response = httpClient.execute(uploadPost)
使用addBinaryBody上传

使用addBinaryBody(String name, InputStream stream, ContentType contentType, String filename)方法

HttpPost uploadPost = new HttpPost(uri);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart(TENANT_ID, new StringBody(tenantId, ContentType.TEXT_PLAIN)).addPart("path", new StringBody(path, ContentType.TEXT_PLAIN)).addBinaryBody("file", input, ContentType.APPLICATION_OCTET_STREAM, fileName);
HttpEntity multipartEntity = builder.build();
uploadPost.setEntity(multipartEntity);
CloseableHttpResponse response = httpClient.execute(uploadPost)

使用MultipartEntityBuilder实现文件上传相关推荐

  1. 转 使用 HttpClient 4 进行文件上传

    http://www.tuicool.com/articles/Y7reYb 1. 概述 本教程我们将描述如何使用 HttpClient 4进行一次多文件上传操作 . 我们将使用  http://ec ...

  2. httpclient base64 文件上传_选择HttpClient还是OkHttp?

    来源:https://urlify.cn/IBbMRn 写在前面 为什么会写这篇文章,起因于和朋友的聊天 这又触及到我的知识盲区了,首先来一波面向百度学习,直接根据关键字httpclient和okht ...

  3. java http 上传文件_java利用httpClient实现后台文件上传请求

    之前写过基于html和js的文件上传方法java 用springMVC 和HttpServletRequest 两种实现文件上传的方法和httpClient后台执行普通post请求的文章java通过h ...

  4. 使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传

    httpclient4.3.6 下进行的测试 package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOExcep ...

  5. Spring MVC + Java 多文件上传及多文件中转上传

    1.html内容 <div><form method="post" action="/Cyberspace/main/informationBatchA ...

  6. Android 文件上传的几种方式

    这两天和新来的同事做一个项目,用到了文件上传,在这里记录下历程 Android文件上传有各种各样的Http框架,我也就简单说说我常用的吧,LiteHttp,OkHttp的封装还没做完全,有时间会补上, ...

  7. ufserver文件上传服务器-用来管理你项目中的图片-性能可比Java、Python高十倍不止

    大家在做项目的时候如果是web项目,项目又不大的情况下上传图片一般都是上传到项目的webapp目录下面,webapp下面会有一个upload的文件夹,今天给大家分享的是在项目中如何把图片上传到其他服务 ...

  8. multipart/form-data与httpclient文件上传

    写在前面:本文讨论的内容都是基于java相关技术栈. 文件上传无论是在传统的基于html的web系统开发,还是目前主流的移动app开发,都是一个比较常见的功能需求.例如:web oa系统,可能会涉及到 ...

  9. springboot设置文件上传大小(tomcat默认1M)

    application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...

最新文章

  1. 并发编程的那些事。(二)
  2. 01 Python基础学习
  3. 将一个c 语言源程序文件中所有注释去掉后,存入另一个文件.,C实验内容.doc
  4. 51单片机300个proteus仿真实例下载
  5. cmd命令查询电脑序列号_如何查看台式电脑序列号
  6. linux ps命令详细解读
  7. Excel2013 单元格锁定
  8. 你的企业如何才能留住员工?
  9. 没有文件夹则创建文件夹
  10. Prefix Sum —— 树状数组+懵逼的组合恒等式
  11. 有舍有得,解散20个群后 ...
  12. 动态inventory
  13. 数据载入、存储及文件格式(数据分析)
  14. MAC 在线安装系统
  15. preg_replace_callback函数的使用
  16. 浅谈短视频背后的社会道德伦理问题(个人观点,仅供参考)
  17. 【Arduino】坐姿检测器
  18. compare()方法+使用compare方法
  19. 植被覆盖度(FVC)计算
  20. 【软件测试】智能电视应用测试要求1

热门文章

  1. Win8,打败你的不是天真是' 无邪‘
  2. WeCode在线少儿编程 | 用Python编写地球公转运动程序,月亮地球太阳全部都在!
  3. ESA Sentinel-2 视角下的 US Aircraft Carrier
  4. axios二次封装之get请求
  5. 2021年中国服装类零售逐渐恢复,服装电商行业及相关企业数量减少,获B轮投融资1亿元[图]
  6. 诺基亚n1 android 6.0,纳尼?Android 4.0?诺基亚N1手机?
  7. MYSQL实现2退3进7退8进
  8. 计算机loopback地址怎么查,loopback地址_锐捷设备常用配置命令_loopback 0是什么意思...
  9. 储能逆变器配套智能电表
  10. 计算机网络工程教程:基于cisco路由器和交换机,高等院校计算机技术与应用系列规划教材·计算机网络工程教程:基于cisco路由器和交换机...