httpClient 使用http协议上传文件
程序员文章站
2022-06-21 21:14:56
...
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<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>
package com.gjp;
import com.gjp.util.PerformanceReport;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
/**
* @Auther: gaojp
* @Date: 2018/10/9 14:40
* @Description: http 通讯工具
*/
public class HttpClientReport {
private static Logger log = LoggerFactory.getLogger(HttpClientReport.class);
public void upload2(final String url, final Map<String, String> params) throws ClientProtocolException, IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse httpResponse = null;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000).build();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
File file = new File(params.get("pdf"));
multipartEntityBuilder.addBinaryBody("pdf", file);
//multipartEntityBuilder.addBinaryBody("file", file,ContentType.create("image/png"),"abc.pdf");
//当设置了setSocketTimeout参数后,以下代码上传PDF不能成功,将setSocketTimeout参数去掉后此可以上传成功。上传图片则没有个限制
//multipartEntityBuilder.addBinaryBody("file",file,ContentType.create("application/octet-stream"),"abd.pdf");
// multipartEntityBuilder.addTextBody("comment", "this is comment");
if (null != params && params.size() > 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
multipartEntityBuilder.addTextBody(entry.getKey(), entry.getValue(), ContentType.create("text/plain", Charset.forName("UTF-8")));
}
}
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
httpResponse = httpClient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
StringBuffer buffer = new StringBuffer();
String str = "";
while (!StringUtils.isEmpty(str = reader.readLine())) {
buffer.append(str);
}
System.out.println("返回:" + buffer.toString());
}
httpClient.close();
if (httpResponse != null) {
httpResponse.close();
}
}
public static void main(String[] args) {
HttpClientReport report = new HttpClientReport();
String url = "http://localhost:8080/cgjy/recPdfController.do?method=getSmallPlatPdf";
//uniteId applyCode beginTime endTime recordTime companyName nos
Map<String, String> param = new HashMap<String, String>();
//文件key 和文件路径
param.put("pdf", "d:\\123.pdf");
param.put("uniteId", "111");
param.put("applyCode", "12");
param.put("beginTime", "20180930");
param.put("endTime", "20180930");
param.put("recordTime", "20181030212230");
param.put("companyName", "测试信息内容499004");
param.put("nos", "2346789");
//格式:uniteId+";"+applyCode+";"+companyName
StringBuffer content = new StringBuffer(50);
content.append("111").append(";").append("12").append(";");
content.append("测试信息内容499004");
System.out.println(content.toString());
String sign = PerformanceReport.aesEncrypt(content.toString());
System.out.println("sign:" + sign);
param.put("sign", sign);
try {
report.upload2(url, param);
} catch (Exception e) {
e.printStackTrace();
}
}
}