How to download a zip file from the Internet using Java and save 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,count);
        }
        out.flush(); out.close(); in.close();                   

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

**When I compile it, I get the following error, but it's good if I use URL evrything directly in the browser

How can I solve it? Or is there another way to download the 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)

Solution

Reference Java docs:

Make sure your program is not blocked by a firewall or proxy

to update:

To configure an agent, follow Peter liljenberg's advice:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>