前言
最近项目中有使用到pdf文件下载功能,在使用过程中,发现spring给我们提供了两个工具类,已经实现了文件下载的功能:StreamUtils和FileCopyUtils,都是org.springframework.util包下面的类,使用非常方便。
相关代码
service实现类
public InputStream getDownloadInputStreamByUrl(String downloadUrl) {
if (ObjectUtils.isEmpty(downloadUrl)) {
throw new RuntimeException("无效参数:fileUrl 不能为空");
}
InputStream in = null;
try {
URL url = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时时间为3秒
conn.setConnectTimeout(3 * 1000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 得到输入流
in = conn.getInputStream();
} catch (Exception e) {
throw new RuntimeException(e);
}
return in;
}
Controller类
@GetMapping("/fileDownload/{filename}")
public void downloadPdfFile(HttpServletResponse response, @PathVariable(name="filename") String filename) {
try {
if (StringUtils.isEmpty(filename)) {
throw new RuntimeException("文件名不可为空!");
}
//通过文件名查找文件对应的url
String downloadUrl = fileDownloadService.getUrlByFiname(filename);
if (StringUtils.isEmpty(filename)) {
throw new RuntimeException("文件对应地址不存在");
}
// 此处异常方法中已处理
InputStream in = fileDownloadService.getDownloadInputStreamByUrl(downloadUrl);
// 设置编码格式
response.setCharacterEncoding("UTF-8");
/* 若要下载指定目录,以下两行需注释,否则会下载两份,一份到指定目录,一份到浏览器下载目录 */
// 设置下载格式
response.setHeader("Content-Type", "application/pdf");
// 注释这行代码,可实现 预览 功能(只限pdf文件)
response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
//下载到指定目录
/*
File file = new File("C:\\tmp");
if (!file.exists()) {
//如果文件夹不存在,则创建新的的文件夹
file.mkdirs();
}
File file1 = new File(file.getPath() + File.separator + filename);
OutputStream outputStream = new FileOutputStream(file1);
FileCopyUtils.copy(in, outputStream);
*/
StreamUtils.copy(in, response.getOutputStream());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
此处通过spring提供的工具类进行文件下载。工具类中实现方式:
此处也可通过FileCopyUtils.copy(InputStream input, OutputStream output)实现文件下载,其底层也是通过调用StreamUtils.copy(InputStream in, OutputStream out)方法实现的,不过此方法最后通过finally关闭了InputStream和OutputStream,逻辑更严谨。
这两个工具类中还有其他的导出方法,可以传递其他参数进去,如图:
可以看到,这两个工具类中基本涵盖了文件下载所需的所有操作。
以上代码中对异常只是做了简单的 throw new RuntimeException() 操作,读者可根据自己的项目对异常进行处理。