java 如何下载文件
使用 Java 下载文件的方法
使用 java.net.URL 和 java.nio.file.Files
通过 URL 类打开连接,并使用 Files.copy 方法将数据流写入本地文件。

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileDownloader {
public static void downloadFile(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
try (InputStream in = url.openStream()) {
Files.copy(in, Paths.get(savePath));
}
}
}
使用 HttpURLConnection 处理 HTTP 请求
对于需要设置请求头或处理 HTTP 响应的场景,HttpURLConnection 更灵活。

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDownloader {
public static void downloadFile(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
try (InputStream in = httpConn.getInputStream();
FileOutputStream out = new FileOutputStream(savePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
使用 Apache HttpClient 库
第三方库如 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;
public class ApacheHttpDownloader {
public static void downloadFile(String fileUrl, String savePath) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet(fileUrl);
try (CloseableHttpResponse response = client.execute(request)) {
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);
}
}
}
}
}
}
}
使用 Java 11 的 HttpClient(JDK 内置)
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;
public class Java11HttpDownloader {
public static void downloadFile(String fileUrl, String savePath) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(fileUrl))
.build();
client.send(request, HttpResponse.BodyHandlers.ofFile(Path.of(savePath)));
}
}
注意事项
- 异常处理:确保捕获
IOException或其他可能的异常,避免程序崩溃。 - 大文件下载:对于大文件,使用缓冲区和分块读写以提高效率。
- HTTPS 支持:若下载 HTTPS 链接,需确保 JVM 信任证书或配置 SSL 上下文。
- 进度监控:可通过计算已下载字节数与总文件大小的比例实现进度显示。
示例调用
public class Main {
public static void main(String[] args) {
try {
FileDownloader.downloadFile("http://example.com/file.zip", "downloaded.zip");
} catch (IOException e) {
e.printStackTrace();
}
}
}






