關於Spring工具類獲取classpath下的文件資源

今天用到spring的工具類去獲取classpath下的資源文件,在此記錄下

1、資源文件不在jar中

import java.io.File;
import org.springframework.util.ResourceUtils;
File testFile = ResourceUtils.getFile("classpath:test.txt");

或者

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource fileRource = new ClassPathResource("test.txt");

獲取文件:fileRource.getFile();
獲取文件流:fileRource.getInputStream();


2、資源文件在jar中

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource fileRource = new ClassPathResource("test.txt");
獲取文件流:fileRource.getInputStream();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章