Spring Boot使用ResourceUtils FileNotFoundException的一次采坑经历

背景介绍:
公司某项目改为基于spring boot的,部分代码直接做了迁移,本地调试正常,打包(.jar)部署线上问现 FileNotFoundException 异常(因为要读取一个公钥私钥文件内容),然后就通过本地jar文件方式启动,debug 追根溯源,终于发现了问题所在,下面就来记录一下。

  1. 获取文件使用的是spring提供的工具类 ResourceUtils ,代码如下
/**
 * org.springframework:spring-core:4.3.18
 * org.springframework.util.ResourceUtils
 */
String fileName = "myFile.txt";
File file = ResourceUtils.getFile(fileName);

因为之前的项目是基于spring+springmvc的,所以获取文件使用以上代码是没有任何问题的,但是更换为spring boot后,就找不到文件了?下面就来看一下这个方法的源代码究竟是啥子情况

  1. ResourceUtils.getFile(fileName)方法源码,主要代码有注释
/**
 * ResourceUtils.getFile(fileName)方法源码
 */
public static File getFile(String resourceLocation) throws FileNotFoundException {
    Assert.notNull(resourceLocation, "Resource location must not be null");
    // 如果文件路径是以 classpath 开头的就会截取掉 classpath,保留后面的字符串即相对路径 myFile.txt
    if(resourceLocation.startsWith("classpath:")) {
        String path = resourceLocation.substring("classpath:".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");
        } else {
            return getFile(url, description);
        }
    } else {
        // 文件路径不是以 classpath 开头
        try {
            return getFile(new URL(resourceLocation));
        } catch (MalformedURLException var5) {
            return new File(resourceLocation);
        }
    }
}

// getFile(URL resourceUrl)方法源码
public static File getFile(URL resourceUrl) throws FileNotFoundException {
    return getFile(resourceUrl, "URL");
}

// getFile(URL resourceUrl, String description)方法源码
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException{
    Assert.notNull(resourceUrl, "Resource URL must not be null");
    // spring boot中是以 jar 开头的,所以会抛出 FileNotFoundException 异常
    if(!"file".equals(resourceUrl.getProtocol())) {
        throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);
    } else {
        try {
            return new File(toURI(resourceUrl).getSchemeSpecificPart());
        } catch (URISyntaxException var3) {
            return new File(resourceUrl.getFile());
        }
    }
}

那么为什么会出现异常呢?
作为技术小白的我一脸懵逼[?手动黑人问号脸?],原来spring boot打成jar包后,项目中的文件访问URL是长这样色儿的

[jar:file:/home/admin/workspace/xxxx/target/xxxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/myFile.txt]

所以上面代码中的最后一个方法就通不过了,就问你尴尬不。
那既然这个工具类行不通就没有办法了吗?答案是有的,因为剧本就是这么写的。。。

  1. 解决方法:使用另外一个工具类 ClassPathResource 即可
/**
 * org.springframework:spring-core:4.3.18
 * org.springframework.core.io.ClassPathResource
 */
String fileName = "myFile.txt";
ClassPathResource resource = new ClassPathResource(fileName);
/**
 * 以下就是该工具类提供的部分方法
 */
String filename = resource.getFilename();
InputStream fiKeyFile = resource.getInputStream();
// 可以自行查看其他sao操作

好了,到此为止喽!
PS:可能你并不适用,还请放过!哈哈。。。

我是bearPotMan,一个经验不足的十八线演(码)员(农)。
Know everything,control everything!

你可能感兴趣的:(Spring Boot使用ResourceUtils FileNotFoundException的一次采坑经历)