Http编程(二)使用Apache 的API实现

要下载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"));
        }
        
    }

}

 

 

 

posted @ 2015-08-29 20:15  尽情飞奔  阅读(3304)  评论(2编辑  收藏  举报