whalesea

springboot访问服务器本地静态文件的方法

一、继承WebMvcConfigurerAdapter,重写addResourceHandlers,在registry里面配置访问路径和映射到的服务器本地路径。

 

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/file/static/*").addResourceLocations("file:E:\\springboot\\");
    }
    
}

结果如图:

 

在上述配置中,访问file/static/下的文件会被映射到本地项目E:\\springboot\\下的目录里面。

 

分类:

技术点:

相关文章:

  • 2021-12-26
  • 2022-01-08
  • 2021-12-07
  • 2021-05-03
  • 2021-11-19
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2021-10-13
  • 2022-01-07
  • 2021-09-13
  • 2021-12-31
相关资源
相似解决方案