基于Springboot框架 web项目 真实路径映射到虚拟url路径的实现

最近有一个需求

前提是很不靠谱的需求 最后一定会删掉重做的 但是要现在凑合用的 

就是用springboot的框架 搭建的一个web项目 因为springboot是自带tomcat的 所以会打包成jar包 放到服务器上部署

问题就是说 我们需要在服务器上 建立一个文件夹 来存放提供给用户的模板文件 什么授权文件模板之类

解决的方案就是说

在配置文件中 写好 linux上真实路径到url虚拟访问路径的映射

映射:
写在springboot的配置文件 application-config.yml文件里

file:
  path:
    real: /home/项目名/file/
    fictitious: IP地址:端口号/项目名/uploadfile/

     

最后添加拦截器 给加上映射

@Configuration
public class SpringbootIntercepterConfig extends WebMvcConfigurerAdapter {

    /**
     *
     * @Title:  文件上传路径映射
     * @Description:储存在  /uploadfile/** 路径下的文件映射到/home/项目名/uploadfile/
     * 
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){

        registry.addResourceHandler("/uploadfile/**").addResourceLocations("file:/home/项目名/uploadfile/");
        super.addResourceHandlers(registry);
    }

}

最后访问的方式就是

就酱~

猜你喜欢

转载自blog.csdn.net/qq_31310291/article/details/81212157