背景介紹:
公司某項(xiàng)目改為基于spring boot的,部分代碼直接做了遷移,本地調(diào)試正常,打包(.jar
)部署線上問(wèn)現(xiàn)FileNotFoundException
異常(因?yàn)橐x取一個(gè)公鑰私鑰文件內(nèi)容),然后就通過(guò)本地jar文件方式啟動(dòng),debug
追根溯源,終于發(fā)現(xiàn)了問(wèn)題所在,下面就來(lái)記錄一下。
- 獲取文件使用的是spring提供的工具類(lèi)
ResourceUtils
,代碼如下
/**
* org.springframework:spring-core:4.3.18
* org.springframework.util.ResourceUtils
*/
String fileName = "myFile.txt";
File file = ResourceUtils.getFile(fileName);
因?yàn)橹暗捻?xiàng)目是基于spring+springmvc的,所以獲取文件使用以上代碼是沒(méi)有任何問(wèn)題的,但是更換為spring boot后,就找不到文件了?下面就來(lái)看一下這個(gè)方法的源代碼究竟是啥子情況
-
ResourceUtils.getFile(fileName)
方法源碼,主要代碼有注釋
/**
* ResourceUtils.getFile(fileName)方法源碼
*/
public static File getFile(String resourceLocation) throws FileNotFoundException {
Assert.notNull(resourceLocation, "Resource location must not be null");
// 如果文件路徑是以 classpath 開(kāi)頭的就會(huì)截取掉 classpath,保留后面的字符串即相對(duì)路徑 myFile.txt
if(resourceLocation.startsWith("classpath:")) {
String path = resourceLocation.substring("classpath:".length());
String description = "class path resource [" + path + "]";
// 取得默認(rèn)的類(lèi)加載器
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 開(kāi)頭
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 開(kāi)頭的,所以會(huì)拋出 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());
}
}
}
那么為什么會(huì)出現(xiàn)異常呢?
作為技術(shù)小白的我一臉懵逼[?手動(dòng)黑人問(wèn)號(hào)臉?],原來(lái)spring boot打成jar包后,項(xiàng)目中的文件訪問(wèn)URL是長(zhǎng)這樣色兒的
[jar:file:/home/admin/workspace/xxxx/target/xxxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/myFile.txt]
所以上面代碼中的最后一個(gè)方法就通不過(guò)了,就問(wèn)你尷尬不。
那既然這個(gè)工具類(lèi)行不通就沒(méi)有辦法了嗎?答案是有的,因?yàn)閯”揪褪沁@么寫(xiě)的。。。
- 解決方法:使用另外一個(gè)工具類(lèi)
ClassPathResource
即可
/**
* org.springframework:spring-core:4.3.18
* org.springframework.core.io.ClassPathResource
*/
String fileName = "myFile.txt";
ClassPathResource resource = new ClassPathResource(fileName);
/**
* 以下就是該工具類(lèi)提供的部分方法
*/
String filename = resource.getFilename();
InputStream fiKeyFile = resource.getInputStream();
// 可以自行查看其他sao操作
好了,到此為止嘍!
PS:可能你并不適用,還請(qǐng)放過(guò)!哈哈。。。
我是bearPotMan,一個(gè)經(jīng)驗(yàn)不足的十八線演(碼)員(農(nóng))。
Know everything,control everything!