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);
    } }
  • 相关阅读:
    ecos之widget
    一个php小白找工作的历程
    php知识点总结(待续)
    2
    php笔试题1
    兄弟连面试宝典php
    第二十一章 消费者选择理论
    第二十章 收入不平等与贫困
    第十九章 收入与歧视
    第十八章 生产要素市场
  • 原文地址:https://www.cnblogs.com/XueTing/p/13690696.html
Copyright © 2011-2022 走看看