java读取远程url图片并获取图片宽高

  1. import java.awt.image.BufferedImage;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7. import java.net.URL;  
  8.   
  9. public class ReturnImgWH {  
  10.     //读取远程url图片,得到宽高  
  11.     public int[] returnImgWH(String imgurl) {  
  12.         boolean b=false;  
  13.         try {  
  14.             //实例化url  
  15.             URL url = new URL(imgurl);  
  16.             //载入图片到输入流  
  17.             java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());  
  18.             //实例化存储字节数组  
  19.             byte[] bytes = new byte[100];  
  20.             //设置写入路径以及图片名称  
  21.             OutputStream bos = new FileOutputStream(new File("C:\\thetempimg.gif"));  
  22.             int len;  
  23.             while ((len = bis.read(bytes)) > 0) {  
  24.                 bos.write(bytes, 0, len);  
  25.             }  
  26.             bis.close();  
  27.             bos.flush();  
  28.             bos.close();  
  29.             //关闭输出流  
  30.             b=true;  
  31.         } catch (Exception e) {  
  32.             //如果图片未找到  
  33.             b=false;  
  34.         }  
  35.         int[] a = new int[2];  
  36.         if(b){//图片存在  
  37.             //得到文件  
  38.             java.io.File file = new java.io.File("C:\\thetempimg.gif");  
  39.             BufferedImage bi = null;  
  40.             boolean imgwrong=false;  
  41.             try {  
  42.                 //读取图片  
  43.                 bi = javax.imageio.ImageIO.read(file);  
  44.                 try{  
  45.                     //判断文件图片是否能正常显示,有些图片编码不正确  
  46.                     int i = bi.getType();  
  47.                     imgwrong=true;  
  48.                 }catch(Exception e){  
  49.                     imgwrong=false;  
  50.                 }  
  51.             } catch (IOException ex) {  
  52.                 ex.printStackTrace();  
  53.             }  
  54.             if(imgwrong){  
  55.                 a[0] = bi.getWidth(); //获得 宽度  
  56.                 a[1] = bi.getHeight(); //获得 高度  
  57.             }else{  
  58.                 a=null;  
  59.             }  
  60.             //删除文件  
  61.             file.delete();  
  62.         }else{//图片不存在  
  63.             a=null;  
  64.         }  
  65.        return a;  
  66.   
  67.     }  
  68.   
  69.     public static void main(String[] args) {  
  70.         ReturnImgWH i = new ReturnImgWH();  
  71.           int[] a=i.returnImgWH("http://www.baidu.com/img/logo-yy.gif");  
  72.           if(a==null){  
  73.               System.out.println("图片未找到!");  
  74.           }else{  
  75.               System.out.println("宽为" + a[0]);  
  76.               System.out.println("高为" + a[1]);  
  77.           }  
  78.     }  
  79. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值