使用 HttpClient 进行文件上传

Posted xiaomifeng0510

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 HttpClient 进行文件上传相关的知识,希望对你有一定的参考价值。

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 进行文件上传的主要内容,如果未能解决你的问题,请参考以下文章

Java 中使用 HttpClient 4.3.6 进行文件上传

使用 HttpClient 进行文件上传

httpclient 实现文件上传中转

Xamarin.Forms 使用HttpClient上传文件

C#:HttpClient,将多个文件作为MultipartFormDataContent上传时的文件上传进度

Titanium HTTPCLIENT 无法上传文件