httpclient4.3 模拟表单提交文件
来源:互联网 发布:数据库检索系统 编辑:程序博客网 时间:2024/05/20 23:04
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.6</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.3.6</version></dependency>
package com.test;import java.io.File;import java.io.IOException;import java.nio.charset.Charset;import org.apache.http.Consts;import org.apache.http.HttpEntity;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.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils; public class ClientMultipartFormPost { /** * 这个例子展示了如何执行请求包含一个多部分编码的实体 * 模拟表单提交 * @throws IOException */ public static void main(String[] args) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); try{ //要上传的文件的路径 String filePath = "/Users/jjs/Downloads/feedbak_image.jpg"; //把一个普通参数和文件上传给下面这个地址 是一个servlet HttpPost httpPost = new HttpPost("http://localhost:8080/feedback/add"); //把文件转换成流对象FileBody FileBody bin = new FileBody(new File(filePath)); //普通字段 重新设置了编码方式 StringBody content = new StringBody("这里是一个评论", ContentType.create("text/plain", Consts.UTF_8)); //StringBody comment = new StringBody("这里是一个评论", ContentType.TEXT_PLAIN); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("media", bin)//相当于<input type="file" name="media"/> .addPart("content", content) .build(); httpPost.setEntity(reqEntity); System.out.println("发起请求的页面地址 " + httpPost.getRequestLine()); //发起请求 并返回请求的响应 CloseableHttpResponse response = httpClient.execute(httpPost); try { System.out.println("----------------------------------------"); //打印响应状态 System.out.println(response.getStatusLine()); //获取响应对象 HttpEntity resEntity = response.getEntity(); if (resEntity != null) { //打印响应长度 System.out.println("Response content length: " + resEntity.getContentLength()); //打印响应内容 System.out.println(EntityUtils.toString(resEntity,Charset.forName("UTF-8"))); } //销毁 EntityUtils.consume(resEntity); } finally { response.close(); } }finally{ httpClient.close(); } } }
0 0
- httpclient4.3 模拟表单提交文件
- HttpClient4模拟表单提交
- HttpClient4模拟带文件上传的表单提交
- HttpClient4.5.2模拟表单post提交上传文件
- 使用HttpClient4提交包含上传文件的表单
- 模拟表单提交文件和变量
- java 模拟表单方式提交上传文件
- java 模拟表单方式提交上传文件
- java模拟form表单提交图片文件
- httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据
- curl 模拟表单提交文件 跨域跨服务器上传文件
- PHP CURL模拟表单post提交文件,curl POST文件
- HttpClient 4.1.3模拟表单提交示例
- PHP模拟表单提交
- VC++ 模拟表单提交
- httpClient 模拟表单提交
- 模拟post表单提交
- 模拟表单提交
- 类图详解
- QQ群无法上传文件,离线传送文件都可以的解决办法
- IndentationError: unexpected indent python
- HTML5学习(五)—2
- 序列化与反序列号
- httpclient4.3 模拟表单提交文件
- Centos6.5 64位 安装Hadoop2.7.0, MapReduce日志分析, Hive2.1.0, JDBC连接Hive查询 (2)
- ipnc rdk 385板子修改ip地址
- leetcode-java-59. Spiral Matrix II
- 使用jconsole远程监控weblogic
- 分享到QQ空间、微博等的代码
- M3U , PLSv2 and ASX Play List Specification
- 寻找和为定值的两个数
- CAP理论