@@ -37,36 +37,36 @@ public class SysConstant {
3737 @ Autowired
3838 private ResourceLoader resourceLoader ;
3939
40- @ Value ("${upload. folder:/tmp/upload}" ) // 容器内部固定路径,默认值为/tmp/upload
40+ @ Value ("${folder.upload :/tmp/upload}" )
4141 private String uploadFolder ;
4242
43+ @ Value ("${folder.static:/tmp/static}" )
4344 private String staticFolder ;
4445
4546 public SysConstant (ResourceLoader resourceLoader ) {
4647 this .resourceLoader = resourceLoader ;
4748 }
4849
49-
5050 @ PostConstruct
5151 public void init () throws IOException {
52- // 获取资源对象
53- File uploadDir = new File (uploadFolder );
54- if (!uploadDir .exists ()) {
55- if (!uploadDir .mkdirs ()) {
56- throw new IOException ("Failed to create upload directory: " + uploadFolder );
57- }
58- }
52+ // 初始化上传目录
53+ initializeDirectory (uploadFolder , "upload" );
54+
55+ // 初始化静态资源目录
56+ initializeDirectory (staticFolder , "static" );
57+ }
5958
60- // Resource uploadResource = resourceLoader.getResource("classpath:/static/upload/");
61- Resource staticResource = resourceLoader .getResource ("classpath:/static/" );
62- if (staticResource .exists ()) {
63- try {
64- this .staticFolder = staticResource .getFile ().getPath ();
65- } catch (IOException e ) {
66- this .staticFolder = staticResource .getURL ().toString ();
67- }
68- } else {
69- throw new IOException ("Resource not found!" );
59+ /**
60+ * 初始化目录,如果不存在则尝试创建
61+ *
62+ * @param path 目录路径
63+ * @param directoryName 目录名称,用于错误提示
64+ * @throws IOException 如果目录创建失败
65+ */
66+ private void initializeDirectory (String path , String directoryName ) throws IOException {
67+ File dir = new File (path );
68+ if (!dir .exists () && !dir .mkdirs ()) {
69+ throw new IOException ("Failed to create " + directoryName + " directory: " + path );
7070 }
7171 }
7272
0 commit comments