简体   繁体   中英

How to download zip file from internet using JAVA and saving it in a specific folder?

This is my code:

public static void downloadZipFile() {
    String saveTo = "C:\\Users\\aria\\Downloads\\Temp";
    try {
        URL url = new URL("http://www.bcfi.be/download/files/R1112B2_BcfiHtm.zip");
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        FileOutputStream out = new FileOutputStream(saveTo + "BcfiHtm.zip");
        byte[] b = new byte[1024];
        int count;
        while ((count = in.read(b)) >= 0) {
            out.write(b, 0, count);
        }
        out.flush(); out.close(); in.close();                   

    } catch (IOException e) {
        e.printStackTrace();
    }
}

**When i compile it i get following error but if i use url directly in browsers evrything is ok.

How can i fix it? or is there any other way to download zip file?**

java.net.UnknownHostException: www.bcfi.be
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
at sun.net.www.http.HttpClient.New(HttpClient.java:307)
at sun.net.www.http.HttpClient.New(HttpClient.java:324)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at be.azvub.ext.prismaFlex.Exterahelp.download.DownloadFile.downloadZipFile(DownloadFile.java:72)
at be.azvub.ext.prismaFlex.Exterahelp.download.DownloadFile.main(DownloadFile.java:37)

Quoting from the Java Docs:

Thrown to indicate that the IP address of a host could not be determined.

Make sure your program is not blocked by the Firewall or a proxy .

UPDATE:

To configure you proxy, do as Peter Liljenberg suggested:

You could pass the proxy information to the openConnection call in your code like this:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("pro",9999)); URLConnection conn = url.openConnection(proxy);

Since you're behind a proxy you can try some different approaches:

1) Add the proxy information to the JVM when starting:

java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber 
-Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword javaClassToRun

In your case it would probably be:

java -Dhttp.proxyHost=pro -Dhttp.proxyPort=9999  javaClassToRun

2) You could pass the proxy information to the openConnection call in your code like this:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("pro",9999));
URLConnection conn = url.openConnection(proxy);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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