• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.net.UnknownHostException

 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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


 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got no exception when I tried it.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you open the file in a browser? You might have a DNS issue, in which case you need to contact your ISP.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some one please assist me
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please assist
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Meet Gaurav wrote:Some one please assist me


Meet Gaurav wrote:Please assist


http://faq.javaranch.com/java/PatienceIsAVirtue
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic