java方法文件下载到本地磁盘
发布人:shili8
发布时间:2025-01-11 10:13
阅读次数:0
**Java 方法:从网络下载文件并保存到本地磁盘**
在 Java 中,下载文件并保存到本地磁盘是一个常见的需求。下面是使用 Java 的方法来实现这一点。
###1. 使用 `HttpURLConnection` 类首先,我们可以使用 `HttpURLConnection` 类来下载文件。这个类提供了一个简单的方式来连接 HTTP服务器,并从其上下载资源。
javaimport java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadFile {
public static void downloadFile(String url, String filePath) throws Exception {
// 创建 URL 对象 URL fileUrl = new URL(url);
// 打开 HTTP 连接 HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection();
// 检查 HTTP 状态码 int responseCode = connection.getResponseCode();
if (responseCode ==200) {
// 获取文件大小 long fileSize = connection.getContentLengthLong();
// 创建输出流 FileOutputStream outputStream = new FileOutputStream(filePath);
// 下载文件 byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = connection.getInputStream().read(buffer)) != -1) {
outputStream.write(buffer,0, bytesRead);
}
// 关闭输出流 outputStream.close();
System.out.println("下载完成!文件大小:" + fileSize + "字节");
} else {
System.out.println("下载失败!");
}
}
public static void main(String[] args) throws Exception {
String url = " /> String filePath = "/path/to/downloaded/file.txt";
downloadFile(url, filePath);
}
}
###2. 使用 `OkHttp` 库如果你需要下载大文件或需要更高级的 HTTP 特性,可以使用 OkHttp 库。下面是示例代码:
javaimport ok />import ok />import ok />
public class DownloadFile {
public static void downloadFile(String url, String filePath) throws Exception {
// 创建 OkHttpClient 对象 OkHttpClient client = new OkHttpClient();
// 创建 Request 对象 Request request = new Request.Builder()
.url(url)
.build();
// 发送请求并获取响应 Response response = client.newCall(request).execute();
// 检查 HTTP 状态码 if (response.code() ==200) {
// 获取文件大小 long fileSize = response.body().contentLength();
// 创建输出流 FileOutputStream outputStream = new FileOutputStream(filePath);
// 下载文件 byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = response.body().byteStream().read(buffer)) != -1) {
outputStream.write(buffer,0, bytesRead);
}
// 关闭输出流 outputStream.close();
System.out.println("下载完成!文件大小:" + fileSize + "字节");
} else {
System.out.println("下载失败!");
}
}
public static void main(String[] args) throws Exception {
String url = " /> String filePath = "/path/to/downloaded/file.txt";
downloadFile(url, filePath);
}
}
###3. 使用 `Apache HttpClient` 库最后,我们可以使用 Apache HttpClient 库来下载文件。下面是示例代码:
javaimport org.apache. />import org.apache. />import org.apache. />import org.apache. />import org.apache. />
public class DownloadFile {
public static void downloadFile(String url, String filePath) throws Exception {
// 创建 HttpClient 对象 CloseableHttpClient client = HttpClients.createDefault();
// 创建 GET 请求 HttpGet request = new HttpGet(url);
// 发送请求并获取响应 HttpResponse response = client.execute(request);
// 检查 HTTP 状态码 if (response.getStatusLine().getStatusCode() ==200) {
// 获取文件大小 long fileSize = response.getEntity().getContentLength();
// 创建输出流 FileOutputStream outputStream = new FileOutputStream(filePath);
// 下载文件 byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = response.getEntity().getContent().read(buffer)) != -1) {
outputStream.write(buffer,0, bytesRead);
}
// 关闭输出流 outputStream.close();
System.out.println("下载完成!文件大小:" + fileSize + "字节");
} else {
System.out.println("下载失败!");
}
}
public static void main(String[] args) throws Exception {
String url = " /> String filePath = "/path/to/downloaded/file.txt";
downloadFile(url, filePath);
}
}
以上是使用 Java 的方法来从网络下载文件并保存到本地磁盘的示例代码。

