繁体   English   中英

如何从URl下载ZIp文件并将它们存储为Zip文件

[英]How to download a ZIp file from a URl and store them as Zip file only

我有一个像下面的网址

  http://blah.com/download.zip

我想要一个java代码从URL下载这个Zip文件,并将其作为ZIP文件保存在我的服务器目录中。 我还想知道最有效的方法是什么。

首先,您的URL 不是 http:\\\\blah.com\\download.zip 它是http://blah.com/download.zip

其次,很简单。 您必须执行HTTP GET请求,获取流并将其复制到FileOutputStream 这是代码示例。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM