aspose file tools*
The moose likes Java in General and the fly likes Null check on an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Null check on an image" Watch "Null check on an image" New topic
Author

Null check on an image

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I am reading an image directly from url. I am using this image in web service. Now if the image is not available my service breaks and if the image is available my service is working fine. I am wondering if there is a way I can check if the image is exists or not. I would appreciate if someone can help me to solve this issue.

Here is my sample code How I read the image:

Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 85
sorry if this is too basic and I misunderstood the question, but couldn't lines 2-7 simply be wrapped in:

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
I tried that before I posted my request. It does not work that way. Thanks for your idea, anyway.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Sam Saha wrote:Now if the image is not available my service breaks

According to the javadocs, ImageIO.read throws an IOException if there's a problem reading from the URL. If you catch that you can execute whatever alternative code is appropriate.
Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 85
not sure what kind of try/catch your code is surrounded with, but I was able to get this to work:



when the bad url is uncommented, it throws an IO exception and just skips the null check...so maybe you just need code to deal with a non-existent image in the catch block.
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
well yes Lester Burnham was right.
Sometimes it appeared to be exception once we hit the broken URL.
or we may say the URL is not valid.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null check on an image