zoukankan      html  css  js  c++  java
  • springboot项目从硬盘指定位置读取文件(获取静态资源)

    方法一:继承WebMvcConfigurerAdapter类

    package com.imooc.demo.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class FileStaticConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
            /**
             *  /source/xxx   指文件的访问方式  如:localhost:8080/source/abc.wav
             *  file:d/voice/  指静态文件存放在服务器上的位置
             */
            registry.addResourceHandler("/source/**").addResourceLocations("file:"+"d:/voice/");
        }
    }
    

      

    方法二、

    在SpringBoot2.0及Spring 5.0 WebMvcConfigurerAdapter已被废弃

    官方推荐WebMvcConfigurer,代码如下:

    package com.imooc.demo.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class FileStaticConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            
            /**
             *  /source/xxx   指文件的访问方式  如:localhost:8080/source/abc.wav
             *  file:d/voice/  指静态文件存放在服务器上的位置
             */
            registry.addResourceHandler("/source/**").addResourceLocations("file:"+"d:/voice/");
        }
    }
    

      

  • 相关阅读:
    第四周学习总结
    第十三周编程总结
    2018秋季第十三周助教总结
    第十三周学习总结
    使用函数输出水仙花数 (void的用法)
    ZOJ3229 有源汇上下界最大流
    codeforces-1176 (div3)
    codeforces-1077 (div3)
    牛客假日团队赛1 题解
    牛客练习赛38 离线 启发式合并并查集
  • 原文地址:https://www.cnblogs.com/zjting/p/10767504.html
Copyright © 2011-2022 走看看