[英]How to download a ZIp file from a URl and store them as Zip file only
I have a url like below 我有一个像下面的网址
http://blah.com/download.zip
I want a java code to download this Zip file from the URL and save it in my server directory as ZIP file only . 我想要一个java代码从URL下载这个Zip文件,并将其作为ZIP文件保存在我的服务器目录中。 I would also like to know what is the most effecient way to do this.
我还想知道最有效的方法是什么。
First, your URL is not http:\\\\blah.com\\download.zip
. 首先,您的URL 不是
http:\\\\blah.com\\download.zip
。 It is http://blah.com/download.zip
. 它是
http://blah.com/download.zip
。
Second, it is simple. 其次,很简单。 You have to perform HTTP GET request, take the stream and copy it to
FileOutputStream
. 您必须执行HTTP GET请求,获取流并将其复制到
FileOutputStream
。 Here is the code sample. 这是代码示例。
URL url = new URL("http://blah.com/download.zip");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream("download.zip");
copy(in, out, 1024);
out.close();
public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buf = new byte[bufferSize];
int n = input.read(buf);
while (n >= 0) {
output.write(buf, 0, n);
n = input.read(buf);
}
output.flush();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.