您的位置 首页 java

java读取远程url图片,得到宽高

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 {

//读取远程url图片,得到宽高

public 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( “C:\\thetempimg.gif”));

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(“C:\\thetempimg.gif”);

BufferedImage bi = null;

try {

//读取图片

bi = javax.imageio.ImageIO.read(file);

} catch (IOException ex) {

ex.printStackTrace();

}

a[0] = bi.getWidth(); //获得 宽度

a[1] = bi.getHeight(); //获得 高度

//删除文件

file.delete();

}else{ //图片不存在

a=null;

}

return a;

}

public static void main(String[] args) {

Img2 i = new Img2();

int[] a=i.returnImgWH(“#34;);

if(a==null){

System.out.println(“图片未找到!”);

}else{

System.out.println(“宽为” + a[0]);

System.out.println(“高为” + a[1]);

}

}

}

文章来源:智云一二三科技

文章标题:java读取远程url图片,得到宽高

文章地址:https://www.zhihuclub.com/184190.shtml

关于作者: 智云科技

热门文章

评论已关闭

38条评论

  1. After I originally left a comment I appear to have clicked the -Notify me when new
    comments are added- checkbox and from now on every
    time a comment is added I get four emails with the exact same comment.
    There has to be an easy method you are able to remove me
    from that service? Thanks a lot!

  2. I think that what you posted was actually very logical.
    But, think about this, what if you composed a catchier title?
    I mean, I don’t wish to tell you how to run your blog,
    however suppose you added something that grabbed people’s attention? I
    mean java读取远程url图片,得到宽高 is kinda vanilla.

    You might peek at Yahoo’s home page and note how
    they write news titles to grab viewers interested. You might add a video or a related pic or two to get people interested about what
    you’ve written. Just my opinion, it could bring your website a
    little livelier.

  3. Excellent goods from you, man. I’ve take into account your
    stuff previous to and you are just too wonderful.
    I actually like what you’ve bought here, certainly like what you’re stating and the best way during which you assert it.
    You are making it enjoyable and you still care for to stay it smart.
    I can not wait to read much more from you. This is really a wonderful web site.

  4. Hi there would you mind stating which blog platform
    you’re working with? I’m looking to start my own blog in the near future but I’m having a tough time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking
    for something unique. P.S Sorry for getting off-topic but I had to
    ask!

  5. Wow that was strange. I just wrote an really long comment but after I clicked submit my
    comment didn’t show up. Grrrr… well I’m
    not writing all that over again. Anyway,
    just wanted to say fantastic blog!

  6. This is my first time go to see at here and i am actually pleassant to read everthing at alone place.

  7. Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis.

    It’s always useful to read through content from
    other writers and use something from their web sites.

  8. Hi colleagues, its fantastic article on the topic of cultureand fully explained, keep it up
    all the time.

  9. These are actually fantastic ideas in on the topic of blogging.
    You have touched some fastidious things here. Any way keep up wrinting.

  10. Hey There. I found your blog using msn. This is a really well
    written article. I’ll be sure to bookmark it and come back
    to read more of your useful info. Thanks for the post.
    I’ll certainly comeback.

  11. What you published made a great deal of sense. But, consider
    this, suppose you typed a catchier title? I ain’t saying your
    content is not solid, but suppose you added a title that makes people want more?
    I mean java读取远程url图片,得到宽高 is kinda
    vanilla. You might peek at Yahoo’s front page and note how
    they write post titles to grab viewers interested. You might try adding a video or a related pic or two to get people interested about what you’ve written. Just my opinion, it might make your website a little
    bit more interesting.

  12. I absolutely love your website.. Pleasant colors & theme.

    Did you build this website yourself? Please reply back as I’m
    wanting to create my very own blog and would like to find out where you got this from or just what the theme is
    named. Thank you!

  13. Valuable info. Fortunate me I discovered your website unintentionally, and I am shocked why this accident didn’t happened earlier!
    I bookmarked it.

  14. Hey there would you mind sharing which blog platform you’re using?
    I’m looking to start my own blog soon but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m
    looking for something unique. P.S Sorry for getting off-topic but
    I had to ask!

  15. Thank you for sharing your thoughts. I really appreciate your efforts and
    I am waiting for your further write ups thank you once again.

  16. I always spent my half an hour to read this blog’s content daily along with a cup of coffee.

  17. Amazing! This blog looks just like my old one! It’s on a completely different topic but it has pretty much the same page layout and design. Outstanding choice of colors!

  18. Hi great website! Does running a blog such as this take a massive amount
    work? I have very little understanding of coding but I was hoping to start my
    own blog in the near future. Anyway, should you have any suggestions or tips for new blog owners please share.
    I know this is off topic nevertheless I just wanted to ask.
    Kudos!

  19. I was pretty pleased to uncover this great
    site. I need to to thank you for your time for this fantastic read!!
    I definitely really liked every bit of it and i also have you book-marked to check out new information on your web site.

  20. What’s up to all, how is all, I think every one is getting more from this site, and your views are fastidious in support of new people.

  21. Please let me know if you’re looking for a
    article writer for your blog. You have some really great articles and I believe I
    would be a good asset. If you ever want to take
    some of the load off, I’d absolutely love to write some material for your blog in exchange
    for a link back to mine. Please shoot me
    an e-mail if interested. Thank you!

  22. Fastidious response in return of this matter with firm arguments and describing
    everything concerning that.

  23. It’s nearly impossible to find knowledgeable people
    about this topic, however, you seem like
    you know what you’re talking about! Thanks

  24. Thanks for your marvelous posting! I certainly enjoyed reading it,
    you might be a great author.I will be sure to bookmark your blog and will come back very soon. I want to encourage you
    to continue your great work, have a nice day!

  25. I like what you guys are usually up too. This sort of clever work and coverage!
    Keep up the good works guys I’ve incorporated you guys to my personal blogroll.

  26. Having read this I thought it was really informative.
    I appreciate you taking the time and energy to put this content together.

    I once again find myself spending way too much time both reading and posting
    comments. But so what, it was still worth it!

  27. Definitely believe that which you stated. Your favorite reason appeared to be on the internet the simplest thing to be aware of.

    I say to you, I definitely get annoyed while people think about worries
    that they plainly don’t know about. You managed to hit the nail
    upon the top and defined out the whole thing without having side-effects
    , people can take a signal. Will probably be back to get more.
    Thanks

  28. What’s Happening i am new to this, I stumbled upon this
    I’ve discovered It positively useful and it has aided me out loads.

    I am hoping to contribute & help other customers like its helped me.
    Good job.

  29. each time i used to read smaller articles or reviews that
    as well clear their motive, and that is also happening with this article which I am reading
    at this time.

  30. I know this web site offers quality dependent articles and other information, is there any other website which presents these kinds of data in quality?

  31. I’m not sure where you are getting your info, but great topic.
    I needs to spend some time learning much more or understanding more.

    Thanks for wonderful information I was looking for
    this information for my mission.

  32. Hello very cool website!! Man .. Beautiful .. Wonderful ..
    I’ll bookmark your site and take the feeds also? I’m satisfied to find so
    many helpful info here within the put up, we’d like work out more techniques on this regard,
    thank you for sharing. . . . . .

  33. It’s very effortless to find out any topic on net as compared
    to textbooks, as I found this piece of writing at this
    site.

  34. Nice post. I used to be checking continuously this weblog and I’m impressed!
    Very helpful information particularly the closing part 🙂 I deal with such info a lot.

    I was looking for this particular information for a very long time.
    Thank you and good luck.

  35. Good day I am so glad I found your blog page, I really found you by mistake, while I was
    browsing on Digg for something else, Anyways I am here now
    and would just like to say many thanks for a marvelous post and a
    all round thrilling blog (I also love the theme/design), I don’t
    have time to read through it all at the minute but I
    have bookmarked it and also added your RSS feeds, so when I have time I will be back to read
    a great deal more, Please do keep up the great work.

网站地图