1.使用 AddPart 方法

public static void upload(String authorization,String baseUrl,String filePath,String userId,String isOverWrite,String remotePath){

   CloseableHttpClient client = HttpClients.createDefault();
String uploadFile_url = Utils.getValue("uploadFile.url");
String url=baseUrl+uploadFile_url; HttpPost post = new HttpPost(url);
System.out.println("---->upload_url:"+url); //设置请求头
HashMap<String, String> header = new HashMap<>();
header.put("Authorization", authorization);//Basic YWRtaW4xMjM6MTIzcXdl
Iterator<String> iterator_header = header.keySet().iterator();
while(iterator_header.hasNext()){
String key = iterator_header.next();
post.addHeader(key,header.get(key));
} //构造待上传数据,加入builder
File file=new File(filePath);
String fimeName = file.getName();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.RFC6532);
builder.setCharset(Charset.forName("UTF-8"));
builder.addPart("userId", new StringBody(userId, ContentType.MULTIPART_FORM_DATA));
builder.addPart("remotePath", new StringBody(remotePath, ContentType.MULTIPART_FORM_DATA));
builder.addPart("isOverWrite", new StringBody(isOverWrite, ContentType.MULTIPART_FORM_DATA));
builder.addPart("isSendEmail", new StringBody(String.valueOf(false), ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowChunkNumber", new StringBody("1", ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowChunkSize", new StringBody("104857600", ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowCurrentChunkSize", new StringBody("90058", ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowTotalSize", new StringBody("90058", ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowIdentifier", new StringBody("90058-2pdf", ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowFilename", new StringBody(fimeName, ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowRelativePath", new StringBody(fimeName, ContentType.MULTIPART_FORM_DATA));
builder.addPart("flowTotalChunks", new StringBody("1", ContentType.MULTIPART_FORM_DATA));
builder.addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY));
HttpEntity entity = builder.build(); post.setEntity(entity);
HttpResponse response = null;
try {
response = client.execute(post);
System.out.println("---->reponse:"+response);
entity = response.getEntity();
JSONObject result = JSONObject.parseObject(EntityUtils.toString(entity));
} catch (IOException e) {
e.printStackTrace();
}
} 2.使用 addTextBody 方法
  public static void uploadInterface(String authorization,String baseUrl,String filePath,String userId,String isOverWrite,String remotePath){

      CloseableHttpClient client = HttpClients.createDefault();
    String uploadFile_url = Utils.getValue("uploadFile.url");
    String url=baseUrl+uploadFile_url;
HttpPost httpPost=new HttpPost(url);//通过post传递     //heard处理
HashMap<String, String> header = new HashMap<>();
    header.put("Authorization", authorization);//
Iterator<String> iterator_header = header.keySet().iterator();
while(iterator_header.hasNext()){
String key = iterator_header.next();
httpPost.addHeader(key,header.get(key));
}
File file=new File(filePath);
String fileName=file.getName();
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();   builder.addTextBody("userId", userId,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("remotePath", "/",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("isOverWrite", "-1",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("isSendEmail", String.valueOf(false),ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowChunkNumber", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowChunkSize", "104857600",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowCurrentChunkSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowTotalSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowIdentifier", "3493921-1jpg",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowFilename", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowRelativePath", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowTotalChunks", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"));    builder.addBinaryBody("file", file, ContentType.create("image/jpeg"), fileName);
HttpEntity entity = builder.build(); httpPost.setEntity(entity); /**发送请求*/
try {
String result="";
HttpResponse response=client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
//判断是否上传成功 返回200
if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } 3.addBinaryBody中文文件乱码解决方法

ContentType contentType = ContentType.create("text/plain",Charset.forName("UTF-8"));
HttpEntity caiJiEntity= MultipartEntityBuilder.create()
.addBinaryBody("file", new File("d://2.mp4"), ContentType.create("video/mp4"), "2.mp4")
.addBinaryBody("file1",new File("d:/1-120915094151.jpg"),

ContentType.create("image/jpg"), "1-120915094151.jpg")

.addTextBody("mtxs", "三面立柱",contentType)
.build();

使用 HttpClient 进行文件上传的更多相关文章

  1. HttpClient构造文件上传

    在项目中我们有时候需要使用到其他第三方的api,而有些api要求我们上传文件,search一下,下面将结果记录一下喽! 含义 ENCTYPE="multipart/form-data&quo ...

  2. HttpClient多文件上传代码及普通参数中文乱码问题解决

    该随笔记录了在实际项目中使用HttpClient调用外部api,需上传文件和普通参数的代码. 笔者在使用 HttpClient 调用 http api 接口时,需要服务端上传文件和一些普通参数给 ht ...

  3. Android使用HttpClient实现文件上传到PHP服务器,并监控进度条

    上传 服务器端PHP 代码如下 : <?php $target_path = "./tmp/";//接收文件目录 $target_path = $target_path.($ ...

  4. HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决

    又是折腾了一天才解决的问题,网上关于这个问题的资料不多,希望写出来能帮到有需要的人. 之前无论怎么设置charset都不起作用, 后来看了这篇文章 才发现MultipartEntityBuilder有 ...

  5. HttpClient文件上传下载

    1 HTTP HTTP 协议可能是如今 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序须要直接通过 HTTP 协议来訪问网络资源. 尽管在 JDK 的 java.net ...

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

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

  7. springMVC + hadoop + httpclient 文件上传请求直接写入hdfs

    1.首先是一个基于httpclient的java 应用程序,代码在这篇文章的开头:点击打开链接 2.我们首先写一个基于springMVC框架的简单接收请求上传的文件保存本地文件系统的demo,程序代码 ...

  8. Android开发之httpclient文件上传实现

    文件上传可能是一个比較耗时的操作,假设为上传操作带上进度提示则能够更好的提高用户体验,最后效果例如以下图: 项目源代码:http://download.csdn.net/detail/shinay/4 ...

  9. Java后台使用httpclient入门HttpPost请求(form表单提交,File文件上传和传输Json数据)

    一.HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...

随机推荐

  1. SQL- @@ROWCOUNT -返回上一行执行影响的行行数

    DECLARE @AFFECT INT declare @t table(name nvarchar(20),qy nvarchar(20),je int)insert into @t select ...

  2. php通过pecl方式安装扩展

    安装pecl cd /usr/local/php/bin/ wget http://pear.php.net/go-pear.phar -O go-pear.php php go-pear.php # ...

  3. 【论文速读】Fangfang Wang_CVPR2018_Geometry-Aware Scene Text Detection With Instance Transformation Network

    Han Hu--[ICCV2017]WordSup_Exploiting Word Annotations for Character based Text Detection 作者和代码 caffe ...

  4. java爬取免费HTTP代理 code-for-fun

    偶然看到一个提供免费HTTP 代理IP的网站,该网站一两个小时就会更新一次,很有用.之后自己就用Java写了一个爬虫,爬取网站上的代理IP,以备后用. 网站源码: <!DOCTYPE html& ...

  5. Windows下安装Redis服务

    说明:本文拷贝自https://jingyan.baidu.com/article/0f5fb099045b056d8334ea97.html Redis是有名的NoSql数据库,一般Linux都会默 ...

  6. WEUI滚动加载

    var row = 6, page = 1; var loading = false; //状态标记 $(document.body).infinite().on("infinite&quo ...

  7. c# 将CSV文件转成list集合

    //定义CSV文件所对应的实体类 public class example { public int t1; public string t2; public string t3; public st ...

  8. 51nod1268 和为K的组合(DFS)

    1268 和为K的组合  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以 ...

  9. JZ2440学习笔记之第一个裸机程序(Keil-MDK)

    CPU:S3C2440, ARM920T, Internal 4KB RAM, Support boot from NAND flash, 128MB for each bank. JZ2440:Me ...

  10. day 07

    # 数据类型的相互转化# 字符编码# 文件操作 # 1.哪些类型可以转化为数字# res = int('10')# print(res)# res = int('-3')# print(res)# r ...