java如何进行文件下载呢?
下文笔者讲述java文件下载的方法及示例分享
java下载文件的实现思路:
使用java.net.URL和java.nio包
及使用Apache Commons IO库
使用方法大全:
**使用 `java.net.URL` 和 `java.io`**:适用于简单的文件下载。
**使用 `java.nio`**:提供更现代和高效的文件操作方式。
**使用 Apache Commons IO**:提供简洁的文件操作方法,适合快速开发。
**使用 `java.net.HttpURLConnection`**:适用于需要处理复杂 HTTP 请求的场景
例
方法一:使用 `java.net.URL` 和 `java.io`
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class FileDownloader {
public static void downloadFile(String fileURL, String saveDir) {
try {
URL url = new URL(fileURL);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fis = new FileOutputStream(saveDir);
byte[] buffer = new byte[1024];
int count;
while ((count = bis.read(buffer, 0, 1024)) != -1) {
fis.write(buffer, 0, count);
}
fis.close();
bis.close();
System.out.println("File downloaded successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileURL = "https://example.com/sample.pdf";
String saveDir = "downloaded_sample.pdf";
downloadFile(fileURL, saveDir);
}
}
方法二:使用`java.nio`
`java.nio` 包提供了更现代和高效的文件操作方式
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileDownloaderNIO {
public static void downloadFile(String fileURL, String saveDir) {
try (ReadableByteChannel rbc = Channels.newChannel(new URL(fileURL).openStream());
FileOutputStream fos = new FileOutputStream(saveDir)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
System.out.println("File downloaded successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileURL = "https://example.com/sample.pdf";
String saveDir = "downloaded_sample_nio.pdf";
downloadFile(fileURL, saveDir);
}
}
方法三:使用Apache Commons IO
Apache Commons IO 库提供了更简洁的文件操作方法。
引入依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class FileDownloaderApacheIO {
public static void downloadFile(String fileURL, String saveDir) {
try {
URL url = new URL(fileURL);
File destination = new File(saveDir);
FileUtils.copyURLToFile(url, destination);
System.out.println("File downloaded successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileURL = "https://java265.com/sample.pdf";
String saveDir = "downloaded_sample_apacheio.pdf";
downloadFile(fileURL, saveDir);
}
}
方法四:使用 `java.net.HttpURLConnection`
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileDownloaderHttpURLConnection {
public static void downloadFile(String fileURL, String saveDir) {
try {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// 检查响应码是否为 HTTP_OK
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// 从 Content-Disposition 头中提取文件名
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
} else {
// 如果没有 Content-Disposition 头,则从 URL 中提取文件名
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// 打开输入流
BufferedInputStream inputStream = new BufferedInputStream(httpConn.getInputStream());
FileOutputStream outputStream = new FileOutputStream(saveDir);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileURL = "https://example.com/sample.pdf";
String saveDir = "downloaded_sample_httpurlconnection.pdf";
downloadFile(fileURL, saveDir);
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


