java下载zip文件
时间: 2023-11-29 20:34:21 浏览: 34
你可以使用Java中的URLConnection和BufferedInputStream类来下载zip文件。以下是一个简单的示例代码:
```
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class DownloadZipFile {
public static void main(String[] args) {
String zipUrl = "https://example.com/file.zip"; // 要下载的zip文件的URL
String savePath = "/path/to/save/file.zip"; // 保存zip文件的路径和文件名
try {
URL url = new URL(zipUrl);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(savePath);
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
fileOutputStream.close();
bufferedInputStream.close();
System.out.println("Zip file downloaded successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,我们使用URLConnection打开zip文件的URL连接并获取输入流。我们使用BufferedInputStream类读取输入流的内容,并使用FileOutputStream将其写入本地zip文件中。最后,我们关闭所有打开的流并输出成功消息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)