1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.yiboshi.science.utils;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
/**
* @Description: 项目静态资源文件工具类
* 仅可用于包含在web项目中的资源文件路径,资源文件必须放置于 web 模块下
* @Author: junqiang.lu
* @Date: 2019/1/4
*/
public class ResourceFileUtil {
/**
* 获取资源文件
*
* @param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
* eg: "templates/pdf_export_demo.ftl"
* @return
* @throws FileNotFoundException
*/
public static File getFile(String relativePath) throws FileNotFoundException {
if (relativePath == null || relativePath.length() == 0) {
return null;
}
if (relativePath.startsWith("/")) {
relativePath = relativePath.substring(1);
}
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX
+ relativePath);
return file;
}
/**
* 获取资源绝对路径
*
* @param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
* eg: "templates/pdf_export_demo.ftl"
* @return
* @throws FileNotFoundException
*/
public static String getAbsolutePath(String relativePath) throws FileNotFoundException {
return getFile(relativePath).getAbsolutePath();
}
/**
* 获取资源父级目录
*
* @param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
* eg: "templates/pdf_export_demo.ftl"
* @return
* @throws FileNotFoundException
*/
public static String getParent(String relativePath) throws FileNotFoundException {
return getFile(relativePath).getParent();
}
/**
* 获取资源文件名
*
* @param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
* eg: "templates/pdf_export_demo.ftl"
* @return
* @throws FileNotFoundException
*/
public static String getFileName(String relativePath) throws FileNotFoundException {
return getFile(relativePath).getName();
}
}