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);
    } }
  • 相关阅读:
    IT综合学习网站收集
    使用CSS实现表格细边框的三种方式
    安装Ionic遇到的问题
    未能解析此远程名称:'nuget.org' 的解决方法
    webapi 安全验证与权限验证
    iOS模拟器可以编译,真机无法编译
    Mac上安装FFmpeg命令行
    写在工作三周年
    MPMoviePlayerController概述
    NSStream概述
  • 原文地址:https://www.cnblogs.com/XueTing/p/13690696.html
Copyright © 2011-2022 走看看