java下载zip文件
时间: 2023-11-29 11:09:26 浏览: 75
你可以使用Java中的URLConnection类来下载zip文件。以下是一段示例代码:
```java
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadZipFile {
public static void main(String[] args) throws IOException {
String url = "http://example.com/example.zip";
String saveFilePath = "example.zip";
URLConnection conn = new URL(url).openConnection();
conn.connect();
InputStream inputStream = conn.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath);
byte[] data = new byte[1024];
int count;
while ((count = bufferedInputStream.read(data, 0, 1024)) != -1) {
fileOutputStream.write(data, 0, count);
}
fileOutputStream.flush();
fileOutputStream.close();
bufferedInputStream.close();
inputStream.close();
System.out.println("Zip file downloaded successfully.");
}
}
```
你只需要将`url`变量替换为要下载的zip文件的URL,将`saveFilePath`变量替换为要保存zip文件的本地路径和文件名,然后运行这段代码即可下载zip文件。
阅读全文