我在Windows 7上成功使用以下代码中的“org.apache.commons.io.FileUtils”下载mp3和图像文件:
FileUtils.copyURLToFile(new URL(urlString),myFile);
但是在mac os x Yosemite版本10.10.2(14C1514)上,我在几次下载后得到了这个例外:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:792)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:789)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
我尝试过添加超时
FileUtils.copyURLToFile(new URL(url),myFile ,60000,60000);
甚至使用其他方法
URLConnection conn = new URL(url).openConnection();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(myFile);
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
更新
我尝试禁用WiFi(以避免@john提到的任何连接错误)并使用有线以太网,但在几次下载后仍然有相同的错误,感谢任何帮助。
答案 0 :(得分:0)
这与ISP有关,问题已解决。