使用httpclient4上传文件
来源:互联网 发布:电子商务软件提供商 编辑:程序博客网 时间:2024/04/28 02:32
1.模拟form表单上传文件,主要是通过httpmime包中MultipartEntity对象,核心代码如下:
File file = new File(filePath);FileBody fb = new FileBody(file);MultipartEntity reqEntity = new MultipartEntity();reqEntity.addPart("myFile", fb);
2.以字节流的形式上传文件,主要通过FileEntity,核心代码如下:
File file = new File(filePath);FileEntity reqEntity = new FileEntity(file);postMethod.setEntity(reqEntity);
简单的demo程序:
package com.pengkw.client;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.FileEntity;import org.apache.http.entity.mime.MultipartEntity;import org.apache.http.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.CoreConnectionPNames;import org.apache.http.util.EntityUtils;public class Client {/** * 读超时时间 */public static final int READ_TIME_OUT = 3000;/** * 连接超时时间 */public static final int CONNECTION_TIME_OUT = 3000;private HttpClient client;public Client(){init();}public void init(){this.client = new DefaultHttpClient();this.client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIME_OUT);this.client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, READ_TIME_OUT);}public void get(String url){HttpGet getMethod = new HttpGet(url);try {HttpResponse response = client.execute(getMethod);HttpEntity entity = response.getEntity();System.out.println(response.getStatusLine().getStatusCode());System.out.println(EntityUtils.toString(entity));} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {getMethod.releaseConnection();}}/** * 普通的post请求 * @param url * @param params */public void post(String url, Map<String, String> params){HttpPost postMethod = new HttpPost(url);try {if(params != null){List<NameValuePair> nvps = new ArrayList<NameValuePair>();Iterator<String> it = params.keySet().iterator();while(it.hasNext()){String name = it.next();nvps.add(new BasicNameValuePair(name, params.get(name)));}postMethod.setEntity(new UrlEncodedFormEntity(nvps));}HttpResponse response = client.execute(postMethod);HttpEntity entity = response.getEntity();System.out.println(response.getStatusLine().getStatusCode());System.out.println(EntityUtils.toString(entity));} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {postMethod.releaseConnection();}}/** * 模拟form表单上传 * @param url * @param filePath * @param params */public void uploadFileAsForm(String url, String filePath, Map<String, String> params){HttpPost postMethod = new HttpPost(url);try {File file = new File(filePath);FileBody fb = new FileBody(file);MultipartEntity reqEntity = new MultipartEntity();reqEntity.addPart("myFile", fb);if(params != null){Iterator<String> it = params.keySet().iterator();while(it.hasNext()){String name = it.next();reqEntity.addPart(name, new StringBody(params.get(name)));}}postMethod.setEntity(reqEntity);HttpResponse response = client.execute(postMethod);HttpEntity entity = response.getEntity();System.out.println(response.getStatusLine().getStatusCode());System.out.println(EntityUtils.toString(entity));} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {postMethod.releaseConnection();}}/** * 以字节流的形式上传 * @param url * @param filePath */public void uploadFileAsStream(String url, String filePath){HttpPost postMethod = new HttpPost(url);File file = new File(filePath);FileEntity reqEntity = new FileEntity(file);postMethod.setEntity(reqEntity);try {HttpResponse response = client.execute(postMethod);HttpEntity entity = response.getEntity();System.out.println(response.getStatusLine().getStatusCode());System.out.println(EntityUtils.toString(entity));} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {postMethod.releaseConnection();}}/** * close http client */public void shutdown(){this.client.getConnectionManager().shutdown();}public static void main(String[] args) {Client client = new Client();client.get("http://www.baidu.com");client.shutdown();}}
- 使用httpclient4上传文件
- HttpClient4文件上传
- HttpClient4 文件上传
- HttpClient4.3 上传文件
- HttpClient4.x 文件上传
- 使用HttpClient4提交包含上传文件的表单
- HttpClient4 post上传文件, struts2 action接收
- HttpClient4 post上传文件, struts2 action接收
- httpClient4上传文件和传递参数
- 使用HttpClient4实现文件上传请求的发送,服务器端以MultipartFile形式接收(附依赖jar包地址)
- 【HTTPClient 系列】HttpClient4.2.5上传文件,无中文文件名问题
- HttpClient4模拟带文件上传的表单提交
- HttpClient4.4.1 关于文件上传 中文文件名编码问题
- HttpClient4.5.2模拟表单post提交上传文件
- HttpClient4使用
- httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据
- android原生POST、httpClient4.X实现向PHP服务器上传文件
- HttpClient4.5.2由Client客户端上传File文件流至Server服务端
- C#条码打印
- 对编程一些新的认识
- MySQL 索引详解
- java jxl解析excel
- 直线加权拟合改进版
- 使用httpclient4上传文件
- The GNU configure and build system
- 黑马程序员_多线程
- ExtJs表单几种验证与扩展
- java如何实现字符串的反转及替换
- 黑马程序员------关于异常
- 0-1背包问题、旅行推销员问题TSP
- note : OD操作整理-修改常量字符串;保存PE文件
- 五月了。