JAVA_HttpClientUtils

时间:2024-01-17 20:28:26
 package org.mobiletrain.utils;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
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.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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; /**
* 请求网络的工具类
* 需导入三个jar包
    commons-logging-1.1.1.jar
    httpclient-4.1.2.jar
    httpcore-4.1.2.jar
* */
public class HttpHelpers { /**
* 下载图片 保存到byte类型的数组中
*
* @param path
* 地址
* @return byte[]
*/
public static byte[] downLoadImg(String path) {
BufferedInputStream bis = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpGet httpGet = new HttpGet(path);
// 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 5,得到响应的HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bis = new BufferedInputStream(responseEntity.getContent());
// byte b[] = toByteArray(bis);
// return b; // 方法二
return EntityUtils.toByteArray(responseEntity); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return null; } /**
* 把图片下载到本地磁盘
*
* @param path
*/
public static void downLoadImgToLocal(String path) {
BufferedInputStream bis = null;
BufferedOutputStream boStream = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpGet httpGet = new HttpGet(path);
// 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 5,得到响应的HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bis = new BufferedInputStream(responseEntity.getContent());
// byte b[] = toByteArray(bis); // 方法二
byte b[] = EntityUtils.toByteArray(responseEntity);
String endsWith = path.substring(path.lastIndexOf("."));
boStream = new BufferedOutputStream(new FileOutputStream((int) (Math.random() * 100) + endsWith));
boStream.write(b); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (boStream != null) {
try {
boStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } /**
* 从互联网获取文本 json xml
*
* @param path
* 地址
* @return 获取到的文本数据
*/ public static String getResourceByInternet(String path) {
BufferedReader bReader = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpGet httpGet = new HttpGet(path);
// 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpGet);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 得到HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bReader = new BufferedReader(new
// InputStreamReader(responseEntity.getContent()));
// StringBuilder sbBuilder = new StringBuilder();
// String line = null;
// while ((line = bReader.readLine()) != null) {
// sbBuilder.append(line);
// }
//
// return sbBuilder.toString(); // 方法二
return EntityUtils.toString(responseEntity); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return null; } public static byte[] toByteArray(BufferedInputStream bufferedInputStream) {
byte b[] = new byte[1024 * 1024];
int len = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while ((len = bufferedInputStream.read(b)) != -1) {
baos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return baos.toByteArray(); } public static String upLoadData(String path, Map<String, String> map) {
BufferedReader bReader = null;
try {
// 1,创建HttpClient对象 Android6.0之前可以使用
HttpClient httpClient = new DefaultHttpClient();
// 2,创建请求对象+指定地址
HttpPost httpPost = new HttpPost(path);
// 设置用于发送到服务端的参数
List<NameValuePair> list = new ArrayList<NameValuePair>(); for (String string : map.keySet()) {
list.add(new BasicNameValuePair(string, map.get(string)));
}
HttpEntity requestEntity = new UrlEncodedFormEntity(list, "gbk");
httpPost.setEntity(requestEntity); // 3,执行请求 获得HttpResponse对象
HttpResponse response = httpClient.execute(httpPost);
// 4,获得响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 得到HttpEntity对象
HttpEntity responseEntity = response.getEntity();
// 方法一
// bReader = new BufferedReader(new
// InputStreamReader(responseEntity.getContent()));
// StringBuilder sbBuilder = new StringBuilder();
// String line = null;
// while ((line = bReader.readLine()) != null) {
// sbBuilder.append(line);
// }
//
// return sbBuilder.toString(); // 方法二
return EntityUtils.toString(responseEntity, "gbk"); } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} return null; } public static void main(String[] args) {
byte b[] = downLoadImg("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg");
BufferedOutputStream bufferedOutputStream;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("ceo.jpg"));
// bufferedOutputStream.write(b); } catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // downLoadImgToLocal("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg"); // System.out.println(getResourceByInternet("http://www.doukantv.com/api/hot/?type=movie&cli=ipad&sys_ver=7.1.1&ver=2.1"));
Map<String, String> map = new HashMap<String, String>();
map.put("uname", "张三");
map.put("pwd", "admin");
System.out.println(upLoadData("http://172.20.136.5:8080/2016_05_27_server/login", map)); } }