• springboot配置虚拟路径访问上传到磁盘的文件


    问题描述:

    文件上传到磁盘后,如果想要访问该文件的话,可以通过配置虚拟路径映射到该磁盘文件进行访问。

     

    解决方法:

    1、在application.properties文件中配置虚拟路径,需要注意的是,"访问文件的基本路径地址"的IP地址和端口号必须和项目的相同

    #磁盘文件存储路径
    file.path=H:/Business/image/
    
    #虚拟路径
    file.static-path=/upload/image/**
    
    #访问文件的基本路径地址
    file.base-url=http://127.0.0.1:8080/upload/image/

    2、编写虚拟路径配置

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    /**
     * 虚拟路径配置类
     */
    @Configuration
    public class WebAppMvcConfig implements WebMvcConfigurer {
        @Value("${file.path}")
        private String path;
    
        @Value("${file.static-path}")
        private String staticPath;
        
        // 如果访问http://localhost:8080/upload/image/test.jpg
        // 实则访问H:/Business/image/test.jpg
        
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {  
    registry.addResourceHandler(staticPath).addResourceLocations("file:"+path);
    } }
  • 相关阅读:
    miragejs 学习
    json-server学习
    react-redux
    webpack4知识汇总2
    webpack4知识汇总1
    vue跳转当前页面
    redux初识
    react知识补漏2
    vue ssr
    状态码
  • 原文地址:https://www.cnblogs.com/XueTing/p/13690696.html
Copyright © 2020-2023  润新知