转自Java HttpClient 发送multipart form-data的Post请求
public static String gateway(String param,String paramvalue,String servicename,String interface_id) throws Exception{标签:Java,postman,FORM,builder,addTextBody,传输数据,MULTIPART,DATA,String From: https://www.cnblogs.com/sumling/p/17144804.html
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String url = URL + "jpaas-jags-server/interface/gateway";
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
String respContent = null;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
Date d= new Date();
String timestamp = String.valueOf(d.getTime());
String time = sdf.format(d);
JSONObject biz_contentJson = new JSONObject();
JSONObject paramsJson = new JSONObject();
paramsJson.put(param,paramvalue);
biz_contentJson.put("appmark",APPMARK);
biz_contentJson.put("time",time);
biz_contentJson.put("sign",encodeMd5(APPMARK+APPWORD+time));
biz_contentJson.put("servicename",servicename);
biz_contentJson.put("params",paramsJson);
builder.addTextBody("app_id",APP_ID,ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("interface_id",interface_id,ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("version","1.0",ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("header","{}",ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("charset","UTF-8",ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("timestamp",timestamp,ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("origin","0",ContentType.MULTIPART_FORM_DATA);
builder.addTextBody("biz_content",biz_contentJson.toString(),ContentType.MULTIPART_FORM_DATA);
String sign = "";
sign = getSign(builder);
builder.addTextBody("sign",sign,ContentType.MULTIPART_FORM_DATA);
HttpEntity multipart = builder.build();
HttpResponse resp = null;
httpPost.setEntity(multipart);
resp = client.execute(httpPost);
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he, "UTF-8");
System.out.println(respContent);
return respContent;
}