如何使用java从URL下载.zip文件
伙计们! 我有个问题!我正在尝试使用以下代码从Internet下载.zip(大小为150 mb)文件:如何使用java从URL下载.zip文件,java,Java,伙计们! 我有个问题!我正在尝试使用以下代码从Internet下载.zip(大小为150 mb)文件: public void downloadBuild(String srcURL, String destPath, int bufferSize, JTextArea debugConsole) throws FileNotFoundException, IOException { debugConsole.append(String.format("**********Start p
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字节)。我做错了什么
FileOutputStream("example.zip").getChannel().transferFrom(Channels.newChannel(new URL("http://www.example.com/example.zip").openStream()), 0, Long.MAX_VALUE);
简单的一行。欲了解更多信息,请阅读
简单的一行。欲了解更多信息,请阅读
简单的一行。欲了解更多信息,请阅读
简单的一行。有关详细信息,请阅读您可以使用以下代码从给定的uri路径下载并提取zip文件
URL url = new URL(uriPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry entry = zipIn.getNextEntry();
while(entry != null) {
System.out.println(entry.getName());
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
System.out.println("===File===");
} else {
System.out.println("===Directory===");
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
您可以使用以下代码从给定的uri路径下载并提取zip文件
URL url = new URL(uriPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry entry = zipIn.getNextEntry();
while(entry != null) {
System.out.println(entry.getName());
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
System.out.println("===File===");
} else {
System.out.println("===Directory===");
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
您可以使用以下代码从给定的uri路径下载并提取zip文件
URL url = new URL(uriPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry entry = zipIn.getNextEntry();
while(entry != null) {
System.out.println(entry.getName());
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
System.out.println("===File===");
} else {
System.out.println("===Directory===");
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
您可以使用以下代码从给定的uri路径下载并提取zip文件
URL url = new URL(uriPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
ZipInputStream zipIn = new ZipInputStream(in);
ZipEntry entry = zipIn.getNextEntry();
while(entry != null) {
System.out.println(entry.getName());
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
System.out.println("===File===");
} else {
System.out.println("===Directory===");
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
为什么使用http post请求?使用此选项下载,不是因为它解决了任何问题,而是您不应该忽略异常。始终至少打印堆栈跟踪。但我的程序中没有异常,但文件未完全下载。为什么使用http post请求?使用此选项下载,不是因为它解决了任何问题,而是您不应该忽略异常。始终至少打印堆栈跟踪。但我的程序中没有异常,但文件未完全下载。为什么使用http post请求?使用此选项下载,不是因为它解决了任何问题,而是您不应该忽略异常。始终至少打印堆栈跟踪。但我的程序中没有异常,但文件未完全下载。为什么使用http post请求?使用此选项下载,不是因为它解决了任何问题,而是您不应该忽略异常。始终至少打印他们的堆栈跟踪。但我的程序中没有异常,但文件没有完全下载。与其中一条评论的答案相同?@KonstantinosChalkias如果你觉得这个问题在另一篇文章中得到了回答,你应该标记这个问题并将其标记为重复,指定它是哪个文章的重复。除此之外,评论不是答案,所以这很好。如果你觉得有人可能会窃取你的评论,你应该避免在评论部分回答问题。这对我有用。。与其中一条评论的答案相同?@KonstantinosChalkias如果你觉得这个问题在另一篇帖子中得到了回答,你应该标记这个问题并将其标记为重复,指定它是哪个帖子的重复。除此之外,评论不是答案,所以这很好。如果你觉得有人可能会窃取你的评论,你应该避免在评论部分回答问题。这对我有用。。与其中一条评论的答案相同?@KonstantinosChalkias如果你觉得这个问题在另一篇帖子中得到了回答,你应该标记这个问题并将其标记为重复,指定它是哪个帖子的重复。除此之外,评论不是答案,所以这很好。如果你觉得有人可能会窃取你的评论,你应该避免在评论部分回答问题。这对我有用。。与其中一条评论的答案相同?@KonstantinosChalkias如果你觉得这个问题在另一篇帖子中得到了回答,你应该标记这个问题并将其标记为重复,指定它是哪个帖子的重复。除此之外,评论不是答案,所以这很好。如果你觉得有人会偷我的东西,你应该避免回答评论部分的问题这对我有用。。