URL connection problem

M

Marcelo

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/...I/Lotus-Elise-II-02/Lotus-Elise-II-02_640.jpg

Do you have any ideas?

thanks a lot,

Marcelo
 
R

Roedy Green

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;
....

I would put in an explicit connect and fill in more of the parameters.

See http://mindprod.com/applets/fileio.html
for how.
 
B

bherbst65

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
 
H

hiwa

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();
 
M

Marcelo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top