在 Java 中进行文件下载有多种方式,其中一些主要的方式包括使用 Java 标准库(java.net 包)、Apache HttpClient 库以及 Spring 框架中的 RestTemplate 等。下面我将详细介绍每种方式的步骤流程,并提供示例代码和依赖坐标。
步骤流程:
示例代码:
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class FileDownloadUsingJavaNet {
public static void main(String[] args) {
String fileUrl = "https://example.com/sample.pdf";
String savePath = "downloaded_file.pdf";
try {
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
OutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
System.out.println("File downloaded successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
依赖坐标(Maven):
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
步骤流程:
示例代码:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileDownloadUsingHttpClient {
public static void main(String[] args) {
String fileUrl = "https://example.com/sample.pdf";
String savePath = "downloaded_file.pdf";
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(fileUrl);
try {
HttpResponse response = httpClient.execute(httpGet);
InputStream inputStream = response.getEntity().getContent();
OutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
System.out.println("File downloaded successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
依赖坐标(Maven):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.5</version>
</dependency>
步骤流程:
getForObject
方法下载文件,将文件内容保存到字节数组。示例代码:
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDownloadUsingRestTemplate {
public static void main(String[] args) {
String fileUrl = "https://example.com/sample.pdf";
String savePath = "downloaded_file.pdf";
RestTemplate restTemplate = new RestTemplateBuilder().build();
ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(fileUrl, byte[].class);
byte[] fileContent = responseEntity.getBody();
try (FileOutputStream outputStream = new FileOutputStream(savePath)) {
if (fileContent != null) {
outputStream.write(fileContent);
System.out.println("File downloaded successfully.");
} else {
System.out.println("Failed to download file.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是三种常见的 Java 文件下载方法的示例代码和步骤流程。你可以根据你的需求选择其中一种方式来下载文件。注意替换示例中的 URL 和保存路径以适应你的具体需求。