Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

URL connection problem

Thread view: 
Marcelo - 13 Nov 2005 20:27 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.