HttpClient 使用 Post 方法传输文件
一、依赖
httpmime-4.5.13.jar
httpcore-4.4.15.jar
httpclient-4.5.13.jar
fastjson-1.2.15.jar
commons-logging-1.1.1.jar
commons-codec-1.15.jar
二、代码
package com;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
public class RestMock<K, V> {
private static SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
public static String doPost(String urlStr, String APIKey, String Authorization, File file) {
System.out.println("url----------------> " + urlStr);
String sResponse = "{}";
Date statTime = new Date();
System.out.println("----------请求开始时间:" + simpleFormat.format(statTime));
try {
String fileName = file.getName();
FileInputStream fileInputStream = null;
InputStream is = null;
if (null != file) {
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("------文件不存在或者文件不可读或者文件是目录--------");
}
is = fileInputStream;
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(urlStr);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(20000)
.setConnectTimeout(20000)
.build();
httpPost.setConfig(requestConfig);
httpPost.addHeader("APIKey", APIKey);
httpPost.addHeader("Authorization", Authorization);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", is, ContentType.MULTIPART_FORM_DATA, fileName);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("-----------------状态码--------------");
System.out.println("---------------------->statusCode: "+statusCode);
HttpEntity responseEntity = response.getEntity();
if (statusCode == HttpStatus.SC_OK) {
if (null != responseEntity) {
String result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
sResponse = JSONObject.parse(result).toString();
System.out.println("sResponse1:--------------->" + sResponse);
}
}
else {
if (null != responseEntity) {
String result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
sResponse = JSONObject.parse(result).toString();
System.out.println("sResponse2:--------------->" + sResponse);
}
}
Date endTime = new Date();
System.out.println("----------请求结束时间:" + simpleFormat.format(endTime));
Date totalTime = new Date(endTime.getTime() - statTime.getTime());
System.out.println("----------请求总用时:" +
simpleFormat.format(totalTime).substring(simpleFormat.format(totalTime).lastIndexOf(":") + 1)
+ " 秒");
if (null != is) {
is.close();
}
if (null != fileInputStream) {
is.close();
}
if (null != httpClient) {
httpClient.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return sResponse;
}
}