1. controller类:
package com.neo.controller; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 通用文件处理controller * @author wjqhuaxia */ @RestController @RequestMapping("/generalFile") public class GeneralFileController { /** * 模版文件下载 * @param fileType 文件类型 对应 TemplateFileTypeEnum 的key * @param notIE 是否IE浏览器,true是,false不是 * @return */ @GetMapping("/downTemplate/{fileType}/{notIE}") public String downTemplate(HttpServletResponse response, @PathVariable String fileType, @PathVariable boolean notIE){ TemplateFileTypeEnum fileTypeEnum = TemplateFileTypeEnum.getEnum(fileType); FileUtils.downloadFile(response, fileTypeEnum, notIE); return "下载成功!"; } // 测试:http://localhost:8080/generalFile/downTemplate/test/false }
2. 文件工具类
package com.neo.controller; import java.io.BufferedInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletResponse; import org.springframework.core.io.ClassPathResource; /** * 文件工具类 * @author wjqhuaxia */ public class FileUtils { /** * 下载项目根目录下doc下的文件 * @param response response * @param fileName 文件名 * @param notIE 是否IE浏览器,true是,false不是 * @return 返回结果 成功或者文件不存在 */ public static String downloadFile(HttpServletResponse response, TemplateFileTypeEnum fileTypeEnum, boolean notIE) { String fileName = fileTypeEnum.getFileName(); String filePath = fileTypeEnum.getFilePath(); String contentType = fileTypeEnum.getContentType(); ClassPathResource classPathResource = new ClassPathResource(filePath + fileName); InputStream stream = null; try { stream =classPathResource.getInputStream(); } catch (IOException e3) { e3.printStackTrace(); } response.setHeader("content-type", "application/octet-stream"); response.setContentType(contentType); try { String name = java.net.URLEncoder.encode(fileName, "UTF-8"); if (notIE) { name = java.net.URLDecoder.decode(name, "ISO-8859-1"); } response.setHeader("Content-Disposition", "attachment;filename=" + name ); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } byte[] buff = new byte[1024]; BufferedInputStream bis = null; OutputStream os = null; try { os = response.getOutputStream(); bis = new BufferedInputStream(stream); int i = bis.read(buff); while (i != -1) { os.write(buff, 0, buff.length); os.flush(); i = bis.read(buff); } } catch (FileNotFoundException e1) { //e1.getMessage()+"系统找不到指定的文件"; return "系统找不到指定的文件"; }catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return "success"; } }
3. 枚举类
package com.neo.controller; /** * 模版文件类型枚举类 * @author wjqhuaxia */ public enum TemplateFileTypeEnum { /** * 测试试例 */ TEMPLATE_TEST("test","测试模版.xlsx","static/templates/","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); private TemplateFileTypeEnum(String fileType,String fileName, String filePath,String contentType){ this.contentType = contentType; this.fileName = fileName; this.fileType = fileType; this.filePath = filePath; } /** * 文件类型 */ private String fileType; /** * 文件名 */ private String fileName; /** * 文件所在项目路径的位置 */ private String filePath; /** * response中设置的contentType */ private String contentType; public String getFileType() { return fileType; } public String getFileName() { return fileName; } public String getFilePath() { return filePath; } public String getContentType() { return contentType; } /** * 通过文件类型获取对应枚举 * @param fileType 文件类型 * @return */ public static TemplateFileTypeEnum getEnum(String fileType){ TemplateFileTypeEnum[] values = TemplateFileTypeEnum.values(); for (TemplateFileTypeEnum templateFileTypeEnum : values) { if(templateFileTypeEnum.getFileType().equals(fileType)){ return templateFileTypeEnum; } } return null; } }
说明:
一、如果只是需要进行简单的静态资源访问,直接使用springboot提供的机制即可,不需要动代码。
可参考: https://www.cnblogs.com/gu-bin/p/11129066.html
二、案例中的文件路径如下:
本案例参考:
https://blog.csdn.net/weixin_33915554/article/details/91710148 --- SpringBoot读取Resource下文件的几种方式
https://blog.csdn.net/liubin5620/article/details/79530490 --- Spring Boot项目实现下载项目根目录下指定目录(doc)里的文件
https://blog.csdn.net/xiaoyu19910321/article/details/79279364 --- 常见 content-type对应表