1 package org.mobiletrain.utils; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.BufferedReader; 6 import java.io.ByteArrayOutputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.Map; 14 15 import org.apache.http.HttpEntity; 16 import org.apache.http.HttpResponse; 17 import org.apache.http.NameValuePair; 18 import org.apache.http.client.HttpClient; 19 import org.apache.http.client.entity.UrlEncodedFormEntity; 20 import org.apache.http.client.methods.HttpGet; 21 import org.apache.http.client.methods.HttpPost; 22 import org.apache.http.impl.client.DefaultHttpClient; 23 import org.apache.http.message.BasicNameValuePair; 24 import org.apache.http.util.EntityUtils; 25 26 /** 27 * 请求网络的工具类 28 * 需导入三个jar包
commons-logging-1.1.1.jar
httpclient-4.1.2.jar
httpcore-4.1.2.jar 29 * 31 */ 32 public class HttpHelpers { 33 34 /** 35 * 下载图片 保存到byte类型的数组中 36 * 37 * @param path 38 * 地址 39 * @return byte[] 40 */ 41 public static byte[] downLoadImg(String path) { 42 BufferedInputStream bis = null; 43 try { 44 // 1,创建HttpClient对象 Android6.0之前可以使用 45 HttpClient httpClient = new DefaultHttpClient(); 46 // 2,创建请求对象+指定地址 47 HttpGet httpGet = new HttpGet(path); 48 // 3,执行请求 获得HttpResponse对象 49 HttpResponse response = httpClient.execute(httpGet); 50 // 4,获得响应码 51 int code = response.getStatusLine().getStatusCode(); 52 if (code == 200) { 53 // 5,得到响应的HttpEntity对象 54 HttpEntity responseEntity = response.getEntity(); 55 // 方法一 56 // bis = new BufferedInputStream(responseEntity.getContent()); 57 // byte b[] = toByteArray(bis); 58 // return b; 59 60 // 方法二 61 return EntityUtils.toByteArray(responseEntity); 62 63 } 64 65 } catch (IOException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } finally { 69 if (bis != null) { 70 try { 71 bis.close(); 72 } catch (IOException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 75 } 76 } 77 } 78 79 return null; 80 81 } 82 83 /** 84 * 把图片下载到本地磁盘 85 * 86 * @param path 87 */ 88 public static void downLoadImgToLocal(String path) { 89 BufferedInputStream bis = null; 90 BufferedOutputStream boStream = null; 91 try { 92 // 1,创建HttpClient对象 Android6.0之前可以使用 93 HttpClient httpClient = new DefaultHttpClient(); 94 // 2,创建请求对象+指定地址 95 HttpGet httpGet = new HttpGet(path); 96 // 3,执行请求 获得HttpResponse对象 97 HttpResponse response = httpClient.execute(httpGet); 98 // 4,获得响应码 99 int code = response.getStatusLine().getStatusCode(); 100 if (code == 200) { 101 // 5,得到响应的HttpEntity对象 102 HttpEntity responseEntity = response.getEntity(); 103 // 方法一 104 // bis = new BufferedInputStream(responseEntity.getContent()); 105 // byte b[] = toByteArray(bis); 106 107 // 方法二 108 byte b[] = EntityUtils.toByteArray(responseEntity); 109 String endsWith = path.substring(path.lastIndexOf(".")); 110 boStream = new BufferedOutputStream(new FileOutputStream((int) (Math.random() * 100) + endsWith)); 111 boStream.write(b); 112 113 } 114 115 } catch (IOException e) { 116 // TODO Auto-generated catch block 117 e.printStackTrace(); 118 } finally { 119 if (bis != null) { 120 try { 121 bis.close(); 122 } catch (IOException e) { 123 // TODO Auto-generated catch block 124 e.printStackTrace(); 125 } 126 } 127 if (boStream != null) { 128 try { 129 boStream.close(); 130 } catch (IOException e) { 131 // TODO Auto-generated catch block 132 e.printStackTrace(); 133 } 134 } 135 } 136 137 } 138 139 /** 140 * 从互联网获取文本 json xml 141 * 142 * @param path 143 * 地址 144 * @return 获取到的文本数据 145 */ 146 147 public static String getResourceByInternet(String path) { 148 BufferedReader bReader = null; 149 try { 150 // 1,创建HttpClient对象 Android6.0之前可以使用 151 HttpClient httpClient = new DefaultHttpClient(); 152 // 2,创建请求对象+指定地址 153 HttpGet httpGet = new HttpGet(path); 154 // 3,执行请求 获得HttpResponse对象 155 HttpResponse response = httpClient.execute(httpGet); 156 // 4,获得响应码 157 int code = response.getStatusLine().getStatusCode(); 158 if (code == 200) { 159 // 得到HttpEntity对象 160 HttpEntity responseEntity = response.getEntity(); 161 // 方法一 162 // bReader = new BufferedReader(new 163 // InputStreamReader(responseEntity.getContent())); 164 // StringBuilder sbBuilder = new StringBuilder(); 165 // String line = null; 166 // while ((line = bReader.readLine()) != null) { 167 // sbBuilder.append(line); 168 // } 169 // 170 // return sbBuilder.toString(); 171 172 // 方法二 173 return EntityUtils.toString(responseEntity); 174 175 } 176 177 } catch (IOException e) { 178 // TODO Auto-generated catch block 179 e.printStackTrace(); 180 } finally { 181 if (bReader != null) { 182 try { 183 bReader.close(); 184 } catch (IOException e) { 185 // TODO Auto-generated catch block 186 e.printStackTrace(); 187 } 188 } 189 } 190 191 return null; 192 193 } 194 195 public static byte[] toByteArray(BufferedInputStream bufferedInputStream) { 196 byte b[] = new byte[1024 * 1024]; 197 int len = 0; 198 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 199 try { 200 while ((len = bufferedInputStream.read(b)) != -1) { 201 baos.write(b, 0, len); 202 } 203 } catch (IOException e) { 204 // TODO Auto-generated catch block 205 e.printStackTrace(); 206 } 207 208 return baos.toByteArray(); 209 210 } 211 212 public static String upLoadData(String path, Map<String, String> map) { 213 BufferedReader bReader = null; 214 try { 215 // 1,创建HttpClient对象 Android6.0之前可以使用 216 HttpClient httpClient = new DefaultHttpClient(); 217 // 2,创建请求对象+指定地址 218 HttpPost httpPost = new HttpPost(path); 219 // 设置用于发送到服务端的参数 220 List<NameValuePair> list = new ArrayList<NameValuePair>(); 221 222 for (String string : map.keySet()) { 223 list.add(new BasicNameValuePair(string, map.get(string))); 224 } 225 HttpEntity requestEntity = new UrlEncodedFormEntity(list, "gbk"); 226 httpPost.setEntity(requestEntity); 227 228 // 3,执行请求 获得HttpResponse对象 229 HttpResponse response = httpClient.execute(httpPost); 230 // 4,获得响应码 231 int code = response.getStatusLine().getStatusCode(); 232 if (code == 200) { 233 // 得到HttpEntity对象 234 HttpEntity responseEntity = response.getEntity(); 235 // 方法一 236 // bReader = new BufferedReader(new 237 // InputStreamReader(responseEntity.getContent())); 238 // StringBuilder sbBuilder = new StringBuilder(); 239 // String line = null; 240 // while ((line = bReader.readLine()) != null) { 241 // sbBuilder.append(line); 242 // } 243 // 244 // return sbBuilder.toString(); 245 246 // 方法二 247 return EntityUtils.toString(responseEntity, "gbk"); 248 249 } 250 251 } catch (IOException e) { 252 // TODO Auto-generated catch block 253 e.printStackTrace(); 254 } finally { 255 if (bReader != null) { 256 try { 257 bReader.close(); 258 } catch (IOException e) { 259 // TODO Auto-generated catch block 260 e.printStackTrace(); 261 } 262 } 263 } 264 265 return null; 266 267 } 268 269 public static void main(String[] args) { 270 byte b[] = downLoadImg("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg"); 271 BufferedOutputStream bufferedOutputStream; 272 try { 273 bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("ceo.jpg")); 274 // bufferedOutputStream.write(b); 275 276 } catch (FileNotFoundException e1) { 277 // TODO Auto-generated catch block 278 e1.printStackTrace(); 279 } catch (IOException e) { 280 // TODO Auto-generated catch block 281 e.printStackTrace(); 282 } 283 284 // downLoadImgToLocal("http://images.china.cn/attachement/jpg/site1000/20140313/844bf52c7d7c148b8abc05.jpg"); 285 286 // System.out.println(getResourceByInternet("http://www.doukantv.com/api/hot/?type=movie&cli=ipad&sys_ver=7.1.1&ver=2.1")); 287 Map<String, String> map = new HashMap<String, String>(); 288 map.put("uname", "张三"); 289 map.put("pwd", "admin"); 290 System.out.println(upLoadData("http://172.20.136.5:8080/2016_05_27_server/login", map)); 291 292 } 293 294 }