HttpClient文件上传
来源:互联网 发布:重大误解知乎 编辑:程序博客网 时间:2024/06/06 00:35
HttpComponents是一个HTTP工具集,其中HttpClient包括了HTTP客户端的API,可以充当浏览器。
用HttpClient API 实现文件上传的代码网上有很多,但是都更新不及时,版本较低。
以下代码的HttpClient版本是4.3.1:
CloseableHttpClient httpclient = HttpClientBuilder.create().build(); CloseableHttpResponse response = null; try { HttpPost httppost = new HttpPost(UPLOAD_URL); HttpEntity req = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addPart("files", new FileBody(new File(FILE1_PATH))) .addPart("files", new FileBody(new File(FILE2_PATH))) .build(); httppost.setEntity(req); System.out.println("executing request: " + httppost.getRequestLine()); response = httpclient.execute(httppost); HttpEntity re = response.getEntity(); System.out.println(response.getStatusLine()); if (re != null) { System.out.println("Response content: " + new BufferedReader(new InputStreamReader(re.getContent())).readLine()); } EntityUtils.consume(re); } finally { try { response.close(); } catch (Exception e) { e.printStackTrace(); } }
代码的依赖包:
<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><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
- httpclient 上传文件
- httpclient上传文件
- httpclient 上传文件
- httpclient 上传文件
- HttpClient 3.1 文件上传
- HttpClient 4 - 文件上传
- common-httpclient上传文件
- android httpclient 上传文件
- Httpclient上传文件
- 使用httpclient上传文件
- HttpClient 4 - 文件上传
- httpclient 上传文件
- HttpClient上传文件
- HttpClient文件上传
- httpclient上传文件
- HttpClient 4 - 文件上传
- HttpClient 上传文件
- HttpClient 文件上传
- 层次语句【未看完全】
- EJB揭秘
- test
- 分布式中 CAP BASE ACID 理解
- 轻量级邮件客户端Geary官方ppa安装
- HttpClient文件上传
- Socket层实现系列 — accept()的实现(一)
- postgresql 字段sql语句
- hive函数参考手册
- 不只是技术!成为IT经理必备的十大软技能
- C语言 - 【随机数种子函数的使用】 和 【任意数求 百位 和 个位 的数】
- java Socket完美实例
- 中文编码杂谈
- Ubuntu中安装Wine