Java read remote URL picture, get wide high

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.