java如何下载zip文件
使用 Java 下载 ZIP 文件的方法
使用 java.net.URL 和 java.nio.file 类
从 URL 下载 ZIP 文件并保存到本地路径,可以使用 java.net.URL 和 java.nio.file 类。以下是示例代码:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class DownloadZipFile {
public static void downloadFile(String fileURL, String savePath) throws IOException {
URL url = new URL(fileURL);
try (InputStream in = url.openStream()) {
Path targetPath = Path.of(savePath);
Files.copy(in, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
}
public static void main(String[] args) {
String fileURL = "https://example.com/file.zip";
String savePath = "downloaded_file.zip";
try {
downloadFile(fileURL, savePath);
System.out.println("File downloaded successfully.");
} catch (IOException e) {
System.err.println("Error downloading file: " + e.getMessage());
}
}
}
使用 Apache HttpClient
如果需要更高级的功能(如处理重定向或 HTTP 认证),可以使用 Apache HttpClient:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DownloadZipWithHttpClient {
public static void downloadFile(String fileURL, String savePath) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(fileURL);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream in = entity.getContent();
FileOutputStream out = new FileOutputStream(savePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
}
}
public static void main(String[] args) {
String fileURL = "https://example.com/file.zip";
String savePath = "downloaded_file.zip";
try {
downloadFile(fileURL, savePath);
System.out.println("File downloaded successfully.");
} catch (IOException e) {
System.err.println("Error downloading file: " + e.getMessage());
}
}
}
使用 Java 11+ 的 HttpClient
Java 11 引入了新的 HttpClient API,可以更简洁地实现下载:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class DownloadZipWithJava11HttpClient {
public static void downloadFile(String fileURL, String savePath) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(fileURL))
.build();
HttpResponse<Path> response = client.send(
request,
HttpResponse.BodyHandlers.ofFile(
Path.of(savePath),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE
)
);
if (response.statusCode() == 200) {
System.out.println("File downloaded successfully.");
} else {
System.err.println("Failed to download file: HTTP " + response.statusCode());
}
}
public static void main(String[] args) {
String fileURL = "https://example.com/file.zip";
String savePath = "downloaded_file.zip";
try {
downloadFile(fileURL, savePath);
} catch (Exception e) {
System.err.println("Error downloading file: " + e.getMessage());
}
}
}
注意事项
- 确保目标 URL 支持直接下载 ZIP 文件。
- 检查文件权限和磁盘空间是否足够。
- 对于大文件,建议使用缓冲流以提高效率。
- 添加适当的异常处理(如网络超时或文件写入失败)。






