Dear Programmers,
I have a terrible headache because of this part of my code.
I don't have a clue how to disconnect from a given URL. I would like to
make a download of some images, but the problem is that the connection
may be lost or hanged up.
The url is correct, but the server seems to be down or something like
that for some urls (because it waits for ever...), however, my code get
stuck here:
urlObject = new URL(url);
URLConnection con = urlObject.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;MSIE
5.5; Windows NT 5.0;H010818)" );
int length = con.getContentLength();
<PROBLEM HERE !!!! >
int MAX_IMAGE_SIZE = length;
....
Some urls that gives me this problem are:
http://www.alientech.to/pic/exelixis03/Auto-Exelixis_045.JPG
http://www.auto-sfondi-desktop.com/Wallpapers_Lotus_/Lotus-Elise-II/Lotus-Elise-
II-02/Lotus-Elise-II-02_640.jpg
Do you have any ideas?
thanks a lot,
Marcelo
Roedy Green - 14 Nov 2005 01:54 GMT
>urlObject = new URL(url);
>URLConnection con = urlObject.openConnection();
[quoted text clipped - 5 lines]
>int MAX_IMAGE_SIZE = length;
>....
I would put in an explicit connect and fill in more of the parameters.
See http://mindprod.com/applets/fileio.html
for how.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
bherbst65@hotmail.com - 14 Nov 2005 01:55 GMT
Marcelo,
I need more information.
Is this an application or an applet.
Are you doing other operations with the images other than downloading
the image?
I have Win2K, a sample applet with buttons to click the specific
images up and safeguards to report error.
Not sure that would help
Bob
hiwa - 14 Nov 2005 03:44 GMT
> download of some images
Just call URL's openStream() method and simply read() on its returned
stream.
try {
in = new BufferedInputStream(url.openStream());
out = new BufferedOutputStream(new
FileOutputStream(outFile));
int value;
while ((value = in.read()) != -1) {
out.write(value);
}
out.flush();
Roedy Green - 14 Nov 2005 05:44 GMT
>http://www.alientech.to/pic/exelixis03/Auto-Exelixis_045.JPG
>http://www.auto-sfondi-desktop.com/Wallpapers_Lotus_/Lotus-Elise-II/Lotus-Elise-
II-02/Lotus-Elise-II-02_640.jpg
you can't load images from a server other than Mom with an Applet. It
must be signed. Or use an application.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Marcelo - 14 Nov 2005 08:54 GMT
Hi,
I guess I expressed myself badly. My program is an application, not an
Applet.
The problem is that my program waits for the server "for ever" and I
would like to change that. My code has the problem here:
urlObject = new URL(url);
URLConnection con = urlObject.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/4.0
(compatible;MSIE 5.5; Windows NT 5.0;H010818)" );
int length = con.getContentLength();
<PROBLEM HERE !!!! >
int MAX_IMAGE_SIZE = length;
....
Whenever I do the con.getContentLength(), Java waits for an answer. Now,
it seems that the server is down, How can I tell java to stop waiting
for the server?
thanks
Marcelo
Roedy Green - 14 Nov 2005 09:57 GMT
>Whenever I do the con.getContentLength(), Java waits for an answer. Now,
>it seems that the server is down, How can I tell java to stop waiting
>for the server?
check out system properties:
sun.net.client.defaultConnectTimeout
sun.net.client.defaultReadTimeout
see http://mindprod.com/jgloss/properties.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.