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/");
        }
    }
    

      

  • 相关阅读:
    目标检测算法原理
    物体检测项目
    Bootstrap+Font Awesome图标不显示 或显示错误解决办法
    关于 微信发送被动回复音乐消息 用户接收不到的问题
    多线程操作SQLite注意事项
    SQLiteDatabase中query、insert、update、delete方法参数说明
    Android开发:使用Fragment改造TabActivity
    UDP广播与多播
    Android 布局文件 属性区别
    Android开发
  • 原文地址:https://www.cnblogs.com/zjting/p/10767504.html
Copyright © 2011-2022 走看看