spring boot 打jar包,获取resource路径下的文件
前言:最近在spring boot项目静态类中获取resource路径下文件,在idea中启动都可以获取,但是打包后变成了jar包 就无法获取到。
我想到了两种方法,一种是根据http访问静态资源比如:localhost:9080/static/template/xxx.ftl文件。
另外一种是根据流获取到文件,然后拷贝到新的文件夹下面。下面说的就是第二种方式的代码
public class DocUtil {
//此路径是其他方法进行调用,且只需要加载一次
private static String sourceTemplatePath;
// 模板文件名称 位于 resource/static/template下面
private static String[] ftlArray = {"申请书.ftl", "授权委托书.ftl", "法定代表人身份证明书.ftl", "逾期督促申请.xls"}; static {
//静态方法调用一次
sourceTemplatePath = createFtlFileByFtlArray();
} private static String createFtlFileByFtlArray() {
String ftlPath = "static/template/";
String path = "";
for (int i = 0; i < ftlArray.length; i++) {
path = createFtlFile(ftlPath, ftlArray[i]);
if (null == path) {
logger.info("ftl not copy success:" + ftlArray[i]);
}
}
return path;
} private static String createFtlFile(String ftlPath, String ftlName) {
try {
//获取当前项目所在的绝对路径
String proFilePath = System.getProperty("user.dir");
logger.info("project run path:" + proFilePath);
//获取模板下的路径
String newFilePath = proFilePath + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + ftlPath;
newFilePath = newFilePath.replace("/", File.separator);
logger.info("newFilePath:" + newFilePath);
//检查项目运行时的src下的对应路径
File newFile = new File(newFilePath + ftlName);
if (newFile.isFile() && newFile.exists()) {
return newFilePath;
}
//当项目打成jar包会运行下面的代码,并且复制一份到src路径下(具体结构看下面图片)
InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(ftlPath + ftlName);
byte[] certData = IOUtils.toByteArray(certStream);
FileUtils.writeByteArrayToFile(newFile, certData); return newFilePath; } catch (IOException e) { logger.error("复制ftl文件失败--> 异常信息:" + e); } return null; }
}
项目打成jar包时的文件路径结构
spring boot 打jar包,获取resource路径下的文件的更多相关文章
- java web项目中 获取resource路径下的文件路径
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...
- Spring boot 打成jar包问题总结
Spring boot 打成jar包问题总结 1.Unable to find a single main class from the following candidates 1.1.问题描述 m ...
- spring boot将jar包转换成war包发布
spring boot将jar包转换成war包发布步骤 将<packaging>jar</packaging>修改为<packaging>war</packa ...
- Spring Boot由jar包转成war包
Spring Boot由jar包转成war包 spring boot 默认是以jar包形式启动web程序,在新建spring boot项目时候可以选择war包的启动方式. 建议在开发的时候建立以jar ...
- Python获取指定路径下所有文件的绝对路径
需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...
- 在Docker容器中运行Spring Boot的jar包 jar外的配置文件无法生效
Spring Boot加载配置文件,默认会从几个固定位置搜索一下看看有没有配置文件 ——application.properties或者bootstrap.properties(如果你使用了sprin ...
- 【转】Spring boot 打成jar包问题总结
http://www.cnblogs.com/xingzc/p/5972488.html 1.Unable to find a single main class from the following ...
- Spring Boot 配置 jar 包外面的 Properties 配置文件
一.概述 Properties 文件是我们可以用来存储项目特定信息的常用方法.理想情况下,我们应该将其保留在 jar 包之外,以便能够根据需要对配置进行更改. 在这个教程中,我们将研究在 Spring ...
- Spring Boot导出jar包发布
一:事由 现在的项目组开发项目使用的是Spring Boot的技术,开发的时候是直接通过一个入口主函数来启动项目的.如果将项目交给客户,怎样才能正确的发布运行呢?百度了一下有关的知识,大概了解到是通过 ...
随机推荐
- 去掉字符空格js
1. 去掉字符串两端的空格 String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, "&quo ...
- kafka中zookeeper的操作
bin/zookeeper-shell.sh localhost:2181 <<< "get /brokers/ids/4" ./zkCli.sh -server ...
- why microsoft named their cloud service Azure?
best guess I can personally make is that because Azure literally means “bright blue color of the clo ...
- 打开Python IDLE时的错误:Subprocess Startup Error
比较常见的是这个 方法1: 修改[Python目录]\Lib\idlelib\PyShell.py文件,在1300行附近,将def main():函数下面 use_subprocess = True ...
- RestTemplate远程调用POST请求:HTTP 415 Unsupported Media Type
这是本项目的接口 称为client @POST @Path("/{urlcode}") @Consumes(MediaTypes.JSON_UTF_8) @Produces(Med ...
- 如何解决微信小程序界面适配问题-引用-生命周期回调函数-优化机制-样式引入
如何解决微信小程序界面适配问题 .wxss page{ height: 100%; width:750rpx; } this.setData({ imageWidth: wx.getSystemInf ...
- Kali学习笔记17:OpenVAS安装部署
正式介绍OpenVAS之前先说一些题外话 1.有一个网站记录了很多的漏洞: https://www.exploit-db.com/ 可以下载利用 2.如果觉得从网上寻找太麻烦,Kali自带工具:sea ...
- NopCommerce用.net core重写ef
最近看了NopCommerce源码,用core学习着写了一个项目,修改的地方记录下.项目地址 NopCommerce框架出来好久了.18年的第一季度 懒加载出来后也会全部移动到.net core.那么 ...
- android WebView详解,常见漏洞详解和安全源码
这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析. 转载请注明出处:http://blog.csdn.net/se ...
- linux 清空历史命令
系统版本:CentOS 6 history -c 命令可以清空当前窗口的历史输出命令. 要彻底删除历史命令可以有如下几种方式: 在当前用户的-目录下执行: 方式1: echo > .bash_h ...