標籤:
要下載jar包import java.io.FileOutputStream;import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;/* * 類比下載 * 使用到的類: * 1.HttpClient: * 2.HttpResponse * 3.HttpEntity * 4.EntityUtils * */public class HttpDemo1 { public static void main(String[] args) throws IOException { // HttpClient:建立了用戶端。 HttpClient client = new DefaultHttpClient(); // 請求 get:HttpGet String path = "http://www.baidu.com/img/bdlogo.gif"; HttpGet get = new HttpGet(path); // 讓用戶端執行請求。 HttpResponse response = client.execute(get); // 資料全部在HttpResponse // 1:響應碼。 int code = response.getStatusLine().getStatusCode(); if(code == 200){ // 取出返回的資料。 資料封裝到HttpEntity對象。 HttpEntity entity = response.getEntity(); // 如何獲得HttpEntity對象中的資料。 byte[] b = EntityUtils.toByteArray(entity); FileOutputStream fos = new FileOutputStream("e:\\bb.gif"); fos.write(b); fos.flush(); fos.close(); } }}import java.io.File;import java.io.IOException;import java.nio.charset.Charset;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;/* * 使用的類: * HttpClient * HttpPost * FileBody * FormBodyPart * MultipartEntity * HttpResponse * EntityUtils * */public class HttpDemo5 { public static void main(String[] args) throws ClientProtocolException, IOException { //1:建立HttpClient 對象(建立用戶端) HttpClient client = new DefaultHttpClient(); //2:建立請求方式(web中的請求方式method) String uri = "http://localhost:8080/FileUpload/FileUploadServlet"; HttpPost post = new HttpPost(uri); //3:封裝要發送的資料(檔案) //3.1:擷取本地的檔案 File file = new File("e:\\aa.jpg"); //3.2:建立FileBody對象(檔案主體) FileBody fileBody = new FileBody(file); //3.3:建立FormBodyPart 對象(表單主體部分) FormBodyPart part = new FormBodyPart("form", fileBody); //4:建立MultipartEntity對象。MultipartEntity:多組件實體 MultipartEntity entity = new MultipartEntity(); //5:把表單主體部分添加到多組件實體中(將檔案類型的資料添加到entity中) entity.addPart(part); //5:將普通文本資料添加到多組件實體中 entity.addPart("username", new StringBody("哈哈", "text/html", Charset.forName("utf-8"))); entity.addPart("password", new StringBody("123")); //6:佈建要求的實體 post.setEntity(entity); //7:讓用戶端執行請求(帶有資料的請求),得到的是HttpResponse對象(響應對象) HttpResponse response = client.execute(post); //8:通過響應對象擷取響應碼 int code = response.getStatusLine().getStatusCode(); //9:如果響應碼為200(成功響應碼),則擷取伺服器返回的資料 if(code == 200){ //9.1:擷取HttpEntity對象(通過響應來擷取) HttpEntity entity2 = response.getEntity(); //10:使用EntityUtils工具類,將擷取到的資料(實體)轉換為位元組數組形式,任何檔案都可以以位元組的形式儲存 byte[] b = EntityUtils.toByteArray(entity2); //輸出內容 System.out.println(new String(b, "utf-8")); } }}
【HttpClient】案例學習