package com;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
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 java.io.*;
import java.nio.charset.Charset;
/**
* @Author HanKeQi
* @Date 2020/4/21 9:13 下午
* @Version 1.0
**/
public class HttpClientTest {
public static void main(String[] args) {
for (int i = 0; i < 100 ; i++) {
Run run = new Run();
Thread thread = new Thread(run);
thread.start();
}
}
}
class Run implements Runnable{
@Override
public void run() {
try {
Run run = new Run(); run.start("http://127.0.0.1/proposal//insure/upload/imageToBase64Steam");
}catch (Exception e){
e.printStackTrace();
}
}
public String start(String path) throws Exception {
String filePath = "/xxxxxxxxxx/src/main/java/com/1587475473226554.video_thumb.jpg";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentType contentType = ContentType.create("application/json", Charset.forName("utf-8"));
builder.addTextBody("open", "1", contentType);
builder.addTextBody("colourways", "24", contentType);
ContentType contentType2 = ContentType.create("multipart/form-data", Charset.forName("utf-8"));
InputStream inputStream = new FileInputStream(new File(filePath));
builder.addBinaryBody("file", inputStream, contentType2, filePath);
HttpEntity entity = builder.build();
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(entity);
httpPost.addHeader("UTOKEN", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmYWlyeWxhbmQiLCJleHAiOjE1ODgwODEzOTE3MDYsInR5cGUiOiJvdXRzaWRlIiwidXNlcklkIjoiMTI0OTQ5NjQ4Mzc0NzEzNTQ4OSIsImlhdCI6MTU4NzQ3NjU5MTcwNn0.EBqIx8WVsoFHz94bF2fy9T2R1nichhHCO8_GQ0Coa6Q");
// httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
System.out.println("创建post请求并装载好打包数据");
// 4. 创建HttpClient对象,传入httpPost执行发送网络请求的动作
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println("发送post请求并获取结果");
// 5. 获取返回的实体内容对象并解析内容
HttpEntity resultEntity = response.getEntity();
String responseMessage = "";
try{
System.out.println("开始解析结果");
if(resultEntity!=null){
InputStream is = resultEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = br.readLine()) != null){
sb.append(line);
}
responseMessage = sb.toString();
System.out.println("解析完成,解析内容为"+ responseMessage);
}
EntityUtils.consume(resultEntity);
}finally{
if (null != response){
response.close();
}
}
return responseMessage;
}
// public String start(String path) throws Exception {
// // 1. 创建上传需要的元素类型
// // 1.1 装载本地上传图片的文件
//
// // 1.3 装载上传字符串的对象
// StringBody name = new StringBody("", ContentType.APPLICATION_JSON);
//
// JSONObject jsonParam = new JSONObject();
// jsonParam.put("fileName", "face_best_23071019920806022783cfae8c45ad49a6a5f30177c9994bda.png");
// StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
// entity.setContentEncoding("UTF-8");
// entity.setContentType("application/json");
//
// // 3. 创建HttpPost对象,用于包含信息发送post消息
// HttpPost httpPost = new HttpPost(path);
// httpPost.setEntity(entity);
// httpPost.addHeader("UTOKEN", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmYWlyeWxhbmQiLCJleHAiOjE1ODgwODEzOTE3MDYsInR5cGUiOiJvdXRzaWRlIiwidXNlcklkIjoiMTI0OTQ5NjQ4Mzc0NzEzNTQ4OSIsImlhdCI6MTU4NzQ3NjU5MTcwNn0.EBqIx8WVsoFHz94bF2fy9T2R1nichhHCO8_GQ0Coa6Q");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
//
// System.out.println("创建post请求并装载好打包数据");
// // 4. 创建HttpClient对象,传入httpPost执行发送网络请求的动作
// CloseableHttpClient httpClient = HttpClients.createDefault();
// CloseableHttpResponse response = httpClient.execute(httpPost);
// System.out.println("发送post请求并获取结果");
// // 5. 获取返回的实体内容对象并解析内容
// HttpEntity resultEntity = response.getEntity();
// String responseMessage = "";
// try{
// System.out.println("开始解析结果");
// if(resultEntity!=null){
// InputStream is = resultEntity.getContent();
// BufferedReader br = new BufferedReader(new InputStreamReader(is));
// StringBuffer sb = new StringBuffer();
// String line = "";
// while((line = br.readLine()) != null){
// sb.append(line);
// }
// responseMessage = sb.toString();
// System.out.println("解析完成,解析内容为"+ responseMessage);
// }
// EntityUtils.consume(resultEntity);
// }finally{
// if (null != response){
// response.close();
// }
// }
// return responseMessage;
// }
}
HttpClient 复杂参数上传图片
最新推荐文章于 2021-07-21 15:21:44 发布