Link Address: http://blog.sina.com.cn/s/blog_407a68fc0100nrb6.html
Import java.io.IOException;
Import Java.awt.image.BufferedImage;
Import Java.net.URL;
Import Java.io.BufferedInputStream;
Import Java.io.OutputStream;
Import Java.io.FileOutputStream;
Import Java.io.File;
Import Javax.imageio.ImageIO;
public class Img2 {
Read the remote URL picture to get a wide height
Public int[] Returnimgwh (String imgurl) {
Boolean b=false;
try {
Instantiating URLs
URL url = new URL (imgurl);
Load image to input stream
Java.io.BufferedInputStream bis = new Bufferedinputstream (Url.openstream ());
Instantiating an array of storage bytes
byte[] bytes = new BYTE[100];
Set the Write path and picture name
OutputStream BOS = new FileOutputStream (New File ("C:\\thetempimg.gif"));
int Len;
while (len = bis.read (bytes)) > 0) {
Bos.write (bytes, 0, Len);
}
Bis.close ();
Bos.flush ();
Bos.close ();
Turn off the output stream
B=true;
} catch (Exception e) {
If the picture is not found
B=false;
}
Int[] A = new int[2];
if (b) {//Picture exists
Get the file
Java.io.File File = new Java.io.File ("C:\\thetempimg.gif");
BufferedImage bi = null;
try {
Reading pictures
Bi = javax.imageio.ImageIO.read (file);
} catch (IOException ex) {
Ex.printstacktrace ();
}
A[0] = Bi.getwidth (); Get width
A[1] = Bi.getheight (); Get high
deleting files
File.delete ();
}else{//Picture does not exist
A=null;
}
return A;
}
public static void Main (string[] args) {
Img2 i = new Img2 ();
Int[] A=i.returnimgwh ("http://www.baidu.com/img/baidu_logo.gif");
if (a==null) {
System.out.println ("Picture not Found!");
}else{
System.out.println ("width is" + a[0]);
System.out.println ("High for" + a[1]);
}
}
}
Java read remote URL picture, get wide high