[转载]HttpClient通过HttpPost传递参数上传图片代码 - 我们轻松了,并不是生活越来越容易了,而是我们越来越强 - ITeye技术网站

[转载]HttpClient通过HttpPost传递参数 – 我们轻松了,并不是生活越来越容易了,而是我们越来越强 – ITeye技术网站.

HttpClient新版本(新老版本没有做明确界定,Sorry!)

package http;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpTests {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("******/abc");
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("username", "vip"));
		nvps.add(new BasicNameValuePair("password", "secret"));
		httpPost.setEntity(new UrlEncodedFormEntity(nvps));
		httpclient.execute(httpPost);
		httpclient.getConnectionManager().shutdown();
	}

}

HttpClient 老版本:

package http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpTests {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		HttpClient httpclient = new HttpClient();
		PostMethod httpPost =new PostMethod("******/abc");
        NameValuePair[] param = { new NameValuePair("username", "vip")};
        httpPost.setRequestBody(param); 
		httpclient.executeMethod(httpPost);
	}

}

上传图片:

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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.util.EntityUtils;

public class HttpClientPostMethod {
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		post();
	}

	public static void post() throws ClientProtocolException, IOException {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://localhost:8080/action.jsp");
		FileBody fileBody = new FileBody(new File("/home/sendpix0.jpg"));
		StringBody stringBody = new StringBody("文件的描述");
		MultipartEntity entity = new MultipartEntity();
		entity.addPart("file", fileBody);
		entity.addPart("desc", stringBody);
		post.setEntity(entity);
		HttpResponse response = httpclient.execute(post);
		if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){  
			
			HttpEntity entitys = response.getEntity();
			if (entity != null) {
				System.out.println(entity.getContentLength());
				System.out.println(EntityUtils.toString(entitys));
			}
        }
		httpclient.getConnectionManager().shutdown();
	}
}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏