文件位置信息如图;

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文件的更多相关文章

  1. SpringBoot读取Resource下文件的几种方式

    https://www.jianshu.com/p/7d7e5e4e8ae3 最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里模板位置resour ...

  2. SpringBoot读取Resource下文件的几种方式(十五)

    需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件. 方式一:ClassPathResource //获取模板文件:注意此处需要 ...

  3. Springboot读取Jar文件中的resource

    如题,碰到了问题. 事情是这样的. 一个导入模板, 因为比较少, 所以就直接放在后台的resources中了.调试的时候是下载没有问题的. 等到发布后,下载就出问题了. 参照: ***.jar!\BO ...

  4. springboot读取resource下的文件

    public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...

  5. springboot 读取 resource 下的文件

    ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...

  6. 解决IDEA springBoot读取*.properties文件中文内容乱码的问题

    1. 配置 properties 文件 2. 读取 sex 属性输出到页面, 中文乱码 3. file --> settings 4. Editor --> File Encodings ...

  7. Java读取resource文件/路径的几种方式

    方式一: String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文 ...

  8. 微信退款SpringBoot读取resource下的证书

    微信支付退款接口,需要证书双向验证,测试的时候证书暂时放在resource下,上图 起初MyConfig中我是这样,在本机IDE中运行没有问题 但到Linux服务器的docker中运行就IO异常了,查 ...

  9. springboot读取resource下的文本文件

    https://blog.csdn.net/programmeryu/article/details/58002218 文本所在位置: 获取ZH.txt: File file = ResourceUt ...

随机推荐

  1. centos6下redis cluster集群部署过程

    一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168.10.10 6379,然后启动主从,主从 ...

  2. Redis常用操作-------Key(键)

    1.DEL key [key ...] 删除给定的一个或多个 key . 不存在的 key 会被忽略. 可用版本: >= 1.0.0 时间复杂度: O(N), N 为被删除的 key 的数量. ...

  3. SCRUM 12.19

    我们的爬虫依然存在一些问题,我们决定暂时将大家的工作重心放在爬虫上. 新的任务分配如下 成员 原本任务 新任务 彭林江 落实API 研究美团爬虫 郝倩 研究遍历美团数据方法 研究遍历美团数据方法 牛强 ...

  4. 个人博客作业Week2(代码规范,代码复审)

    Q:是否需要有代码规范 首先我们来搞清楚什么是“代码规范”,它和“代码风格”又有什么关系.依据个人的审美角度,我可能更喜欢在函数与函数之间空出一行,可能在命名习惯和代码注释上更加的internatio ...

  5. Week 2 代码审查

    我的伙伴是6班的小伙子潘礼鹏,经过几天的相处我觉得真的是说话很有趣的人,性格非常好,我们很划得来. 以下为我对他的代码的审查结果: VS2012与VS2013的兼容性 在这里写一个工具集的问题,不同的 ...

  6. 【Beta阶段】M2事后分析

    先上照片,最后一次开会了啊... 计划 你原计划的工作是否最后都做完了? 如果有没做完的,为什么? 答:没有全部做完,到目前为止,我们的还有几个实验的报告生成功能没有上线.这几个实验的数据处理文件已经 ...

  7. 阅读<构建之法>10、11、12章

    第十章: 典型用户和场景对后面工作有什么帮助吗? 第十一章: 每日构建的目的是什么呢?有没有具体说明? 第十二章: 产品定位人群是否也局限了产品的可拓展性?

  8. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  9. opencv学习笔记(四)

    ROI---设定感兴趣的区域(region of interest) 定义: Mat imageROI; //方法一:通过Rect指定矩形区域 imageROI=image(Rect(500,250, ...

  10. SpringMVC一例 是否需要重定向

    在ASP.NET MVC下: return view("List") 和 return RedirectToAction("List") 百度知道的最佳答案: ...