springboot-项目获取resources下文件
背景:
项目的resources 目录下放置了一份txt文件,需求是想获取到这份文件转成File对象,调用文件上传的接口进行文件上传操作。

实现:
1、使用 ResourceUtils:本地ok,测试环境无效
File file = ResourceUtils.getFile("classpath:whitelist/微乐分新白名单2月推荐-前100.txt");
注意:ResourceUtils.getFile() 方法可以获取到resources 下的文件,本地没问题 但是发布到 测试环境(linux系统下)没用。
2、ClassPathResource,用于打成jar包后获取文件的方法:本地&测试环境都ok
ClassPathResource classPathResource = new ClassPathResource("whitelist/微乐分新白名单2月推荐-前100.txt");
InputStream inputStream = classPathResource.getInputStream();
File file = asFile(inputStream);//手动转换:InputStream To File
备注:不可以直接通过classPathResource.getFile()得到File,会失败,暂时不清楚为什么,先解决问题,方式为由classPathResource获取到InputStream,然后再将InputStream转成File,转换的方式如下:
/**
* InputStream To File
*
* @param in InputStream
*
* @return File
*
* */
public static File asFile(InputStream in) throws IOException {
File tempFile = File.createTempFile("whitelist", ".tmp");
tempFile.deleteOnExit();
FileOutputStream out = new FileOutputStream(tempFile);
IOUtils.copy(in, out);
return tempFile;
}
参考文章:
springboot-项目获取resources下文件的方法
springboot-项目获取resources下文件的更多相关文章
- springboot-项目获取resources下文件的方法
spring项目获取resources下文件的方法 最近写读取模板文件做一些后续的处理,将文件放在了项目的resources 下,发现了一个好用的读取方法: 比如上边是你需要读取的文件: 读 ...
- Maven项目读取resources下文件的路径问题(getClassLoader的作用)
读取resources下文件的方法 网上有问答如下:问: new FileInputStream("src/main/resources/all.properties") new ...
- springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)
项目是spring-boot + spring-cloud 并使用maven 管理依赖.在springboot+maven项目下怎么读取resources下的文件实现文件下载? 怎么获取resourc ...
- springboot项目获取resource下的文件
package com.expr.exceldemo; import org.springframework.core.io.ClassPathResource; public class Test ...
- java(包括springboot)读取resources下文件方式
1.使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties.只能在开发工具中使用,部署之后无法读取.(不通用) File fi ...
- Maven项目读取resources下文件的路径
要取编译后的路径,而不是你看到的src/main/resources的路径.如下: URL url = 类名.class.getClassLoader().getResource("conf ...
- springboot打成jar后获取classpath下文件失败
原文链接:https://blog.csdn.net/qq_18748427/article/details/78606432 springboot打成jar后获取classpath下文件失败 使用如 ...
- Spring Boot 获取 java resources 下文件
Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...
- java 获取classpath下文件多种方式
java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...
- spring java 获取webapp下文件路径
spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...
随机推荐
- java字符串和图片相互转换
package com.thinkgem.jeesite.modules.api.wechat; import sun.misc.BASE64Decoder; import sun.misc.BASE ...
- docker容器启动报错Unable to access jarfile
1.错误表现 Errot: Unable to access jarfile /opt/run-java/deployments/chiano.jar 2.原因:容器的基础镜像对jar的权限有要求 3 ...
- Window/linux(docker) 单台宿主机部署多个Jenkins节点
Window/linux(docker) 单台宿主机部署多个Jenkins节点 在使用Jenkins过程中,增加了手动输入的步骤,会阻塞节点运行任务: 由于资源有限,需要在一台机器挂载了很多Jenki ...
- 通过网页下载qq音乐在线听歌曲
1.输入网址 打开 qq音乐网页版 https://y.qq.com/ 2.搜索喜欢的歌曲 3.播放喜欢的歌曲 4.谷歌浏览器检查代码 5.找到 network栏 搜索 m4a 6.找到相关地址 复制 ...
- 下载低版本Xcode方法
1.浏览器打开下面链接 https://developer.apple.com/download/all/ 2.没登录需要登录appid账号 3.搜索想要下载的xcode版本号 4.点击下载即可
- Sql Server 服务无法正常启动,126异常
SQL Server 一记 异常信息 方法/步骤 打开[开始菜单]找到软件SQL Server下面的[配置管理器] 可以看到数据库主服务是 '停止' 状态: ![数据库主服务 展开[SQL Serve ...
- 链表与malloc的疑惑
1.奇怪点:如果我只是需要一个结点的空间为什么malloc的转换形式写成--Node=(int *)malloc(sizeof(int)) 自我解答:void *malloc(unsigned int ...
- elasticsearch 根据主键_id更新部分字段
package com.better517na.ebookingbusiservice.helper;import com.alibaba.fastjson.JSON;import com.aliba ...
- keras构建1D-CNN模型
接触过深度学习的人一定听过keras,为了学习的方便,接下来将要仔细的讲解一下这keras库是如何构建1D-CNN深度学习框架的 from keras.datasets import imdb fro ...
- HTML悬浮div
需求,html底部为地图,上层是各个div HTML<body> //地图 <div id='map' id="demo1"></div> &l ...