简体   繁体   English

java未知主机异常无法解决

[英]java unknown host exception not able to ressolve

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 我想从一个URL下载一个zip文件,为此,我使用下面的代码打开了一个URL并从其中下载了一个zip文件。

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命令进行证书生成证书的问题

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 之后,我使用以下代码设置生成的keystore.jks文件的位置

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

Cause of this Exception according to javadocs is, Thrown to indicate that the IP address of a host could not be determined. 根据javadocs引起此异常的原因是,抛出该异常表示无法确定主机的IP地址。 So check the fAddress String. 因此,请检查fAddress字符串。

try passing " http://www.novell.com/home/ " in your fAddress variable insteat "www.novell.com" 尝试通过“ http://www.novell.com/home/ ”你fAddress变量insteat“www.novell.com”

Hope it helps.. 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM