fix resource

This commit is contained in:
Looly 2025-04-26 13:08:55 +08:00
parent b0e37e3ef3
commit 761c8c1757
4 changed files with 7 additions and 3 deletions

View File

@ -314,7 +314,7 @@ public class FileUtil {
}
// 如果用户需要相对项目路径则使用project:前缀
if (path.startsWith("project:")) {
if (path.startsWith(UrlUtil.PROJECT_URL_PREFIX)) {
return new File(path);
}

View File

@ -243,7 +243,7 @@ public class ResourceUtil {
*/
public static Resource getResource(final String path) {
if (StrUtil.isNotBlank(path)) {
if (path.startsWith(UrlUtil.FILE_URL_PREFIX) || FileUtil.isAbsolutePath(path)) {
if (StrUtil.startWithAny(path, UrlUtil.FILE_URL_PREFIX, UrlUtil.PROJECT_URL_PREFIX) || FileUtil.isAbsolutePath(path)) {
return new FileResource(path);
}
}

View File

@ -55,6 +55,10 @@ public class UrlUtil {
* 针对ClassPath路径的伪协议前缀兼容Spring: "classpath:"
*/
public static final String CLASSPATH_URL_PREFIX = "classpath:";
/**
* 针对project路径的伪协议前缀: "project:"
*/
public static final String PROJECT_URL_PREFIX = "project:";
/**
* URL 前缀表示文件: "file:"
*/

View File

@ -59,6 +59,6 @@ public class ResourceUtilTest {
@Test
void getResourceTest2() {
// project:开头表示基于项目的相对路径此处无文件报错
Assertions.assertThrows(NoResourceException.class, () -> ResourceUtil.getResource("project:test.xml"));
Assertions.assertThrows(NoResourceException.class, () -> ResourceUtil.getResource("project:test.xml").getStream());
}
}