Spring加载文件资源
MUEDSA,•Development
优先加载包外,找不到再加载包内。可能有些问题,欢迎PR[GitHub wechat-mch-mock]
public class ResUtil {
private static final PathMatchingResourcePatternResolver RESOLVER = new PathMatchingResourcePatternResolver();
private static final String FILE_RESOURCE_ROOT_PATH = new ClassPathResource(new File(".").getAbsolutePath()).getPath() + "/";
public static void findResourceInputStreams(String relativePath, Map<String, InputStream> foundMap) throws IOException {
Map<String, InputStream> map = new HashMap<>();
findFileResourceInputStreams(relativePath, map);
if(map.isEmpty()){
findClassPathResourceInputStreams(relativePath, map);
}
foundMap.putAll(map);
}
public static void findFileResourceInputStreams(String relativePath, Map<String, InputStream> foundMap) throws IOException {
Resource[] resources = RESOLVER.getResources(ResourceUtils.FILE_URL_PREFIX + StringUtils.applyRelativePath(FILE_RESOURCE_ROOT_PATH, relativePath));
for (Resource resource : resources) {
if (resource.exists() && resource.isFile() && resource.isReadable()) {
foundMap.put(resource.getFilename(), resource.getInputStream());
}
}
}
public static void findClassPathResourceInputStreams(String relativePath, Map<String, InputStream> foundMap) throws IOException {
Resource[] resources = RESOLVER.getResources(ResourceUtils.CLASSPATH_URL_PREFIX + relativePath);
for (Resource resource : resources) {
if (resource.exists() && resource.isReadable()) {
foundMap.put(resource.getFilename(), resource.getInputStream());
}
}
}
public static InputStream getResourceInputStream(String relativePath) throws IOException {
InputStream inputStream = getFileInputStream(relativePath);
if(Objects.isNull(inputStream)) inputStream = getClassPathFileInputStream(relativePath);
return inputStream;
}
public static InputStream getFileInputStream(String relativePath) throws IOException {
Resource fileResource = RESOLVER.getResource(ResourceUtils.FILE_URL_PREFIX + StringUtils.applyRelativePath(FILE_RESOURCE_ROOT_PATH, relativePath));
InputStream inputStream = null;
if(fileResource.exists() && fileResource.isFile() && fileResource.isReadable()) {
inputStream = fileResource.getInputStream();
}
return inputStream;
}
public static InputStream getClassPathFileInputStream(String relativePath) throws IOException {
Resource resource = RESOLVER.getResource(ResourceUtils.CLASSPATH_URL_PREFIX + relativePath);
InputStream inputStream = null;
if (resource.exists() && resource.isReadable()) {
inputStream = resource.getInputStream();
}
return inputStream;
}
}