資源圖片下載

  1. package imageView;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8. /** 
  9.  * @說明 從網絡獲取圖片到本地 
  10.  * @author ………… 
  11.  * @version 1.0 
  12.  * @since 
  13.  */  
  14. public class GetImage {  
  15.     /** 
  16.      * 測試 
  17.      * @param args 
  18.      */  
  19.     public static void main(String[] args) {  
  20.         String url = "http://www.baidu.com/img/baidu_sylogo1.gif";  
  21.         byte[] btImg = getImageFromNetByUrl(url);  
  22.         if(null != btImg && btImg.length > 0){  
  23.             System.out.println("讀取到:" + btImg.length + " 字節");  
  24.             String fileName = "百度.gif";  
  25.             writeImageToDisk(btImg, fileName);  
  26.         }else{  
  27.             System.out.println("沒有從該連接獲得內容");  
  28.         }  
  29.     }  
  30.     /** 
  31.      * 將圖片寫入到磁盤 
  32.      * @param img 圖片數據流 
  33.      * @param fileName 文件保存時的名稱 
  34.      */  
  35.     public static void writeImageToDisk(byte[] img, String fileName){  
  36.         try {  
  37.             File file = new File("D:\\" + fileName);  
  38.             FileOutputStream fops = new FileOutputStream(file);  
  39.             fops.write(img);  
  40.             fops.flush();  
  41.             fops.close();  
  42.             System.out.println("圖片已經寫入到D盤");  
  43.         } catch (Exception e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.     /** 
  48.      * 根據地址獲得數據的字節流 
  49.      * @param strUrl 網絡連接地址 
  50.      * @return 
  51.      */  
  52.     public static byte[] getImageFromNetByUrl(String strUrl){  
  53.         try {  
  54.             URL url = new URL(strUrl);  
  55.             HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  56.             conn.setRequestMethod("GET");  
  57.             conn.setConnectTimeout(5 * 1000);  
  58.             InputStream inStream = conn.getInputStream();//通過輸入流獲取圖片數據  
  59.             byte[] btImg = readInputStream(inStream);//得到圖片的二進制數據  
  60.             return btImg;  
  61.         } catch (Exception e) {  
  62.             e.printStackTrace();  
  63.         }  
  64.         return null;  
  65.     }  
  66.     /** 
  67.      * 從輸入流中獲取數據 
  68.      * @param inStream 輸入流 
  69.      * @return 
  70.      * @throws Exception 
  71.      */  
  72.     public static byte[] readInputStream(InputStream inStream) throws Exception{  
  73.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  74.         byte[] buffer = new byte[1024];  
  75.         int len = 0;  
  76.         while( (len=inStream.read(buffer)) != -1 ){  
  77.             outStream.write(buffer, 0, len);  
  78.         }  
  79.         inStream.close();  
  80.         return outStream.toByteArray();  
  81.     }  
  82. }  
發佈了17 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章