发送POST请求,包含文件MultipartFile参数,普通字符串参数,请求头参数
请求入下图
MultipartEntityBuilder pom文件
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
/**
*
**/
public ResultInfo fileUpLoad(MultipartFile multipartFile, String uId) throws Exception{
ResultInfo resultInfo = new ResultInfo();
CloseableHttpClient httpClient = HttpClients.createDefault();
//请求头
Map<String, String> headers = new HashMap<>();
headers = StoreUtils.genaratTIFHeader(appKey,paastoken);
Map<String, String> otherParams = new HashMap<>();
otherParams.put("uid",uId);
String result = "";
try {
HttpPost httpPost = new HttpPost(uploadFileUrl);
//设置请求头
if (headers != null && headers.size() > 0) {
for (Map.Entry<String, String> e : headers.entrySet()) {
String value = e.getValue();
String key = e.getKey();
if (StringUtils.isNotBlank(value)) {
httpPost.setHeader(key, value);
}
}
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("utf-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//加上此行代码解决返回中文乱码问题
// 文件传输http请求头(multipart/form-data)
String fileName = multipartFile.getOriginalFilename();
//builder.addBinaryBody("file", multipartFile.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);
builder.addBinaryBody("file", multipartFile.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);
ContentType contentType = ContentType.create("multipart/form-data", Charset.forName("UTF-8"));
builder.addTextBody("uid",uId, contentType);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
Map map = JSON.parseObject(result, Map.class);
Integer errorCode = (Integer) map.get("state");
if(errorCode.equals(1)){
Object data = map.get("row");
JSONObject json = JSONObject.parseObject(data.toString());
Disk disk = new Disk();
if(null != json){
disk.setDocid(json.getString("docid"));
buttStoreDAO.insertDisk(disk);
}
resultInfo.setResultCode(ResultCode.SUCCESS);
return resultInfo;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultInfo;
}
多个附件
/**
* 使用httpclint 发送文件,如果不传输文件,直接设置fileParams=null,
* 如果不设置请求头参数,直接设置headerParams=null,就可以进行普通参数的POST请求了
*
* @param url 请求路径
* @param fileParams 文件参数
* @param otherParams 其他字符串参数
* @param headerParams 请求头参数
* @return
*/
public static String uploadFile(String url, Map<String, MultipartFile> fileParams, Map<String, String> otherParams, Map<String, String> headerParams) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
HttpPost httpPost = new HttpPost(url);
//设置请求头
if (headerParams != null && headerParams.size() > 0) {
for (Map.Entry<String, String> e : headerParams.entrySet()) {
String value = e.getValue();
String key = e.getKey();
if (StringUtils.isNotBlank(value)) {
httpPost.setHeader(key, value);
}
}
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("utf-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//加上此行代码解决返回中文乱码问题
// 文件传输http请求头(multipart/form-data)
if (fileParams != null && fileParams.size() > 0) {
for (Map.Entry<String, MultipartFile> e : fileParams.entrySet()) {
String fileParamName = e.getKey();
MultipartFile file = e.getValue();
if (file != null) {
String fileName = file.getOriginalFilename();
builder.addBinaryBody(fileParamName, file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
}
}
}
// 字节传输http请求头(application/json)
ContentType contentType = ContentType.create("application/json", Charset.forName("UTF-8"));
if (otherParams != null && otherParams.size() > 0) {
for (Map.Entry<String, String> e : otherParams.entrySet()) {
String value = e.getValue();
if (StringUtils.isNotBlank(value)) {
builder.addTextBody(e.getKey(), value, contentType);// 类似浏览器表单提交,对应input的name和value
}
}
}
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}