httpclient多文件上传

  1. 依赖坐标
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.1</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpmime</artifactId>
	<version>4.3.1</version>
</dependency>
  1. 完整代码
package http;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
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.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	private static final String POST_NOT_OK = "Post Request For Url[%s] is not ok. Response is null";
	private static final String POST_ERROR_CODE = "Post Request For Url[%s] is not ok. Response Status Code is %s";
	private static final int CONNECT_TIMEOUT = 10000;
	private static final int CONNECTION_REQUEST_TIMEOUT = 3000;
	private static final int SOCKET_TIMEOUT = 20000;

	public static String postRequest(String url, Map<String, String> headers, Map<String, String> params,
			Map<String, List<File>> files) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		httpPost.setConfig(RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
				.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build());
		if (headers != null) {
			for (String key : headers.keySet()) {
				httpPost.setHeader(key, headers.get(key));
			}
		}

		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		if (files != null) {
			for (String key : files.keySet()) {
				for (File file : files.get(key)) {
					builder.addPart(key, new FileBody(file));
				}
			}
		}
		if (params != null) {
			for (String key : params.keySet()) {
				builder.addTextBody(key, params.get(key));
			}
		}
		httpPost.setEntity(builder.build());

		HttpResponse response = HttpClientBuilder.create().build().execute(httpPost);
		if (response == null || response.getStatusLine() == null) {
			throw new RuntimeException(String.format(POST_NOT_OK, url));
		} else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
			throw new RuntimeException(String.format(POST_ERROR_CODE, url, response.getStatusLine().getStatusCode()));
		}
		return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.name());
	}
}

mark,后续再整理。。。

public static String getPostMethod(String url, Map<String, Object> map) throws Exception {
		HttpClient httpClient = HttpClientBuilder.create().build();
		httpClient = new DefaultSSLUtils();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
		httpPost.setHeader("Accept", "application/json");
		httpPost.setEntity(new StringEntity(JSON.toJSONString(map), StandardCharsets.UTF_8));
		HttpResponse response = httpClient.execute(httpPost);
		if (ObjectUtils.isNull(response) || ObjectUtils.isNull(response.getStatusLine())) {
			throw new Exception("Post Request For Url[{}] is not ok. Response is null");
		} else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
			logger.info("异常状态码 :" + response.getStatusLine().getStatusCode());
			throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {}");
		}
		return EntityUtils.toString(response.getEntity());
	}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值