Java下载项目中静态文件方式
作者:竹秋千道
这篇文章主要介绍了Java下载项目中静态文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
Java下载项目中静态文件
废话不多说,直接上代码,拷贝即可用~~~
项目结构
下载工具类
/** * @program: myutil * @description: 从本地项目(本地磁盘上)下载静态文件 * @author: lsy * @create: 2020-08-13 16:58 **/ public class LocalFileUtils { /** * @param response * @param fileName * @description 根据指定项目路径下的某个excel, 下载文件 */ public static void exportFile(HttpServletResponse response, String fileName) { // 第一种获取静态资源 ClassPathResource classPathResource = new ClassPathResource("static/excleTemplate/" + fileName);// "static/excleTemplate/ImportModel.xlsx" // 第二种获取静态资源 // InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/excleTemplate/" + fileName); // 第三种获取静态资源 // InputStream inputStream = this.getClass().getResourceAsStream("static/excleTemplate/" + fileName); InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = classPathResource.getInputStream(); outputStream = response.getOutputStream(); int BUFFER_SIZE = 1024 * 4; byte[] buffer = new byte[BUFFER_SIZE]; int reader = 0; while ((reader = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, reader); } response.setContentType("application/octet-stream"); response.setCharacterEncoding("utf-8"); String newFileName = URLEncoder.encode(classPathResource.getFilename(), "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + newFileName); } catch (IOException e) { e.printStackTrace(); } finally { try { if (outputStream != null) { /**flush():仅仅刷新缓冲区(一般写字符时要用,因为字符时先进入缓冲区),然后将内存中的数据立刻写出(因为缓冲区是装满之后才会写出 ,用flush()就不必等到缓冲区满,立刻写出,流对象还可以继续使用) */ outputStream.flush(); /**close():关闭流对象. 也会先刷新一次缓冲区,再关闭. 关闭之后,流对象不可以继续使用 */ outputStream.close(); inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
控制器
@ApiOperation(value = "获取resource下附件") @GetMapping(value = "/exportFile") public void exportFile(String fileName, HttpServletResponse response) { // fileName = "ImportModel.xlsx"; fileName = "labixiaoxin.jpg"; LocalFileUtils.exportFile(response, fileName); }
Java把静态资源文件下载到本地
场景
springboot项目中下载resources/static 下面的静态文件(或者本地文件)
@RequestMapping("/doLoad") public void doLoad(HttpServletRequest request, HttpServletResponse response){ String filename = "×××模版"; try { // 清空输出流 response.reset(); String resultFileName = filename + ".xlsx"; resultFileName = URLEncoder.encode(resultFileName,"UTF-8"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 设定输出文件头 response.setContentType("application/msexcel");// 定义输出类型 //输入流:文件路径 // 本地路径:E:\\java\\demo\\导入模板.xlsx DataInputStream in = new DataInputStream( new FileInputStream(new File("src/main/resources/static/file/导入模版.xlsx"))); //输出流 OutputStream out = response.getOutputStream(); //输出文件 int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } out.close(); in.close(); } catch (Exception e) { e.printStackTrace(); // 清空输出流 response.reset(); } }
效果:
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。