Wie zip-Datei herunterladen aus dem internet mit Hilfe von JAVA und speichern Sie Sie in einem bestimmten Ordner?
Das ist mein 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();
}
}
**Wenn ich es kompilieren bekomme ich folgenden Fehler wenn ich aber die url direkt im Browser evrything ist ok.
Wie kann ich es beheben? oder gibt es eine andere Möglichkeit zum download als zip-Datei?**
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)
- Es ist kein Java-problem, scheint, wie Sie haben ein problem mit der Netzwerk-lookup. Bist du hinter einem proxy?
- Können Sie zum download über einen browser? Ich könnte, aber ich hoffe, dass die zip-Datei nicht enthalten sensible Daten.
- In meinem Fall hab ich die Verbindung Mal aus. Unterhalb Lösung hat mir geholfen, tooo.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Zitat aus dem Java-Docs:
Stellen Sie sicher, dass Ihr Programm wird nicht blockiert durch die Firewall oder eine proxy.
UPDATE:
Konfigurieren Sie proxy verwenden, vergessen Sie, wie Peter Liljenberg vorgeschlagen:
Da bist du hinter einem proxy können Sie versuchen, einige unterschiedliche Ansätze:
1) Fügen Sie die proxy-Informationen, die der JVM beim Start:
In deinem Fall wäre es wohl:
2) könnte passieren Sie die proxy-Informationen, um die openConnection-rufen Sie in Ihrem code wie folgt: