httpClient 使用http协议上传文件
程序员文章站
2022-06-21 21:07:50
...
<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();
}
}
}
推荐阅读
-
php使用ereg验证文件上传的方法,_PHP教程
-
请求后台配置项http错误,上传功能将不能正常使用!php版本
-
Yii2使用自带的UploadedFile实现的文件上传
-
Python使用百度API上传文件到百度网盘代码分享
-
使用perl上传文件到S3,并设置http的header
-
Spring MVC的文件上传和下载以及拦截器的使用实例
-
阿里云文件存储SMB协议服务及其申请和使用指南 虚拟机浏览器windowsdosunix
-
使用Application Loader 3.0应用文件 上传步骤
-
使用Application Loader 3.0应用文件 上传步骤
-
Java 发送http请求上传文件功能实例