Springboot文件下载工具类
发布人:shili8
发布时间:2024-12-23 21:17
阅读次数:0
**SpringBoot 文件下载工具类**
在SpringBoot项目中,经常需要实现文件下载功能。例如,用户点击下载按钮后,服务器端返回一个文件给客户端,这个过程涉及到文件的读取、流式传输以及浏览器的解析等步骤。在这种情况下,我们可以使用工具类来简化这些复杂的操作。
**工具类设计**
我们将这个工具类命名为`FileDownloadUtil`,它提供了一个统一的接口,让开发者能够轻松地实现文件下载功能。工具类中包含以下几个主要方法:
* `downloadFile(HttpServletResponse response, String filePath)`: 根据文件路径返回文件流。
* `downloadFile(HttpServletResponse response, byte[] fileBytes, String fileName)`: 根据字节数组和文件名返回文件流。
**工具类代码**
javaimport org.springframework. />import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet. />import java.io.*;
import java.util.zip.GZIPInputStream;
/**
* @author [Your Name]
*/
public class FileDownloadUtil {
/**
* 根据文件路径返回文件流 *
* @param response HttpServletResponse对象 * @param filePath 文件路径 * @return 文件流 */
public static void downloadFile(HttpServletResponse response, String filePath) {
// 判断文件是否存在 File file = new File(filePath);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
try (InputStream inputStream = new FileInputStream(file)) {
// 根据文件类型设置响应头 setResponseHeader(response, file.getName(), MediaType.APPLICATION_OCTET_STREAM_VALUE);
// 将文件流写入到输出流中 writeInputStreamToOutputStream(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException("读取文件失败", e);
}
}
/**
* 根据字节数组和文件名返回文件流 *
* @param response HttpServletResponse对象 * @param fileBytes 字节数组 * @param fileName 文件名 */
public static void downloadFile(HttpServletResponse response, byte[] fileBytes, String fileName) {
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes)) {
// 根据文件类型设置响应头 setResponseHeader(response, fileName, MediaType.APPLICATION_OCTET_STREAM_VALUE);
// 将文件流写入到输出流中 writeInputStreamToOutputStream(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException("读取字节数组失败", e);
}
}
/**
* 根据文件类型设置响应头 *
* @param response HttpServletResponse对象 * @param fileName 文件名 * @param contentType 响应体类型 */
private static void setResponseHeader(HttpServletResponse response, String fileName, String contentType) {
// 设置文件名 response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 设置响应体类型 response.setContentType(contentType);
}
/**
* 将输入流写入到输出流中 *
* @param inputStream 输入流 * @param outputStream 输出流 */
private static void writeInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
byte[] buffer = new byte[1024];
int len;
while ((len = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer,0, len);
}
} catch (IOException e) {
throw new RuntimeException("写入输出流失败", e);
}
}
}
**使用示例**
java@GetMapping("/download")
public void download(HttpServletResponse response) {
String filePath = "/path/to/file.txt";
FileDownloadUtil.downloadFile(response, filePath);
}
@GetMapping("/downloadBytes")
public void downloadBytes(HttpServletResponse response) {
byte[] fileBytes = getBytesFromFile("/path/to/file.txt");
String fileName = "file.txt";
FileDownloadUtil.downloadFile(response, fileBytes, fileName);
}
**注意事项**
* 在使用`FileDownloadUtil`类时,请确保文件路径正确,并且文件存在。
* 如果需要压缩文件下载,可以在`downloadFile`方法中使用`GZIPInputStream`来压缩文件流。
* 如果需要设置响应头的其他属性(例如,Content-Type、Content-Length等),可以在`setResponseHeader`方法中添加相应代码。

