文件位置信息如图;

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.Resourceprivate 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();}}}}

  

转载于:https://www.cnblogs.com/leirenyuan/p/9811260.html

springboot 读取 resource文件相关推荐

  1. springboot读取linux文件_spring\-boot以jar包方式时读取resource或是template文件 | Prayer's Laputa...

    现象 以jar包方式部署系统,想读取resource或是template下面的文件时,报 File Not Found 我遇到的情况是,整个项目达成了一个包,在开发环境(windows + idea) ...

  2. SpringBoot读取properties文件中的值

    SpringBoot读取properties文件中的值 properties文件(test.properties) xx.xyz.url=http://www.xxx.com/ xx.xyz.name ...

  3. springboot读取resource下json文件出现空指针异常

    1.pom包依赖 <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</ ...

  4. springboot读取资源文件配置(一)

    1.在pom文件中引入相关依赖 <!--资源文件属性配置:用来读取application.properties中的属性值到对应的类中--> <dependency><gr ...

  5. SpringBoot读取资源文件

    在Java项目中,资源文件一般都为 *.properties 文件(只能存储key-value格式的数据),SpringMVC中提供有MessageSource接口进行资源文件的读取,在SpringB ...

  6. Springboot 读取JSON文件

    1.使用类加载器读取json文件 @ApiOperation(value = "获取省市区接口", notes = "获取省市区接口")@PostMapping ...

  7. Springboot读取.csv文件并转化为JSON对象

    有时候我们需要读取.csv文件并将其中的数据处理成json对象以便后续处理,在这里整理了简单的处理流程. 1. 代码实现 1)引入依赖 <dependency><groupId> ...

  8. springboot读取linux文件_SpringBoot读取Resource下文件的几种方式

    最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传:这里待下载模板位置为resource/excelTemplate/test.xlsx,尝试了四种读取方 ...

  9. SpringBoot 读取资源文件

    Messages.propertieswelcome.url=www.baidu.com welcome.msg=\u6B22\u8FCE{0}\u7BA1\u7406!Pages.propertie ...

  10. springboot读取linux文件_SpringBoot 读取文件代码模版

    引言 Alice上线后,学生导入模版文件下载失败. 未打包是没问题的,打完包后就发生了错误.开始逐步定位问题. 原方法 File file = ResourceUtils.getFile(Resour ...

最新文章

  1. HDOJ/HDU 1556 Color the ball(树状数组)
  2. 考前自学系列·计算机组成原理·控制器的组成及各部分的功能
  3. 性能测试关注点整理总结!
  4. 安装itunes需要管理员身份_ITUNES无法安装,提示没有权限如何解决?
  5. 渭南师范计算机科学与技术,渭南师范学院计算机科学与技术专业2016年在陕西理科高考录取最低分数线...
  6. Python使用psutil模块,做你的电脑管家
  7. leetcode @python 120. Triangle
  8. 数据库和数据表的建立
  9. 22考研在职跨考软件工程(专业课408)目前待录取——经验分享和感受
  10. 大学生记账本微信小程序
  11. 猴子问题----约瑟夫环算法
  12. 华为防火墙基于IP地址的带宽管理
  13. [TI TDA4 J721E]开发板网络调试功能及开机自动配置网络
  14. 用C语言对一元二次方程求解
  15. CSP-J 2021-网络连接
  16. 苹果打字怎么换行_iPhone实用小技巧,安卓转苹果的同学看完收获尤其大
  17. solaris IPMP
  18. 基于最小错误率的贝叶斯决策实现手写数字识别
  19. linux下dhcp服务器分配出去的IP地址及剩余IP地址
  20. android微信网页视频播放器,安卓微信内置浏览器video的坑

热门文章

  1. sqlalchemy.exc.OperationalError: (mysql.connector.errors.OperationalError) MySQL Connection not avai
  2. 脑机接口系统的柔性电极概述
  3. AI Agent:引领软件开发革命的未来
  4. DNS bind部署记录
  5. C语言自动待机程序,关机 、待机和休眠编程
  6. python塔防小游戏代码_Python入门塔防小游戏,开发步骤和源码,带你轻松学python...
  7. 9. Sed manage Pattern
  8. python自动化之自主开发鼠标流程化工具(主要为pythonautogui+tkinter)
  9. 2019UMS培训day6解题报告
  10. git push时报错error: failed to push some refs to ‘https://gitee.com/ ***.git‘解决方案