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 ...
随机推荐
- Redis_简单使用
可基于内存也可持久化的Key-Value(字典, Remote Dictionary Server,远程字典服务器)数据库. 客户端:http://redis.io/clients 命令:http:/ ...
- mariadb第二章-增删改
MariaDB 数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:de ...
- C. The Tower is Going Home
链接 [http://codeforces.com/contest/1075/problem/C] 题意 有个1e9*1e9的棋盘(1,1)位置在左下角也就是车这枚棋子的位置,然后有n个在某一列后面划 ...
- 美食应用 吃了么 beta 测试报告
为了更好的测试我们应用的兼容性和性能,我们借助了网上的平台Testin云测和百度MTC平台来测试我们的应用,一下是我们的测试结果. 一.兼容性测试 我们对119台终端机器进行了测试,通过测试的有99台 ...
- Freemaker的了解
freemarket 模板技术 与web容器没什么关系 可以用struct2作为视图组件 第一步导入jar包 项目目录下建立一个templates目录 在此目录下建立一个模板文件a.ftl文件 ...
- 第一次Sprint
项目刚开始做的话,离客户的需求应该,蛮远的. 用的是eclipse加安卓模拟器在弄. 目前主要弄APP的界面和一些主要的功能算法,各个功能板块的位置划分的内容. Github团队地址是:https:/ ...
- ubuntu16.04下载安装navicate
1.下载试用版本地址: https://www.navicat.com.cn/download/navicat-premium 2.解压缩 tar -zxvf /home/rain/download ...
- Spring Cloud的Zuul的使用问题
Zuul Client 放在移动App中,Zuul Server可以做集群. Zuul Client放在jar包吗?ios怎么办? Zuul与Spring Security配合使用,与Shiro做集成 ...
- WinForm(WPF) splash screen demo with C#
https://www.codeproject.com/Articles/21062/Splash-Screen-Control https://www.codeproject.com/Article ...
- SpringMVC一例 是否需要重定向
在ASP.NET MVC下: return view("List") 和 return RedirectToAction("List") 百度知道的最佳答案: ...