使用 HttpClient POST 提交表单并上传 [英] Use HttpClient POST to submit form with upload
问题描述
我有一个看起来像这样的 html 表单:
<div class="字段><input id="product_picture" name="product[picture]" size="30" type="file"/>
我想编写一个自动创建产品的 Java 模块.这是我已经拥有的:
HttpHost host = new HttpHost("localhost", 3000, "http");HttpPost httpPost = new HttpPost("/products");列表data = new ArrayList();data.add(new BasicNameValuePair("product[name]", "Product1"));UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, "UTF-8");httpPost.setEntity(实体);HttpResponse postResponse = httpClient.execute(host, httpPost);
这很好用,它能够创建名为Product1"的新产品.但我不知道如何处理上传部分.我想要这样的东西:
data.add(new BasicNameValuePair("product[name]", "Product1"));
但它不是Product1",而是一个文件.我阅读了 HttpClient 的文档,据说只有字符串.
有谁知道如何处理上传部分?
依赖项:
<依赖><groupid>org.apache.httpcomponents</groupid><artifactid>httpclient</artifactid><version>4.0.1</version></依赖><依赖><groupid>org.apache.httpcomponents</groupid><artifactid>httpmime</artifactid><version>4.0.1</version></依赖>
代码:[棘手的部分是使用 MultipartEntity ]
HttpClient client = new DefaultHttpClient();client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);HttpPost post = new HttpPost( url );MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );//对于文件参数entity.addPart( paramName, new FileBody((( File ) paramValue ), "application/zip" ));//对于通常的字符串参数entity.addPart( paramName, new StringBody( paramValue.toString(), "text/plain", Charset.forName("UTF-8")));post.setEntity( entity );//开始了!String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );client.getConnectionManager().shutdown();
I have an html form that looks something like this:
<div class="field>
<input id="product_name" name="product[name]" size="30" type="text"/>
</div>
<div class="field>
<input id="product_picture" name="product[picture]" size="30" type="file"/>
</div>
I want to write a Java module that automates the creation of product. Here is what I already have:
HttpHost host = new HttpHost("localhost", 3000, "http");
HttpPost httpPost = new HttpPost("/products");
List<BasicNameValuePair> data = new ArrayList<BasicNameValuePair>();
data.add(new BasicNameValuePair("product[name]", "Product1"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, "UTF-8");
httpPost.setEntity(entity);
HttpResponse postResponse = httpClient.execute(host, httpPost);
This works fine, it is able to create new product with name "Product1". But I don't know how to handle the upload part. I would like something looks like this:
data.add(new BasicNameValuePair("product[name]", "Product1"));
but instead of "Product1" it is a File. I read the documentation of HttpClient it's said that there's only string.
Does anyone know how to handle the uploading part ?
Dependencies:
<dependency>
<groupid>org.apache.httpcomponents</groupid>
<artifactid>httpclient</artifactid>
<version>4.0.1</version>
</dependency>
<dependency>
<groupid>org.apache.httpcomponents</groupid>
<artifactid>httpmime</artifactid>
<version>4.0.1</version>
</dependency>
Code:[Tricky part is use of MultipartEntity ]
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost( url );
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
// For File parameters
entity.addPart( paramName, new FileBody((( File ) paramValue ), "application/zip" ));
// For usual String parameters
entity.addPart( paramName, new StringBody( paramValue.toString(), "text/plain", Charset.forName( "UTF-8" )));
post.setEntity( entity );
// Here we go!
String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );
client.getConnectionManager().shutdown();
这篇关于使用 HttpClient POST 提交表单并上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!