zoukankan      html  css  js  c++  java
  • 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);
    } }
  • 相关阅读:
    CSP-S 2021游记
    logback-spring.xml配置
    springboot编译的命令
    Springboot 常用注解
    logback如何配置springboot框架
    如何使用IDEA快速创建一个springboot项目
    slf4j、log4j、 logback关系详解和相关用法
    SSM整合及Maven pom.xml
    OO第四单元总结
    OO第三单元总结
  • 原文地址:https://www.cnblogs.com/XueTing/p/13690696.html
Copyright © 2011-2022 走看看