java下载zip文件
时间: 2023-11-29 18:36:31 浏览: 40
你可以使用Java的URLConnection类来下载一个zip文件。以下是一个示例代码:
```java
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadZipFile {
public static void main(String[] args) throws IOException {
String fileUrl = "http://example.com/example.zip";
String saveDir = "/path/to/save/directory/";
String fileName = "example.zip";
URL url = new URL(fileUrl);
URLConnection conn = url.openConnection();
BufferedInputStream inputStream = new BufferedInputStream(conn.getInputStream());
FileOutputStream outputStream = new FileOutputStream(saveDir + fileName);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded successfully!");
}
}
```
在这个例子中,你需要设置文件的下载链接(fileUrl)、要保存的目录(saveDir)和文件名(fileName)。程序将会从链接下载zip文件并保存在指定的目录下。
阅读全文