Spring MVC Get file under WEB-INF without a request

后端 未结 7 812
暗喜
暗喜 2020-12-24 07:08

I am trying to get hold of a file ( or a directory ) under /WEB-INF/.../

outside of a request. I need it in a bean loaded at server startup.

A

7条回答
  •  囚心锁ツ
    2020-12-24 07:44

    As long as your bean is declared in web application context you can obtain an instance of ServletContext (using ServletContextAware, or by autowiring).

    Then you can access files in webapp directory either directly (getResourceAsStream(), getRealPath()), or using ServletContextResource.

    EDIT by momo:

    @Autowired
    ServletContext servletContext;
    
    ... myMethod() { 
         File rootDir = new File( servletContext.getRealPath("/WEB-INF/myDIR/") );
    }
    

提交回复
热议问题