需要做一个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别忘了

最新文章

  1. jq.validate 自定义验证两个日期
  2. redis 常用命令
  3. 浅谈struts2之chain
  4. word中那些重要但是被人忽略的快捷键和长word文档的跳转
  5. OWIN规范中最让人费解的地方
  6. Apache中关于页面缓存的设置
  7. div+css3列布局,带详尽注释
  8. BeanUtils数据封装与表单JavaBean
  9. Linux查看系统信息的命令及已安装软件包的命令
  10. windows通过node环境搭建安装npm,cnpm,vue-cli
  11. Nginx 的两种认证方式
  12. MySQL——优化嵌套查询和分页查询
  13. ubuntu 下 teamview 取消自动启动 autostart
  14. 添加aimate动画
  15. Shell初学(三)传参
  16. STM32通用定时器配置
  17. Nested Loops,Hash Join 和 Sort Merge Join. 三种不同连接的不同:
  18. 软件工程android项目简介
  19. HDU - 2204 Eddy&#39;s爱好 (数论+容斥)
  20. Base64转Blob

热门文章

  1. Cocos2d-JS项目之三:使用合图
  2. lua学习项目笔记
  3. 从今天起,记录CEF使用开发心得经验
  4. MS SQL的存储过程
  5. WindowsStore页面导航
  6. 使用nodejs搭建图片服务器(一)
  7. SEO优化之Title 和 Meta 标签
  8. Android ActionBar Home按钮返回事件处理的两种方式
  9. [git]安装git-pylint-commit-hook提高python项目中的代码质量
  10. node-gyp rebuild 卡住?