这是我的代码:
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 evrything就可以了。
我该如何解决?或者还有其他方法可以下载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)
答案 0 :(得分:7)
从Java Docs引用:
抛出以指示主机的IP地址不能 确定。
确保您的程序未被防火墙或代理阻止。
<强>更新强>
要配置代理,请按照Peter Liljenberg的建议:
您可以将代理信息传递给openConnection调用 你的代码是这样的:
代理代理=新代理(Proxy.Type.HTTP,new 如果InetSocketAddress( “亲”,9999)); URLConnection conn = url.openConnection(代理);
答案 1 :(得分:6)
由于您在代理服务器后面,您可以尝试一些不同的方法:
1)启动时将代理信息添加到JVM:
java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword javaClassToRun
在你的情况下,它可能是:
java -Dhttp.proxyHost=pro -Dhttp.proxyPort=9999 javaClassToRun
2)您可以将代理信息传递给代码中的openConnection调用,如下所示:
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("pro",9999));
URLConnection conn = url.openConnection(proxy);