package com;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import com.yipai.util.ImageCompressUtil;

public class ImgeTest {
	//读取远程url图片,得到宽高
    public static int[] returnImgWH(String imgurl) {
        boolean b=false;
        try {
            //实例化url
            URL url = new URL(imgurl);
            //载入图片到输入流
            java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
            //实例化存储字节数组
            byte[] bytes = new byte[100];
            //设置写入路径以及图片名称
            OutputStream bos = new FileOutputStream(new File( "D:\\1.jpg"));
            int len;
            while ((len = bis.read(bytes)) > 0) {
                bos.write(bytes, 0, len);
            }
            bis.close();
            bos.flush();
            bos.close();
            //关闭输出流
            b=true;
        } catch (Exception e) {
            //如果图片未找到
            b=false;
        }
        int[] a = new int[2];
        if(b){    //图片存在
            //得到文件
            java.io.File file = new java.io.File("D:\\1.jpg");
            BufferedImage bi = null;
            try {
                //读取图片
                bi = javax.imageio.ImageIO.read(file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            a[0] = bi.getWidth(); //获得 宽度
            a[1] = bi.getHeight(); //获得 高度
            System.out.println(a[0]);
            System.out.println(a[1]);
            //删除文件
//            file.delete();
        }else{     //图片不存在
            a=null;
        }
       return a;

    }
	public static void main(String args[]) throws Exception {
		
		
		ImageCompressUtil.returnImgWH("http://pic80.nipic.com/file/20151024/20212712_161959849911_2.jpg");
	}
}
最近下载更多
chenxiaoqiang  LV2 2022年3月2日
john93  LV15 2018年12月14日
hw2166  LV3 2017年10月23日
王子不能再爱了  LV1 2016年6月11日
dz1020  LV6 2016年5月6日
myshier  LV1 2016年3月31日
WwZQ  LV15 2016年2月2日
tankai998  LV12 2016年1月28日
最近浏览更多
893889486 2022年9月7日
暂无贡献等级
niugaofeng  LV3 2021年8月4日
jiuliuge  LV1 2021年5月21日
406615406  LV2 2021年1月11日
dongzhan  LV12 2020年12月7日
wei112233  LV15 2020年5月20日
帝释天  LV2 2020年4月14日
浮生hjhj  LV2 2019年8月8日
annazhang  LV29 2019年5月17日
weixh7  LV25 2019年3月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友