在web项目中,难免设计文件下载与上传,对于文件上传下载使用频繁的应用需要依靠数据库存储具体位置,而对于上传下载不那么频繁的应用,则只需要将文件放在resource目录下,对于这种情况,我们要了解的就是如何下载,以及如何去获取resource下的文件路径。
放在resource/static 目录下文件的获取
SprintBoot 打Jar包环境下使用如下方式,亲测有效
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 获取远程服务器IP和端口
try {
//获取所有匹配的文件
Resource[] resources = resolver.getResources("static/images/faceSearch/*.*");
for(Resource resource : resources) {
//获得文件流,因为在jar文件中,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
InputStream stream = resource.getInputStream();
if (log.isInfoEnabled()) {
log.info("读取的文件流 [" + stream + "]");
}
String targetFilePath =env.getProperty("faceSearchDemo.faceSerachPics")+resource.getFilename();
if (log.isInfoEnabled()) {
log.info("放置位置 [" + targetFilePath + "]");
}
File ttfFile = new File(targetFilePath);
if(!ttfFile.getParentFile().exists()) {
ttfFile.getParentFile().mkdir();
}
FileUtils.copyInputStreamToFile(stream, ttfFile);
}
}catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
而对于 打成war包的应用则使用如下方式:
File file = ResourceUtils.getFile("classpath:static/images/faceSearch");
if (file.exists()) {
File[] files = file.listFiles();
List<String> imageList = new ArrayList<>();
if (files != null) {
for(File fileC :files) {
imageList.add(fileC.getName());
}
}
model.addAttribute("faceSerachImgList", imageList);
SpringBoot 文件下载示例
前端代码
<body>
<form class="layui-form" id="instructions">
<div class="layui-form-item">
<div class="layui-input-block">
<a href="/instructionsDownload">使用说明书下载</a>
</div>
</div>
</form>
</body>
后台代码,文件放在目录resource/static/instructions/测试工具帮助文档.docx ,注意getResource()的路径中注意去掉resource
@GetMapping("/instructionsDownload")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException{
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource= resolver.getResource("static/instructions/测试工具帮助文档.docx");
String fileName = "测试工具帮助文档.docx";
if (fileName != null) {
File file = new File("static/instructions/"+fileName);
if (resource.exists()) {
response.setContentType("application/force-download");
//初始代码:response.addHeader("Content-Disposition","attachment;fileName=" +fileName);
response.addHeader("Content-Disposition","attachment;fileName=" +new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
// response.addHeader("Content-Disposition","attachment;fileName=" +fileName);
byte[] buffer = new byte[1024];
InputStream fis = null;
BufferedInputStream bis = null;
try {
fis = resource.getInputStream();
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("下载成功...");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (fis != null)
fis.close();
}
}
}
return null;
}
这篇文章写得很好,文章的作者的博客也超级好,不出书都可惜了
https://www.cnblogs.com/xdp-gacl/p/4200090.html
这篇也可以借鉴
https://blog.csdn.net/lq15310444798/article/details/78770192