图片转换成字节流或者保存到本地

  1. package com.hg.threadPoolExecutorDemo;  
  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.  * @version 1.0  
  11.  * @since  
  12.  */    
  13. public class GetImage {    
  14.     /**  
  15.      * 测试  
  16.      * @param args  
  17.      */    
  18.     public static void main(String[] args) {    
  19.         String url = "http://www.baidu.com/img/baidu_sylogo1.gif";    
  20.         byte[] btImg = getImageFromNetByUrl(url);    
  21.         if(null != btImg && btImg.length > 0){    
  22.             System.out.println("读取到:" + btImg.length + " 字节");    
  23.             String fileName = "百度.gif";    
  24.             writeImageToDisk(btImg, fileName);    
  25.         }else{    
  26.             System.out.println("没有从该连接获得内容");    
  27.         }    
  28.     }    
  29.     /**  
  30.      * 将图片写入到磁盘  
  31.      * @param img 图片数据流  
  32.      * @param fileName 文件保存时的名称  
  33.      */    
  34.     public static void writeImageToDisk(byte[] img, String fileName){    
  35.         try {    
  36.             File file = new File("D:\\" + fileName);    
  37.             FileOutputStream fops = new FileOutputStream(file);    
  38.             fops.write(img);    
  39.             fops.flush();    
  40.             fops.close();    
  41.             System.out.println("图片已经写入到D盘");    
  42.         } catch (Exception e) {    
  43.             e.printStackTrace();    
  44.         }    
  45.     }    
  46.     /**  
  47.      * 根据地址获得数据的字节流  
  48.      * @param strUrl 网络连接地址  
  49.      * @return  
  50.      */    
  51.     public static byte[] getImageFromNetByUrl(String strUrl){    
  52.         try {    
  53.             URL url = new URL(strUrl);    
  54.             HttpURLConnection conn = (HttpURLConnection)url.openConnection();    
  55.             conn.setRequestMethod("GET");    
  56.             conn.setConnectTimeout(5 * 1000);    
  57.             InputStream inStream = conn.getInputStream();//通过输入流获取图片数据    
  58.             byte[] btImg = readInputStream(inStream);//得到图片的二进制数据    
  59.             return btImg;    
  60.         } catch (Exception e) {    
  61.             e.printStackTrace();    
  62.         }    
  63.         return null;    
  64.     }    
  65.     /**  
  66.      * 从输入流中获取数据  
  67.      * @param inStream 输入流  
  68.      * @return  
  69.      * @throws Exception  
  70.      */    
  71.     public static byte[] readInputStream(InputStream inStream) throws Exception{    
  72.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();    
  73.         byte[] buffer = new byte[1024];    
  74.         int len = 0;    
  75.         while( (len=inStream.read(buffer)) != -1 ){    
  76.             outStream.write(buffer, 0, len);    
  77.         }    
  78.         inStream.close();    
  79.         return outStream.toByteArray();    
  80.     }    
  81. }    

第二种方法:
[java]  view plain  copy
  1. package com.hg.threadPoolExecutorDemo;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.net.URL;  
  8. import java.net.URLConnection;  
  9.   
  10. public class DownloadImage {  
  11.   
  12.     /**  
  13.      * @param args  
  14.      * @throws Exception   
  15.      */  
  16.     public static void main(String[] args) throws Exception {  
  17.         // TODO Auto-generated method stub    
  18. //      download("http://ui.51bi.com/opt/siteimg/images/fanbei0923/Mid_07.jpg",  
  19. //              "51bi.jpg", "D:\\image\\");  
  20.         download("http://www.hao123.com/",  
  21.                 "nidi.html""D:\\image\\");  
  22.     }  
  23.   
  24.     public static void download(String urlString, String filename,  
  25.             String savePath) throws Exception {  
  26.         // 构造URL    
  27.         URL url = new URL(urlString);  
  28.         // 打开连接    
  29.         URLConnection con = url.openConnection();  
  30.         //设置请求超时为5s    
  31.         con.setConnectTimeout(5 * 1000);  
  32.         // 输入流    
  33.         InputStream is = con.getInputStream();  
  34.   
  35.         // 1K的数据缓冲    
  36.         byte[] bs = new byte[1024];  
  37.         // 读取到的数据长度    
  38.         int len;  
  39.         // 输出的文件流    
  40.         File sf = new File(savePath);  
  41.         if (!sf.exists()) {  
  42.             sf.mkdirs();  
  43.         }  
  44.         OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename);  
  45.         // 开始读取    
  46.         while ((len = is.read(bs)) != -1) {  
  47.             os.write(bs, 0, len);  
  48.         }  
  49.         // 完毕,关闭所有链接    
  50.         os.close();  
  51.         is.close();  
  52.     }  
  53.   
  54. }  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值