Java 下载 Response 的几种方式
在 Java 中,从服务器下载响应内容可以通过多种方式实现,以下是几种常见的方法:

使用 HttpURLConnection 下载文件
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDownload {
public static void downloadFile(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try (InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(savePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
} finally {
connection.disconnect();
}
}
}
使用 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;
public class HttpClientDownload {
public static void downloadFile(String fileUrl, String savePath) throws Exception {
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);
}
}
}
}
}
}
}
使用 Java 11+ 的 HttpClient 下载文件
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Java11HttpClientDownload {
public static void downloadFile(String fileUrl, String savePath) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(fileUrl))
.build();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
Path path = Paths.get(savePath);
try (InputStream in = response.body();
OutputStream out = Files.newOutputStream(path)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
下载并显示进度
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class ProgressDownload {
public static void downloadWithProgress(String fileUrl, String savePath) throws IOException {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int fileSize = connection.getContentLength();
try (InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(savePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
long totalBytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
int progress = (int) ((totalBytesRead * 100) / fileSize);
System.out.printf("Download progress: %d%%\n", progress);
}
} finally {
connection.disconnect();
}
}
}
注意事项
- 异常处理:实际应用中需要更完善的异常处理
- 资源管理:确保所有流和连接都正确关闭
- 大文件处理:对于大文件,考虑使用缓冲区优化
- 网络超时:设置合理的连接和读取超时
- 认证:如果需要认证,添加相应的认证头信息
- 并发下载:对于大文件,可以考虑分块并发下载
方法可以根据你的具体需求和环境选择使用,Java 11+ 的 HttpClient 是目前最现代和推荐的方式。

