2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > resources目录下文件加载失败 class path resource [***] cannot be resolved to URL because it does not exist

resources目录下文件加载失败 class path resource [***] cannot be resolved to URL because it does not exist

时间:2019-01-20 07:26:31

相关推荐

resources目录下文件加载失败 class path resource [***] cannot be resolved to URL because it does not exist

jar包中文件加载解决姿势:

new InputStreamReader(Objects.requireNonNull(IotRSAPemUtil.class.getResourceAsStream("/templates/pkcs8_rsa_private_key.pem")));

此问题为编译时,你的resources路径下的文件没有被纳入编译,看看你的target文件下是否有对应的文件?jar包BOOT-INF下面的classes下是否存在你的文件?你会惊讶地看到,没有该文件...所以在项目启动时,无法加载到对应的文件。

原因,pom.xml里的配置,是否被exclude?后缀文件是否被过滤

<build><resources><resource><targetPath>${project.build.directory}/classes</targetPath><directory>src/main/resources</directory><filtering>true</filtering><includes><include>**/*.properties</include><include>**/*.xml</include>< !-- 将你文件后缀配到这里,include起来 -- ></includes></resource></resources></build>

或者将文件直接让maven给打包到jar包中,配置如下

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><nonFilteredFileExtensions><nonFilteredFileExtension>sql</nonFilteredFileExtension><nonFilteredFileExtension>xlsx</nonFilteredFileExtension><nonFilteredFileExtension>pem</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin>

改好配置还是不能正确加载,万能的重启大法,此时就该走马观花啦。

本地起来没问题,打成jar包后报文件找不到?

根本原因就是文件没能被正确加载!

根本原因就是文件没能被正确加载!

根本原因就是文件没能被正确加载!

解决文件加载即可。

在本地启动,下面这行代码是可行的,打成jar包会报错!!!

ResouceUtils.getFile()是专门用来加载非压缩和Jar包文件类型的资源,所以它根本不会

去尝试加载Jar中的文件,要想加载Jar中的文件,只要用可以读取jar中文件的方式加载即可,比如 xx.class.getClassLoader().getResouceAsStream()这种以流的形式读取文件的方式.

// 这种方法打成jar,启动时报文件找不到File file = ResourceUtils.getFile("classpath:templateFile/test.xlsx"); // 这种方法也是行不通的this.class.getResourceAsStream("classpath:templateFile/test.xlsx");

getFile()源码:

public static File getFile(String resourceLocation) throws FileNotFoundException {Assert.notNull(resourceLocation, "Resource location must not be null");if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());String description = "class path resource [" + path + "]";ClassLoader cl = ClassUtils.getDefaultClassLoader();URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));if (url == null) {throw new FileNotFoundException(description +" cannot be resolved to absolute file path because it does not exist");}return getFile(url, description);}try {// try URLreturn getFile(new URL(resourceLocation));}catch (MalformedURLException ex) {// no URL -> treat as file pathreturn new File(resourceLocation);}}

解决文件加载 ,读取文件

Resource resource = new ClassPathResource("templateFile/test.xlsx"); File sourceFile = resource.getFile();

可行的方法:

// 方法一:读取流Resource resource = new ClassPathResource("classpath:rsa/****.pem");InputStream resourceAsStream = resource.getInputStream();// 方法二:InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("classpath:rsa/****.pem");

Spring 推荐我们使用ClassPathResource来获取,当然使用getFile的接口仍然是会抛出异常的,但是可以使用classPathResource.getInputStream();

//获取临时文件目录String folder = System.getProperty("java.io.tmpdir");File file = new File(folder + File.separator + cfgpath);file.createNewFile();//common-lang3FileUtils.copyInputStreamToFile(inputStream,file);

成功在jar中加载的方案:

new InputStreamReader(Objects.requireNonNull(IotRSAPemUtil.class.getResourceAsStream("/templates/pkcs8_rsa_private_key.pem")));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。