HttpURLConnection根据URL下载图片

来源:互联网 发布:windows defender 禁用 编辑:程序博客网 时间:2024/04/28 08:09

来看下最简单的根据URL下载图片,此方法在实际开发中,不建议使用,因为有些图片是下载不了的,比如百度上的一些图片,返回http的响应码是405

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cn.ztz.test;  
  2.   
  3. import java.io.BufferedOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.InetAddress;  
  10. import java.net.URL;  
  11.   
  12. public class HttpDownLoad {  
  13.     public static void download(String url, String dir,String fileName) {  
  14.         HttpURLConnection httpURLConnection = null;  
  15.         OutputStream out = null;  
  16.         InputStream in = null;  
  17.         try {  
  18.             URL sendUrl = new URL(url);  
  19.             httpURLConnection = (HttpURLConnection) sendUrl.openConnection();  
  20.             httpURLConnection.setRequestMethod("POST");  
  21.             httpURLConnection.setRequestProperty("contentType""utf-8");  
  22.             httpURLConnection.setDoOutput(true);   
  23.             httpURLConnection.setUseCaches(false);  
  24.             httpURLConnection.setConnectTimeout(3000);  
  25.             httpURLConnection.setReadTimeout(3000);  
  26.             httpURLConnection.setRequestProperty(  
  27.                     "User-agent",InetAddress.getLocalHost().getHostAddress() + ":"  
  28.                             + System.getProperty("catalina.home"));  
  29.             out = httpURLConnection.getOutputStream();  
  30.             // 清空缓冲区数据  
  31.             out.flush();  
  32.             // 获取HTTP状态码  
  33.             int httpStatusCode = httpURLConnection.getResponseCode();  
  34.             if(httpStatusCode!=200){  
  35.                 throw new RuntimeException("异常");  
  36.             }  
  37.             in = httpURLConnection.getInputStream();  
  38.             // 获取文件长度  
  39.             int len = httpURLConnection.getContentLength();  
  40.             // 路径+文件名  
  41.             String pathAndName = dir + File.separator + fileName;  
  42.             // 保存文件  
  43.             saveFileByByte(in, pathAndName, len);  
  44.         } catch (Exception e) {  
  45.             e.printStackTrace();  
  46.         } finally {  
  47.             if (out != null) {  
  48.                 try {  
  49.                     out.close();  
  50.                 } catch (Exception e) {  
  51.                     e.printStackTrace();  
  52.                     throw new RuntimeException(e.getMessage());  
  53.                 }  
  54.             }  
  55.             if (in != null) {  
  56.                 try {  
  57.                     in.close();  
  58.                 } catch (Exception e) {  
  59.                     e.printStackTrace();  
  60.                 }  
  61.             }  
  62.             if (httpURLConnection != null) {  
  63.                 httpURLConnection.disconnect();  
  64.                 httpURLConnection = null;  
  65.             }  
  66.         }  
  67.     }  
  68.   
  69.     //写文件  
  70.     private static void saveFileByByte(InputStream in, String path, int len)  
  71.             throws Exception {  
  72.         byte[] byteDatas = new byte[len];  
  73.         BufferedOutputStream bw = null;  
  74.         try {  
  75.             // 创建文件对象  
  76.             File f = new File(path);  
  77.             // 创建文件路径  
  78.             if (!f.getParentFile().exists())  
  79.                 f.getParentFile().mkdirs();  
  80.             // 写入文件  
  81.             bw = new BufferedOutputStream(new FileOutputStream(path));  
  82.             int bytesRead = 0;  
  83.             while ((bytesRead = in.read(byteDatas, 0, byteDatas.length)) != -1) {  
  84.                 bw.write(byteDatas, 0, bytesRead);  
  85.             }  
  86.         } catch (Exception e) {  
  87.             e.printStackTrace();  
  88.             throw e;  
  89.         } finally {  
  90.             try {  
  91.                 if (bw != null)  
  92.                     bw.close();  
  93.             } catch (Exception e) {  
  94.                 throw e;  
  95.             }  
  96.         }  
  97.     }  
  98. }  
0 0