一. ClassPathResource
import org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.io.InputStream;
public void run(String... args) throws Exception {
// 讀取本地的文件
String filePath = "/temp/A110120119/測試文件.text";
ClassPathResource readFile = new ClassPathResource(filePath);
// 獲取文件對象
File file = readFile.getFile();
System.out.println(file.getName());
// 獲取文件流
InputStream inputStream = readFile.getInputStream();
}
二. DefaultResourceLoader
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
public void run(String... args) throws Exception {
// 本地靜態(tài)資源路徑
String filePath = "/temp/A110120119/測試文件.text";
ResourceLoader resourceLoader = new DefaultResourceLoader();
// 讀取本地靜態(tài)資源
File orgFile = resourceLoader.getResource(filePath).getFile();
System.out.println("本地靜態(tài)資源的文件名為:" + orgFile.getName());
// 創(chuàng)建臨時文件(此時為空)
File tempFile = File.createTempFile("拷貝測試文件", ".text");
// 將本地靜態(tài)資源內(nèi)容復(fù)制到創(chuàng)建的臨時文件中
FileCopyUtils.copy(orgFile, tempFile);
// 讀取臨時文件中的所有內(nèi)容并打印
Files.readAllLines(tempFile.toPath()).forEach(System.out::println);
}
三. PathMatchingResourcePatternResolver
PathMatchingResourcePatternResolver是一個
Ant模式通配符
的Resource查找器,可以用來查找類路徑下或者文件系統(tǒng)中的資源。
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
public void run(String... args) throws Exception {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
// 本地靜態(tài)資源路徑
String filePath = "/temp/A110120119/測試文件.text";
// 獲取指定路徑下的資源文件
Resource resource = resourcePatternResolver.getResource(filePath);
System.out.println(resource.getFilename());
System.out.println("-----------------------------------------------");
// 獲取temp文件夾下,所有文件夾中以 .text 為后綴的所有文件
Resource[] resources = resourcePatternResolver.getResources("/temp/**/*.text");
for (Resource resourceFile : resources) {
System.out.println(resourceFile.getFilename());
}
System.out.println("-----------------------------------------------");
// 獲取本地磁盤中的資源文件路徑
Resource osFile = resourcePatternResolver.getResource("E:/寫真/jojo/下載.png");
System.out.println(osFile.getFilename());
}
四. ResourceUtils
???在SpringBoot中盡量避免使用ResourceUtils讀取資源文件。ResourceUtils.getFile()
獲取的是資源文件的絕對路徑,當(dāng)項目打包為jar或者war包之后部署,資源文件的絕對路徑改變,因此會報錯。
import org.springframework.util.ResourceUtils;
public void run(String... args) throws Exception {
// 本地靜態(tài)資源路徑
String filePath = "/temp/A110120119/測試文件.text";
File file = ResourceUtils.getFile(filePath);
System.out.println(file.getName());
}
文章來源:http://www.zghlxwxcb.cn/news/detail-470242.html
參考資料文章來源地址http://www.zghlxwxcb.cn/news/detail-470242.html
- Springboot 生產(chǎn)環(huán)境下讀取Resource下的文件
- SpringBoot不要使用ResourceUtils讀取資源文件
到了這里,關(guān)于SpringBoot 讀取項目中靜態(tài)資源文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!