当前位置: 首页 > news >正文

Java从resources文件下载文档,文档没有后缀名

业务场景:因为公司会对excel文档加密,通过svn或者git上传代码也会对文档进行加密,所以这里将文档后缀去了,这样避免文档加密。

实现思路:将文档去掉后缀,放入resources下,获取输入流,最后加上后缀,前端成功下载

效果图

请添加图片描述


请添加图片描述

上代码

package com.***.util;import cn.hutool.core.util.StrUtil;
import org.springframework.core.io.ClassPathResource;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;/*** @author longwei* @Description excel帮助类* @date 2023/8/30 14:36*/
public class ExcelUtils {/*** 从静态资源下载文件** @param fileName 文件名,没有后缀* @param suffix   文件后缀* @param request  request* @param response response*/public static void downloadFileByLocalPath(HttpServletRequest request, HttpServletResponse response,String fileName, String suffix) throws Exception {if (StrUtil.isEmpty(fileName) || StrUtil.isEmpty(suffix)) {throw new RuntimeException("文件信息不能为空");}InputStream inputStream = new ClassPathResource("file" + File.separator + fileName).getInputStream();fileName = fileName + suffix;downFileByInputStream(request, response, inputStream, fileName);}public static void downFileByInputStream(HttpServletRequest request, HttpServletResponse response, InputStream inputStream, String fileName) throws Exception {byte[] buffer = new byte[1024];BufferedInputStream bis = null;OutputStream os = null;String finalFileName;try {final String userAgent = request.getHeader("USER-AGENT");//IE浏览器if (StrUtil.contains(userAgent, "MSIE") || StrUtil.contains(userAgent, "Trident")) {finalFileName = URLEncoder.encode(fileName, "UTF8");}//google,火狐浏览器else if (StrUtil.contains(userAgent, "Mozilla")) {finalFileName = new String(fileName.getBytes(), "ISO8859-1");}//其他浏览器else {finalFileName = URLEncoder.encode(fileName, "UTF8");}response.setCharacterEncoding("UTF-8");// 设置强制下载不打开response.setContentType("application/force-download");// 设置文件名response.addHeader("Content-Disposition", "attachment;fileName=" + finalFileName);bis = new BufferedInputStream(inputStream);os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {os.write(buffer, 0, i);i = bis.read(buffer);}} catch (Exception e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (Exception e) {e.printStackTrace();}}if (bis != null) {try {bis.close();} catch (Exception e) {e.printStackTrace();}}if (os != null) {try {os.flush();os.close();} catch (Exception e) {e.printStackTrace();}}}}}

controller层

    @RequestMapping("/downloadTemplate")public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) {breedInfoService.downloadTemplate(request, response);}

service.impl层,这里直接用ExcelUtils方法

	@Overridepublic void downloadTemplate(HttpServletRequest request, HttpServletResponse response) {String fileName = "中药饮片导入模板";try {ExcelUtils.downloadFileByLocalPath(request, response, fileName, ".xlsx");} catch (Exception e) {log.error("下载中药饮片导入模板失败-{}", e.getMessage());throw new BusinessException("下载中药饮片导入模板失败,请联系管理员!");}}
http://www.lryc.cn/news/191451.html

相关文章:

  • 【动手学深度学习-Pytorch版】BERT预测系列——BERTModel
  • Python之元组、字典和集合练习
  • 【数据结构】归并排序和计数排序(排序的总结)
  • 某医疗机构:建立S-SDLC安全开发流程,保障医疗前沿科技应用高质量发展
  • 验证二叉搜索树的后序遍历序列
  • 第三章 内存管理 一、内存的基础知识
  • 【Java学习之道】Java常用集合框架
  • logicFlow 流程图编辑工具使用及开源地址
  • ATF(TF-A)/OPTEE之动态代码分析汇总
  • 10-11 周三 shell xargs tr curl 做大事情
  • 1.1 向量与线性组合
  • django: You may need to add ‘localhost‘ to ALLOWED_HOSTS
  • 网络安全(黑客技术)—自学手册
  • 【Vue】之Vuex的入门使用,取值,修改值,同异步请求处理---保姆级别教学
  • ubuntu20.04 nerf Instant-ngp (下) 复现,自建数据集,导出mesh
  • 【常见错误】SVN提交项目时,出现了这样的提示:“XXX“ is scheduled for addition, but is missing。
  • 深度学习基础知识 给模型的不同层 设置不同学习率
  • 【Python 零基础入门】 Numpy
  • 1600*C. Circle of Monsters(贪心)
  • 国外互联网巨头常用的项目管理工具揭秘
  • sql 注入(4), 盲注
  • 【string题解 C++】字符串相乘 | 翻转字符串III:翻转单词
  • CentOS 7下JumpServer安装及配置(超详细版)
  • 基于 ACK Fluid 的混合云优化数据访问(五):自动化跨区域中心数据分发
  • sentinel的启动与运行
  • 模拟量采集无线WiFi网络接口TCP Server, UDP, MQTT
  • 五、OSPF动态路由实验
  • 系统架构设计:16 论软件开发过程RUP及其应用
  • Gralloc ION DMABUF in Camera Display
  • 【LVS】lvs的四种模式的区别是什么?