论httpclient上传带参数【commons-httpclient和apache httpclient区别】
2024-08-21 22:40:51
需要做一个httpclient上传,然后啪啪啪网上找资料
1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试
PostMethod filePost = new PostMethod(url);
filePost.setParameter("system", "vinuxpost");
try {
Part part[] = UploadRequestHelper.getPart(request);
filePost.setRequestEntity(new MultipartRequestEntity(part, filePost
.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
System.out.println(status);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
其中一段主要代码,上传文件方式用的是byte方式,然后带上需要的参数,然后就遇到了一个奇怪的问题,就是参数过不来,然后去掉了上传文件的部分,参数就过来了,好奇怪,用了一下午,网上找资料还是没有解决,目前还没有解决!!!希望有人能告诉我,哈哈哈,也许可能是jar包版本的问题,我看网上好多资料都是可以带参数上传文件的。
希望大神能帮我解决上面的问题
2.然后接着apache httpclient上传,上代码
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<MultipartFile> listFile = UploadRequestHelper
.getListMultipartFile(request);
// 创建待处理的表单域内容文本
StringBody system = new StringBody("vinuxpost");
MultipartEntity reqEntity = new MultipartEntity();
for (MultipartFile file : listFile) {
ByteArrayBody bab = new ByteArrayBody(file.getBytes(),
file.getName());
reqEntity.addPart(file.getName(), bab);
}
reqEntity.addPart("system", system);
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println("状态:" + response.getStatusLine());
不出意外,成功了,可以继续下面的工作了,哈哈哈
总结,commons-httpclient和apache httpclient 的区别:
首先commons-httpclient好像很少更新了,而且文档不是很充足,网上的例子很多都是重复的,apache httpclient 一直在更新,哈哈,资料比较多!!!
贴上apache httpclient maven 嘿嘿就这了
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.1</version>
</dependency>
需要 httpmime别忘了
最新文章
- jq.validate 自定义验证两个日期
- redis 常用命令
- 浅谈struts2之chain
- word中那些重要但是被人忽略的快捷键和长word文档的跳转
- OWIN规范中最让人费解的地方
- Apache中关于页面缓存的设置
- div+css3列布局,带详尽注释
- BeanUtils数据封装与表单JavaBean
- Linux查看系统信息的命令及已安装软件包的命令
- windows通过node环境搭建安装npm,cnpm,vue-cli
- Nginx 的两种认证方式
- MySQL——优化嵌套查询和分页查询
- ubuntu 下 teamview 取消自动启动 autostart
- 添加aimate动画
- Shell初学(三)传参
- STM32通用定时器配置
- Nested Loops,Hash Join 和 Sort Merge Join. 三种不同连接的不同:
- 软件工程android项目简介
- HDU - 2204 Eddy&#39;s爱好 (数论+容斥)
- Base64转Blob