JSP远程获取图像的大小、宽度和高度
<%@ page contentType="text/html; charset=GBK" %>
<%@ page language="java" %>
<%@ page import= "java.net.* "%>
<%@ page import="java.awt.Image,java.awt.image.*" %>
<%@ page import="javax.imageio.*" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.io.*"%>
<%
//Image image = null;
try {
// Read from a file
// File file = new File("image.gif");
// image = ImageIO.read(file);
// Read from an input stream
// InputStream is = new BufferedInputStream(
// new FileInputStream("image.gif"));
// image = ImageIO.read(is);
// Read from a URL
URL url = new URL("http://www.java3z.com/cwbwebhome/images/logo.gif");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
long len = urlConnection.getContentLength();
if(len<=0){
out.print( "无法获知文件大小 ");
}else {
out.print( "文件大小为 " + len/1024+ "k "+"<br />");
}
BufferedImage bi = null;
bi = javax.imageio.ImageIO.read(url);
//image= javax.imageio.ImageIO.read(url);
int[] a = new int[2];
a[0]= bi.getWidth();
a[1] = bi.getHeight(); //获得 高度
int b=a[0];
out.print("图片宽:"+a[0]+"<br />");
out.print("图片高:"+a[1]);
} catch (IOException e) {
}
%>