HttpHttpApache兩種上傳方法

第一種:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
 * 用httpClient來模擬登陸。
 */
public class HttpPostDemo {
public static void main(String[] args) {
String path="http://192.168.10.88:8080/MyServer/login";
HttpClient client=new DefaultHttpClient();
HttpPost post = new HttpPost(path);
//將數據封裝到post對象裏。
//建立一個存儲封裝鍵值對對象的集合
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
//將數據封裝到NameValuePair對象裏,第一個參數爲鍵,第二個參數爲值
NameValuePair value1 = new BasicNameValuePair("username", "admin");
NameValuePair value2=new BasicNameValuePair("userpwd", "111");
//將對象添加到集合中。
parameters.add(value1);
parameters.add(value2);
HttpEntity entity = null;
try {
//將數據集合封裝成HttpEntity
entity = new UrlEncodedFormEntity(parameters);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//設置HttpEntity
post.setEntity(entity);
//讓客戶端執行請求。
try {
HttpResponse response=client.execute(post);
int code=response.getStatusLine().getStatusCode();
if(code==200){
HttpEntity entity2=response.getEntity();
String str=EntityUtils.toString(entity2);
System.out.println(str);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第二種:
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.FormBodyPart;
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 HttpUpload {
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
String path="http://localhost:8080/FileUpload/FileUploadServlet";
HttpPost post = new HttpPost(path);
//這裏須要上傳數據,因此須要改變表單form的enctyped屬性值,改成mul/data-form
MultipartEntity entity=new MultipartEntity();
//封裝文件
FileBody body=new FileBody(new File("e:\\login.jpeg"));
FormBodyPart pary=new FormBodyPart("form1", body);
//將封裝文件的對象添加到MultipartEntity
entity.addPart(pary);
//添加其餘的數據
try {
entity.addPart("username",new StringBody ("admin"));
entity.addPart("passwoed",new StringBody("123"));
//設置給HttpPost對象
post.setEntity(entity);
//執行請求
HttpResponse response=client.execute(post);
//獲得響應碼
int  code=response.getStatusLine().getStatusCode();
if(code==200){
HttpEntity entity2=response.getEntity();
//EntityUtils.toByteArray(entity); 
String str= EntityUtils.toString(entity2);
System.out.println(str);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相關文章
相關標籤/搜索