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. Javascript基础语法(二)

    三.运算符 1. 算术运算符  + - * / % ++ -- 1.1赋值运算符 = += . -= .*=. /= 1 +=2;  ==>   1 = 1 + 2; 2. 比较运算符 > ...

  2. CentOS7──xxx is not in the sudoers file

    提示"xxx is not in the sudoers file. This incident will be reported.其中 ”XXX“是你的用户名,也就是你的用户名没有权限使用 ...

  3. 在sparkStreaming实时存储时的问题

    1.实时插入mysql时遇到的问题,使用的updateStaeBykey有状态的算子 必须设置checkpoint  如果报错直接删掉checkpoint 在创建的时候自己保存偏移量即可 再次启动时读 ...

  4. Linux中LAMP构架的实现

    LAMP:Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度共同组 ...

  5. C++隐藏任务栏图标

    在VC编程中,有时候我们需要将我们的程序在任务栏上的显示隐藏起来,我试过几种方法,下面我介绍一下我知道的三种方法. 第一种方法是设置窗口WS_EX_TOOLWINDOW扩展样式,通过在OnInitDi ...

  6. Pytrhon结束死循环的子线程

    Python在子线程无线循环的过程中,如果直接ctrl+c结束程序的话,虽然程序可以结束,但是会导致子线程资源无法回收,一般情况不会有太大影响,但是使用TCP通信的时候,子线程是占用特定的端口的,在资 ...

  7. flutter 读取sdcard权限问题相关

    https://stackoverflow.com/questions/46698751/permission-denied-at-externalstoragedirectory-access-vi ...

  8. How to Make a Computer Operating System

    How to Make a Computer Operating System 如何制作一个操作系统(翻译版) 原文地址:Github:How to Make a Computer Operating ...

  9. IDEA修改显示星号*和热部署

    IDEA修改显示*星号: IDEA热部署: 两步: 1. 2. Ctrl+Alt+Shift+/ 打开下面界面,选Registry 对于Springboot应用,可能无法启动,在上述两步不起作用的情况 ...

  10. Bigger-Mai 养成计划,Python基础巩固三

    1.集合的基本操作(set)集合是一个无序的,不重复的元素集合,他的主要作用是去重:把一个列表变为集合就自动去重了.关系测试:测试两组数据之间的交集,差集,并集等关系 list_1 = [1,4,6, ...