Spring MVC Get file under WEB-INF without a request

后端 未结 7 815
暗喜
暗喜 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:21

    This is how you can do it if you just want to access it from a Service (not through ServletContext):

        final DefaultResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource("classpath:templates/mail/sample.png");
        File myFile = resource.getFile();
    

    Note that the last line may throw IOException so you need to catch / rethrow

    Note, that the file is here: src\main\resources\templates\mail\sample.png

提交回复
热议问题