gpt4 book ai didi

java - 如何使用 JAVA 从 Internet 下载 zip 文件并将其保存在特定文件夹中?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:30:51 26 4
gpt4 key购买 nike

这是我的代码:

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

**当我编译它时出现以下错误,但如果我直接在浏览器中使用 url,一切正常。

我该如何解决?或者有没有其他方法可以下载 zip 文件?**

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)

最佳答案

引用自 Java 文档:

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

确保您的程序未被防火墙代理阻止。

更新:

要配置代理,请按照 Peter Liljenberg 的建议进行操作:

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

关于java - 如何使用 JAVA 从 Internet 下载 zip 文件并将其保存在特定文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8848966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com