POST发送form-data格式请求多文件传递,接收实现,中文乱码以及文件名乱码解决
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections.MapUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HttpUtil {
private final static ContentType CONTENT_TYPE = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
private HttpUtil() {
}
public static String postFormData(String url, Map<String, Object> params, File file) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
RequestConfig requestConfig = RequestConfig.custom()
// 默认连接超时
.setConnectTimeout(6 * 10000)
.setConnectionRequestTimeout(6 * 10000)
// 请求超时
.setSocketTimeout(6 * 10000)
.build();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(StandardCharsets.UTF_8);
//设置模式,防止传输附件到第三方系统文件名乱码
multipartEntityBuilder.setMode(HttpMultipartMode.RFC6532);
CloseableHttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
if (file == null) {
throw new RuntimeException("文件不能为空");
}
multipartEntityBuilder.addBinaryBody("file", new FileInputStream(file),ContentType.DEFAULT_BINARY,file.getName());
if (MapUtils.isNotEmpty(params)) {
Iterator<String> iterator = params.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = params.get(key);
StringBody stringBody = new StringBody(objIsEmpty(value), CONTENT_TYPE);
multipartEntityBuilder.addPart(key, stringBody);
}
}
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
httpResponse = httpClient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
StringBuffer buffer = new StringBuffer();
String str;
while (!StringUtils.isEmpty(str = reader.readLine())) {
buffer.append(str);
}
httpClient.close();
if (httpResponse != null) {
httpResponse.close();
}
return buffer.toString();
}
public static String postFormData(String url, Map<String, Object> params, List<File> files) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
RequestConfig requestConfig = RequestConfig.custom()
// 默认连接超时
.setConnectTimeout(6 * 10000)
.setConnectionRequestTimeout(6 * 10000)
// 请求超时
.setSocketTimeout(6 * 10000)
.build();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(StandardCharsets.UTF_8);
//设置模式,防止传输附件到第三方系统文件名乱码
multipartEntityBuilder.setMode(HttpMultipartMode.RFC6532);
CloseableHttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
if (CollectionUtil.isEmpty(files)) {
throw new RuntimeException("文件不能为空");
}
for (File file : files) {
multipartEntityBuilder.addBinaryBody("files", new FileInputStream(file),ContentType.DEFAULT_BINARY,file.getName());
}
if (MapUtils.isNotEmpty(params)) {
Iterator<String> iterator = params.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = params.get(key);
StringBody stringBody = new StringBody(objIsEmpty(value), CONTENT_TYPE);
multipartEntityBuilder.addPart(key, stringBody );
}
}
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
httpResponse = httpClient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
StringBuffer buffer = new StringBuffer();
String str;
while (!StringUtils.isEmpty(str = reader.readLine())) {
buffer.append(str);
}
httpClient.close();
if (httpResponse != null) {
httpResponse.close();
}
return buffer.toString();
}
}