java获取指定地址图片高度宽度简单代码

package com.test;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

public class ImageUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		
		String imageUrl="http://avatar.csdn.net/9/F/2/1_5iasp.jpg";
		BufferedImage image=getBufferedImage(imageUrl);
		if (image!=null)
		{
			System.out.println("图片高度:"+image.getHeight());
			System.out.println("图片宽度:"+image.getWidth());
		}
		else
		{
			System.out.println("图片不存在!");
		}
		

	}
	
	/**
	 * 
	 * @param imgUrl 图片地址
	 * @return 
	 */
	public static BufferedImage getBufferedImage(String imgUrl) {
		URL url = null;
		InputStream is = null;
		BufferedImage img = null;
		try {
			url = new URL(imgUrl);
			is = url.openStream();
			img = ImageIO.read(is);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return img;
	}

}


posted on 2013-08-30 18:15  you Richer  阅读(433)  评论(0编辑  收藏  举报