想了解springbootlinux打包后访问不到resources下面的模板文件的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于springboot打成jar包后,访问不到路径的相关问题,
想了解springboot linux 打包后访问不到 resources 下面的模板文件的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于springboot打成jar包后,访问不到路径的相关问题,此外,我们还将为您介绍关于7.6 SpringBoot 读取 Resource 下文件的几种方式、java(包括springboot)读取resources下文件方式实现、java(包括springboot)读取resources下文件方式、Spring boot打包jar分离lib和resources方法实例的新知识。
本文目录一览:- springboot linux 打包后访问不到 resources 下面的模板文件(springboot打成jar包后,访问不到路径)
- 7.6 SpringBoot 读取 Resource 下文件的几种方式
- java(包括springboot)读取resources下文件方式实现
- java(包括springboot)读取resources下文件方式
- Spring boot打包jar分离lib和resources方法实例
springboot linux 打包后访问不到 resources 下面的模板文件(springboot打成jar包后,访问不到路径)
在本地是可以直接获取模板文件并下载,但是服务器上就不行
本地代码:
@Override
public void downArchRelayTemplate(HttpServletRequest request, HttpServletResponse response) {
// 定义下载文件名称
String filename = ArchRelayTemplateEnum.FILENAME.getValue();
// 得到要下载的文件
try {
File file = new File(this.getClass().getResource(ArchRelayTemplateEnum.FILE_PATH.getValue()).toURI());
FileUtil.downFile(request,response,filename,file);
} catch (Exception e) {
e.printStackTrace();
log.error("模板文件下载失败:"+e.getMessage());
}
}
服务器报错:{"bizError":false,"code":50001,"message":"文件生成失败:class path resource [tpl/appointDismiss.docx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/code/cas/target/cas-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes!/tpl/appointDismiss.docx","response":null}
意思读取不到jar包里面的文件,因为springboot是打包jar包,然后是执行运行的jar包,而不是读取的target下面编译好的文件,
解决方案:通过流读取文件内容然后转换为文件下载
@Override
public void downArchMaterialRecord(HttpServletRequest request,HttpServletResponse response)throws IOException {
InputStream in = null;
ServletOutputStream out = null;
// 定义下载文件名称
String filename = "批量新建材料转出.xls";
// 得到要下载的文件
try {
// 两种获取流的方式都可以
//InputStream inputStream = this.getClass().getResourceAsStream("/xls/downArchMaterialRecord.xls");
InputStream inputStream = new ClassPathResource("/xls/downArchMaterialRecord.xls").getInputStream();
File originFile = new File("tempFile.xls");
FileCopyUtils.copy(FileCopyUtils.copyToByteArray(inputStream),originFile);
FileUtil.downFile(request,response,filename,originFile);
FileUtil.deleteDir(originFile.getPath());
} catch (Exception e) {
e.printStackTrace();
log.error("模板文件下载失败:"+e.getMessage());
}
}
参考:https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar
7.6 SpringBoot 读取 Resource 下文件的几种方式
https://www.jianshu.com/p/7d7e5e4e8ae3
最近在项目中涉及到 Excle 的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传;这里模板位置 resource/excelTemplate/test.xlsx,尝试了四种读取方式,并且测试了四种读取方式分别的 windows 开发环境下 (IDE 中) 读取和生产环境 (linux 下 jar 包运行读取)。
第一种:
ClassPathResource classPathResource = new ClassPathResource("excleTemplate/test.xlsx");
InputStream inputStream =classPathResource.getInputStream();
第二种:
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx");
第三种:
InputStream inputStream = this.getClass().getResourceAsStream("/excleTemplate/test.xlsx");
第四种:
File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx");
InputStream inputStream = new FileInputStream(file);
经测试:
前三种方法在开发环境 (IDE 中) 和生产环境 (linux 部署成 jar 包) 都可以读取到,第四种只有开发环境 时可以读取到,生产环境读取失败。
推测主要原因是 springboot 内置 tomcat,打包后是一个 jar 包,因此通过文件读取获取流的方式行不通,因为无法直接读取压缩包中的文件,读取只能通过类加载器读取。
前三种都可以读取到其实殊途同归,直接查看底层代码都是通过类加载器读取文件流,类加载器可以读取 jar 包中的编译后的 class 文件,当然也是可以读取 jar 包中的文件流了。
用解压软件打开 jar 包查看结果如下:
其中 cst 文件中是编译后 class 文件存放位置,excleTemplate 是模板存放位置,类加载器读取的是 cst 下 class 文件,同样可以读取 excleTemplate 下的模板的文件流了。
java(包括springboot)读取resources下文件方式实现
这篇文章主要介绍了java(包括springboot)读取resources下文件方式实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文主要介绍了java(包括springboot)读取resources下文件方式实现,分享给大家,具体如下:
1、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用)
File file = new File("src/main/resources/resource.properties");
@Test public void testReadFile2() throws IOException { File file = new File("src/main/resources/resource.properties"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); fis.close(); }
2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)
File file = ResourceUtils.getFile("classpath:resource.properties"); FileInputStream fis = new FileInputStream(file);
@Test public void testReadFile3() throws IOException { File file = ResourceUtils.getFile("classpath:resource.properties"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); fis.close(); }
3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)
Resource resource = new ClassPathResource("resource.properties"); InputStream is = resource.getInputStream();
@Test public void testReadFile() throws IOException { // ClassPathResource ClassPathResource = new ClassPathResource("resource.properties"); Resource resource = new ClassPathResource("resource.properties"); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); is.close(); }
4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)
package com.tsinkai.ettp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBoottest; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.test.context.junit4.springrunner; @RunWith(springrunner.class) @SpringBoottest public class EttpCustomApplicationTests { @Autowired ResourceLoader resourceLoader; @Test public void testReaderFile() throws IOException { Resource resource = resourceLoader.getResource("classpath:resource.properties"); InputStream is = resource.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String data = null; while((data = br.readLine()) != null) { System.out.println(data); } br.close(); isr.close(); is.close(); } }
到此这篇关于java(包括springboot)读取resources下文件方式实现的文章就介绍到这了,更多相关java 读取resources内容请搜索小编以前的文章或继续浏览下面的相关文章希望大家以后多多支持小编!
java(包括springboot)读取resources下文件方式
1、使用项目内路径读取,该路径只在开发工具中显示,类似:src/main/resources/resource.properties。只能在开发工具中使用,部署之后无法读取。(不通用)
File file = new File("src/main/resources/resource.properties");
@Test
public void testReadFile2() throws IOException {
File file = new File("src/main/resources/resource.properties");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
isr.close();
fis.close();
}
2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)
File file = ResourceUtils.getFile("classpath:resource.properties");
FileInputStream fis = new FileInputStream(file);
@Test
public void testReadFile3() throws IOException {
File file = ResourceUtils.getFile("classpath:resource.properties");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
isr.close();
fis.close();
}
3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)
Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();
@Test
public void testReadFile() throws IOException {
// ClassPathResource classPathResource = new ClassPathResource("resource.properties");
Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
isr.close();
is.close();
}
4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)
package com.tsinkai.ettp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests {
@Autowired
ResourceLoader resourceLoader;
@Test
public void testReaderFile() throws IOException {
Resource resource = resourceLoader.getResource("classpath:resource.properties");
InputStream is = resource.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data = null;
while((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
isr.close();
is.close();
}
}
Spring boot打包jar分离lib和resources方法实例
这篇文章主要介绍了Spring boot打包jar分离lib和resources方法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
为什么要配置、依赖文件分离:
1.在传统jar包中,所有文件都打包到一个jar包中,jar非常臃肿,在进行服务更新部署时非常不便,而且传输不稳定时导致传输失败。如果过实行文件分离、在依赖不改变的情况下,仅仅上传更新后的 编译文件是非常方便的。如果要修改一些配置文件:properties、xml,静态文件等可以直接在服务器上编辑。那么怎么实行配置、依赖文件分离呢?插件介绍
maven-jar-plugin 这个插件式专门用来打包用的,可以配置需要打包进去的文件,程序的入口类等。
maven-resources-plugin 这个插件是用来拷贝资源文件的。
maven-maven-dependency-plugin 这个插件是用来拷贝依赖库的。
maven-assembly-plugin 可以说包含了以上插件的功能,但是可以做到更精细的控制。
spring-boot-maven-plugin 这个不用说,springboot 项目最重要的插件,整个项目的打包处理过程还是要依附于它。
打包成可执行jar,不仅仅局限SpringBoot项目(主入口函数存在)
maven-jar-plugin 插件打包jar
在pom文件中配置,但是这样 依赖的jar并不会打进来(后面会有解决方法),适用不需要依赖文件的项目。
org.apache.maven.pluginsmaven-jar-plugin2.3truexxx.xxx.Main${project.build.directory}
maven-assembly-plugin 插件打包jar
maven-assembly-pluginfalsejar-with-dependenciesxxx.xxx.Mainmake-assemblypackageassembly
打包SpringBoot 项目
方案一、
org.apache.maven.pluginsmaven-jar-plugintruelib/falsexxx.xxx.Application/resources${project.build.directory}/disorg.apache.maven.pluginsmaven-dependency-plugincopy-dependenciespackagecopy-dependencies ${project.build.directory}/dis/lib/ maven-resources-plugincopy-resourcespackagecopy-resourcessrc/main/resources${project.build.directory}/dis/resourcesorg.springframework.bootspring-boot-maven-pluginZIPtruerepackage
方案二
这里依赖assembly.xml 描述文件
maven-assembly-pluginfalseassembly.xml${project.build.directory}/dist/make-assemblypackagesingleorg.apache.maven.pluginsmaven-jar-pluginxxx.xxx.Applicationlib/trueresources/
assembly.xml
distributionzipfalsesrc/main/resources//resources/libruntime${project.groupId}:${project.artifactId}/${project.groupId}:${project.artifactId}
我们今天的关于springboot linux 打包后访问不到 resources 下面的模板文件和springboot打成jar包后,访问不到路径的分享已经告一段落,感谢您的关注,如果您想了解更多关于7.6 SpringBoot 读取 Resource 下文件的几种方式、java(包括springboot)读取resources下文件方式实现、java(包括springboot)读取resources下文件方式、Spring boot打包jar分离lib和resources方法实例的相关信息,请在本站查询。
本文标签: