springboot 读取 resource文件

文件位置信息如图;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView; import com.gdtopway.common.utils.AjaxJson;
import com.szsj.entity.sample.SampleGrid;
import com.szsj.entity.sample.SampleInformatization;
import com.szsj.entity.sample.SampleMarkettechnilogy;
import com.szsj.entity.sample.SampleOthertechnology;
import com.szsj.entity.sample.SampleProductiontechnology;
import com.szsj.entity.sample.SampleScience;
import com.szsj.entity.sample.SampleSmallconstruction;
import com.szsj.entity.sample.SampleUtil;
import com.szsj.entity.semantic.TreeVO; @RestController
@RequestMapping("/sample")
public class SampleController { @javax.annotation.Resource
private ResourceLoader resourceLoader; /**
* 下载抽样模板文件
*/
@RequestMapping("downLoadExcel")
public void downLoadExcel(HttpServletResponse response, HttpServletRequest request) {
InputStream inputStream = null;
ServletOutputStream servletOutputStream = null;
try {
String filename = "抽样模板.xls";
String path = "cymb.xls";
Resource resource = resourceLoader.getResource("classpath:"+path);response.setContentType("application/vnd.ms-excel");
response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.addHeader("charset", "utf-8");
response.addHeader("Pragma", "no-cache");
String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName); inputStream = resource.getInputStream();
servletOutputStream = response.getOutputStream();
IOUtils.copy(inputStream, servletOutputStream);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (servletOutputStream != null) {
servletOutputStream.close();
servletOutputStream = null;
}
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
// 召唤jvm的垃圾回收器
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
springboot 读取 resource文件的更多相关文章
- SpringBoot读取Resource下文件的几种方式
https://www.jianshu.com/p/7d7e5e4e8ae3 最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里模板位置resour ...
- SpringBoot读取Resource下文件的几种方式(十五)
需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件. 方式一:ClassPathResource //获取模板文件:注意此处需要 ...
- Springboot读取Jar文件中的resource
如题,碰到了问题. 事情是这样的. 一个导入模板, 因为比较少, 所以就直接放在后台的resources中了.调试的时候是下载没有问题的. 等到发布后,下载就出问题了. 参照: ***.jar!\BO ...
- springboot读取resource下的文件
public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...
- springboot 读取 resource 下的文件
ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...
- 解决IDEA springBoot读取*.properties文件中文内容乱码的问题
1. 配置 properties 文件 2. 读取 sex 属性输出到页面, 中文乱码 3. file --> settings 4. Editor --> File Encodings ...
- Java读取resource文件/路径的几种方式
方式一: String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文 ...
- 微信退款SpringBoot读取resource下的证书
微信支付退款接口,需要证书双向验证,测试的时候证书暂时放在resource下,上图 起初MyConfig中我是这样,在本机IDE中运行没有问题 但到Linux服务器的docker中运行就IO异常了,查 ...
- springboot读取resource下的文本文件
https://blog.csdn.net/programmeryu/article/details/58002218 文本所在位置: 获取ZH.txt: File file = ResourceUt ...
随机推荐
- Swarm基于多主机容器网络 - overlay networks 梳理
前面介绍了Docker管理工具-Swarm部署记录,下面重点说下Swarm基于多主机容器通信的覆盖网络 在Docker版本1.12之后swarm模式原生支持覆盖网络(overlay networks) ...
- Pairproject 移山之道 阅读随笔和一些问题
首先不得不承认这本书的写作方式很独特,不像其他的计算机类的图书那样枯燥,让人读起来感觉很有意思,他也颠覆了我对计算机类图书的看法,这种写作方式值得我们学习. 先谈谈收获吧.读了两年大学,这是第一次写类 ...
- 实践——ELF文件格式分析
一.分析文件头 1. 段入口类型定义(/usr/include/elf.h)下面产生的hello是32位的 使用命令#Hexdump –x ELF_1.o 第一行: 前4字节,蓝色部分,是一个魔数,表 ...
- filter运行出现 <filter object at 0x000001B68F052828> 判断素数
刚接触filter时 运行总是出现<filter object at 0x000001B68F052828> 得不到想要的数据 后来发现是因为filter的结果是一个数组 需要 lis ...
- github 学习心得
https://github.com/kongxiangyu/test 通过本次实验,学会了如何使用github来管理代码.如果是开源的项目,通过网站托管方式进行统一管理,当然是非常棒的,并且有很多功 ...
- text3
GitHub地址https://github.com/gaodejian/gaodejian/blob/master/firework 课题研究的目的和意义 java编程语言在编程方面的具体应用,以及 ...
- Activiti的BPMN演示工具
场景是这样的:产品经理不懂技术,又不想安装Java以及Eclipse(需要安装Activiti BPMN Designer的插件). 这两天解决.bpmn的解析(BPMNParser)时顺带找到一个顺 ...
- [转帖] SQL参数化的优点 CopyFrom https://www.cnblogs.com/-lzb/articles/4840671.html
梦在远方的小猪 感谢原作者... 后面总结的五点感觉挺好的.. 自己之前的知识点一直没有串起来. 转帖记录一下感谢. sql参数化参数化 说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优 ...
- 智能制造(MES)四大阶段
智能制造的发展会经历标准化.自动化.信息化.智能化四个阶段标准化,对于生产流程.业务流程.生产制造多方面的标准化.质量检测标准化.企业管理.供应链等.标准化是组织现代化生产的重要组成部分,对于生产专业 ...
- Linux基础学习(7)--用户和用户组管理
第七章——用户和用户组管理 一.用户配置文件 1.用户信息文件/etc/passwd: (1)用户管理简介:所以越是对服务器安全性要求高的服务器,越需要建立合理的用户权限等级制度和服务器操作规范. ...