springboot项⽬从硬盘指定位置读取⽂件(获取静态资源)⽅法⼀:继承WebMvcConfigurerAdapter类
package com.fig;
import t.annotation.Configuration;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurationSupport;
import org.springframework.fig.annotation.WebMvcConfigurer;
import org.springframework.fig.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.fig;
import t.annotation.Configuration;
spring怎么读多个文件import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurationSupport;
import org.springframework.fig.annotation.WebMvcConfigurer;
import org.springframework.fig.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/");
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。