Spring : serving static resources outside context root

前端 未结 2 413
长发绾君心
长发绾君心 2020-12-02 18:54

in a web app, I need to serve static contents (images) located outside the application context directory. The overall application architecture requires me to use Tomcat to p

相关标签:
2条回答
  • 2020-12-02 19:35

    There is one more simple correction

    the code should be

    <mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/>

    Did you notice the difference ? You need to put '/' at the end of the absolute path.

    or you can use the java configuration

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String rootPath = System.getProperty("user.home");
        String imagePath = "file:"+rootPath + File.separator + "tmpFiles/";
        System.out.println(imagePath);
        registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
        registry.addResourceHandler("/tmpFiles/**").addResourceLocations(imagePath);
    }
    

    Its working for me.

    0 讨论(0)
  • 2020-12-02 19:36

    <mvc:resources> can serve resources from the outside, you need to use the usual Spring resource path syntax:

    <mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir/"/> 
    
    0 讨论(0)
提交回复
热议问题