1

I Wanted to download one zip file from one URL, for that I Used the below code to open an URL and download one zip file from .But what happens is I am getting following exception

java.net.UnknownHostException: www.abc.com

So I just did some reasearch and guessed that could be the problem of certficate and generated the certificate by using below keytool command

keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048

After that I set the location of generated keystore.jks file by using the following code

 System.setProperty("javax.net.ssl.trustStore","C:\\Programme\\Java\\jdk1.6.0_31\\jre\\bin\\keystore.jks");

After running the code, still I am getting the same exception

java.net.UnknownHostException: www.abc.com 

Any idea how to ressolve? I am able to access this site from the Browser.

My full code below:

import java.io.*;
import java.net.*;

public class UrlDownload {
    final static int size = 1024;

    public static void fileUrl(String fAddress, String localFileName,
            String destinationDir) {
        OutputStream outStream = null;
        URLConnection uCon = null;

        InputStream is = null;
        try {
            URL Url;
            byte[] buf;
            int ByteRead, ByteWritten = 0;
            Url = new URL(fAddress);
            outStream = new BufferedOutputStream(new FileOutputStream(
                    destinationDir + "\\" + localFileName));

            uCon = Url.openConnection();
            is = uCon.getInputStream();
            buf = new byte[size];
            while ((ByteRead = is.read(buf)) != -1) {
                outStream.write(buf, 0, ByteRead);
                ByteWritten += ByteRead;
            }
            System.out.println("Downloaded Successfully.");
            System.out.println("File name:\"" + localFileName
                    + "\"\nNo ofbytes :" + ByteWritten);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void fileDownload(String fAddress, String destinationDir) {
        int slashIndex = fAddress.lastIndexOf('/');
        int periodIndex = fAddress.lastIndexOf('.');

        String fileName = fAddress.substring(slashIndex + 1);

        if (periodIndex >= 1 && slashIndex >= 0
                && slashIndex < fAddress.length() - 1) {
            fileUrl(fAddress, fileName, destinationDir);
        } else {
            System.err.println("path or file name.");
        }
    }

    public static void main(String[] args) {

        String url = "http://www.abc.com/coolsolutions/tools/downloads/ntradping.zip";
        System.setProperty("javax.net.ssl.trustStore","C:\\Programme\\Java\\jdk1.6.0_31\\jre\\bin\\keystore.jks");
            String destAddress = "C:\\downloads";
                fileDownload(url,destAddress);

        } 
    }

I tried the below code. That throws unknown host exception.

InetAddress inetAddress = InetAddress.getByName("http://www.google.com");
    String ipAddress = inetAddress.getHostAddress().toString()'
    System.out.println(ipAddress );
13
  • 1
    Well UnknownHostException is quite specific. Are you sure that your computer can reach that host? Ever tried to do a nslookup on that? By the way: using a self signed certificate in your trust store would not help with the connection at all as the remote host probably would not trust your certificate for establishing the connection. Commented Nov 6, 2013 at 9:49
  • Is this website your own, I mean hosted on your local system ?. Java docs for UnknownHostException says "Thrown to indicate that the IP address of a host could not be determined." Commented Nov 6, 2013 at 9:52
  • Your code should work correctly. I've tried to download the file that was in your source but it was not found (try to replace it with: docs.oracle.com/javase/tutorial/figures/uiswing/components/…) and it will work. Another thing that I suggest is to use try with resource (that is if you are using JDK 7) - that will eliminate the need for finally. Commented Nov 6, 2013 at 9:58
  • I have tried this code from my office. Not from home. so any possibility that I have to do something else in my proxy or anything like that to make if work? I am able to access that particular url through Browser and download zip file from there. But When it comes to java code, I get this exception. Commented Nov 6, 2013 at 10:08
  • For me the URL : accessing the zip-url redirects to another page with the message that the page was not found. Is any authentication required to fetch the same? Commented Nov 6, 2013 at 10:22

1 Answer 1

0

Cause of this Exception according to javadocs is, Thrown to indicate that the IP address of a host could not be determined. So check the fAddress String.

try passing "http://www.novell.com/home/" in your fAddress variable insteat "www.novell.com"

Hope it helps..

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that one as well. it was not working. Also I have written 'InetAddress inetAddress = InetAddress.getByName("google.com")'; which also causes the same unknown exception. This I tried to check my system configuration whether I can access any external site through java code. I think it is the problem of my system setting. Not able to access external url thorugh java code. Any idea how to make it work? Any proxy or host file change or something like that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.