wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes java.net.UnknownHostException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.net.UnknownHostException" Watch "java.net.UnknownHostException" New topic
Author

java.net.UnknownHostException

Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Hi

Am trying to download this file. But am getting java.net.UnknownHostException. Could some one please assist me to resolve this one.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
I got no exception when I tried it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Can you open the file in a browser? You might have a DNS issue, in which case you need to contact your ISP.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Thanks, I tried it from my home.. And its working fine.

Whether I need to change

byte data[] = new byte[1024];

To download very big files.
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
I will second that, you might be having a DNS issue at the location you tried earlier. See if you can access http://java.sun.com from that machine which is running this code. So, incase you are running the code from some remote UNIX/Linux box, run it from that box and not the client machine.


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Meet Gaurav wrote:Whether I need to change

byte data[] = new byte[1024];

To download very big files.


Not really, the buffer size doesn't really matter. A larger buffer size might improve the speed of your transfer in some cases, you could try it. But the real problem here is that your code is wrong. Here's your copying loop:


So what you do is, you call the read() method. It returns the number of bytes it read, which may be any number between 0 and 1024. If it returns -1 then you're at EOF so you want to exit, which you do.

Now you're inside the loop and you know that some number of bytes were read. But you threw away the exact number and you're writing 1024 bytes regardless. One day this is going to come back and bite you.

So what your code should look like is this:
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Am getting the unhost exception again while trying to download a text file from the application server. Whether authentication is required ?
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Some one please assist me
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

"UnknownHostException" means that your computer can't find the numeric IP address for this machine name. Generally the computer has to ask something called a "DNS server" for this information. As someone said above, this sounds like a problem with your DNS server -- meaning your local network or your ISP is having a problem. In any case, it's completely unrelated to your Java code, which is fine (with Paul's changes, of course.) Talk to your local network administrator to get this straightened out.


[Jess in Action][AskingGoodQuestions]
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
However this code is not working, When I tried to download a file from Application server.

for downloading a file from the application server, Which not requires any internet connection rite. Then wats the reason am getting unknownhostexception.

Please assist me
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Please assist
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

UnknownHostException means that the name you're using for the server is unknown to your computer, whether the server is across the Internet or across the room. This has nothing to do with your code, and everything to do with your computer/network configuration. You need to talk to your local network techs to help you sort this out -- there's nothing anybody here can do.

And if you told us right away that the problem was not with downloading a file from java.sun.com, but from something on your own LAN, then we would have reached this conclusion much sooner!
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
In case if we are talking about the application server whether it needs any Authentication to access the files. If so how to pass the userid and password in the above program.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Meet Gaurav wrote:Some one please assist me

Meet Gaurav wrote:Please assist

http://faq.javaranch.com/java/PatienceIsAVirtue
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
Sorry Rob

I have a different query now. After downloading a txt file from the server. I lost the text alignment

Any way to getback the alignment while downloading
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Lost the alignment? What does that mean?

Can you explain what you were looking at before and what you're looking at now, and how they differ? And please: if you were looking at the file in a browser before, for example, tell us that. Don't tell us something completely different, like you were doing earlier in the thread. Just the facts, please.
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492

For example just take a bank statement its a simple txt file with proper alignment (newline, spacing) like month, date credit and debit. After downloading the file to client machine it seems to be mis alignment(everything came to single line with trimed spaces)

Anyway to copy the file directly from the server with proper alignment
 
wood burning stoves
 
subject: java.net.UnknownHostException
 
Similar Threads
Download a file from a https: website
write file from URL problem
Update applet text runtime
403 Error while download a file using java from Solaris OS
PUT request method not working