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 ...
随机推荐
- Dell BOSS 卡是什么
全名: Boot Optimized Storage Solution 针对 M.2 接口的 SSD,主板上必须设计接口进行适配. 设计一款主板对于硬件厂商来说是有成本的,其中包括 主板设计成本 产品 ...
- UVA - 116 Unidirectional TSP 多段图的最短路 dp
题意 略 分析 因为字典序最小,所以从后面的列递推,每次对上一列的三个方向的行排序就能确保,数字之和最小DP就完事了 代码 因为有个地方数组名next和里面本身的某个东西冲突了,所以编译错了,后来改成 ...
- Visual Studio 2015的安装及单元测试练习
第一部分:Visual Studio 2015的安装 我电脑系统是win10,所以安装的是Visual Studio 2015,安装步骤部分截图如图所示: 1.安装类型选项界面:可以选择默认安装,可以 ...
- 20135327郭皓--Linux内核分析第六周 进程的描述和进程的创建
进程的描述和进程的创建 一.进程的描述 操作系统三大功能: 进程管理 内存管理 文件系统 进程描述符task_struct数据结构 task _ struct:为了管理进程,内核必须对每个进程进行清晰 ...
- Linux内核第六节 20135332武西垚
如何描述一个进程:进程描述符的数据结构: 如何创建一个进程:内核是如何执行的,以及新创建的进程从哪里开始执行: 使用gdb跟踪新进程的创建过程. 进程的描述 操作系统三大功能: 进程管理(最核心最基础 ...
- Filter(转载)
web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...
- Linux下Vim使用备忘
1.Insert键,决定是Insert模式还是Replace模式. 2.Esc键,退出编辑模式(Insert Or Replace). 3.:wq (ZZ) 保存并退出Vim. http://caib ...
- [转帖] .NET FrameWork 版本的确定方法
检测电脑安装的net framework版本 https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx To find .N ...
- 将ubuntu14.04 从mysql从5.5删除之后安装5.7遇到的一些问题(本篇不讨论热升级)
五一放假实在无聊 继续玩弄新的服务器.发现有台mysql版本实在有点老,估计是akiho直接使用 apt-get install mysql-server ,然后又没有更新到最新的源,然后无脑安装了5 ...
- mysql 8.0,运行springboot项目配置:
1.修改pom.xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connecto ...