guys! 我有问题!我试图下载一个.zip(大小为150 MB),使用此代码从Internet文件:如何用java下载url的.zip文件
public void downloadBuild(String srcURL, String destPath, int bufferSize, JTextArea debugConsole) throws FileNotFoundException, IOException {
debugConsole.append(String.format("**********Start process downloading file. URL: %s**********\n", srcURL));
try {
URL url = new URL(srcURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.connect();
in = httpConn.getInputStream();
out = new FileOutputStream(destPath);
byte buffer[] = new byte[bufferSize];
int c = 0;
while ((c = in.read(buffer)) > 0) {
out.write(buffer, 0, c);
}
out.flush();
debugConsole.append(String.format("**********File. has been dowloaded: Save path is: %s********** \n", destPath));
} catch (IOException e) {
debugConsole.append(String.format("**********Error! File was not downloaded. Detail: %s********** \n", e.toString()));
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
}
但该文件没有完全下载。 (只有4000字节)。我究竟做错了什么?
你为什么使用http post request?使用此下载http://stackoverflow.com/questions/18872611/download-file-from-server-in-java –
不是说它解决任何问题,但你不应该忽略例外。总是至少打印他们的堆栈跟踪。 – Pshemo
但我在我的程序中没有异常,但文件文件没有完全下载。 –