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

check for the existence of image file on webpage prior to download

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Dear Java Experts,

I am looking for a way to check for the existence of an image file that reside on the remote webserver. Below is the Java statements that has successfully downloaded
http://images.abc.com/img/2012102/15509/2010035044_1_PM.JPG to my local Windows 7 PC:


However, there are additional images under the same URI available but require checking for their existence prior to downloading them. For instance,

As a result, is it possible to use URLConnection.getContentLength() or other approach to determine its connection status / existence to avoid getting an
IOException in the event that say http://images.abc.com/img/2012102/15509/2010035044_5_PM.JPG does not exist? There may be other ways to achieve the same
objective that I haven't thought of.

Thanks a lot,

Jack
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

Looks like you are building a FTP type program looking/downloading files on some remote server. Have you consider using FTP library like JSCH? Will make life much easier.

Of course once files are downloaded, you can do whatever you like with them.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Thank you K. Tsang for your suggestion but need your clarification on whether JSCH FTP can access files via URL HTTP protocol. My experience with FTP in
general is that it requires user authentication compared to URLConnection / ImageIO.

It would be excellent if you could provide / refer to an example which uses JSCH to download URL document.

My Google searches on JSCH have not found sufficient example on how it is use in this context. On the other hand, there are plenty of CURL / libCurl, JavacURL
material but not sure which is a better tool to use.

I am only interested in downloading a few advertisement images (small size JPEG) but needs to validate their existence other additional related files not published,
to avoid bringing down the primary Java application altogether.

Thanks again,

Jack
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jack Bush wrote: Thank you K. Tsang for your suggestion but need your clarification on whether JSCH FTP can access files via URL HTTP protocol


There's no such thing as "URL HTTP protocol".

A URL is a way of locating a resource. It has nothing to do with the protocol for retrieving that resource. We can have an FTP URL or an HTTP URL or a file URL, etc.

To transfer the file, you can use the FTP protocol, or you can use the HTTP protocol, or you can use some other protocol. However, to see if the file exists, you will not, in the general case, be able to use the HTTP protocol. It's possible in some cases, and if you control the server, you may be able to configure it to allow you to find out, but it's not something you should generally rely on.

And there's not usually a reason to check if it exists before attempting a download. Just try the download, and if it fails, handle the failure. You'll have to do that anyway even if "exists" returns true. If you're lucky, the failure will include an indication of whether it was because the file doesn't exist or for some other reason.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Thanks Jeff for your clarification, I recognise the differences between FTP URL and HTTP URL and have tried converting the existing HTTP URL http://images.abc.com/img/2012102/15509/2010035044_1_PM.JPG
to ftp://images.abc.com/img/2012102/15509/2010035044_1_PM.JPG in order to download it using FTP but failed to find it. My challenge is either to convert the existing HTTP URL to FTP one, or to
use the right download tool / libraries that could take the HTTP URL. Also not sure how to handle a download failure either. Looks like there are a lot of missing puzzles that needs to be
enlightened on since I am new to download in Java, a little example or reference material goes a long way.

Your patience is very much appreciated.

Many thanks,

Jack
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jack Bush wrote: Thanks Jeff for your clarification, I recognise the differences between FTP URL and HTTP URL and have tried converting the existing HTTP URL http://images.abc.com/img/2012102/15509/2010035044_1_PM.JPG
to ftp://images.abc.com/img/2012102/15509/2010035044_1_PM.JPG in order to download it using FTP but failed to find it. My challenge is either to convert the existing HTTP URL to FTP one,


That will only be possible in certain special circumstances. To wit:

1. There will have to be an FTP server running serving that provides access to the resources you want. Are you certain that there will be one?

2. There will have to be a known voncertion between the HTTP location and the FTP one. There's no reason to assume that the FTP service will be rooted at the same file system location on the server as the HTTP server is, and there's no way in general to find out where either one is relative to the remote file system or to each other. So you'll have to know ahead of time how the FTP server and HTTP server have been configured.

use the right download tool / libraries that could take the HTTP URL.


You can't just assume that there exists a corresponding FTP path to any given HTTP resource. Unless you know that an FTP server is running and know how it's configured, you'll have to use HTTP. There are HTTP clients out there. Apache has a decent one I believe. Even the classes provided in the core API are okay for simple stuff.

Also not sure how to handle a download failure either.


At the moment that's not a Java question. It's a requirements question. You're basic options are 1) Try again, and 2) Throw an exception.

a little example or reference material goes a long way.


I'm sure a google search for something like java http download example will turn up something useful.

Good luck!

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Thanks Jeff for your suggestion,

Jack
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: check for the existence of image file on webpage prior to download