作者:手机用户2602919547 | 来源:互联网 | 2023-10-12 02:33
1、全局配置方式:importorg.springframework.context.annotation.Configuration;importorg.spri
1、全局配置方式:
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 WebConfigurer implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {//和页面有关的静态目录都放在项目的static目录下registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");//上传的图片在F盘下的uploads目录下,访问路径如:http://localhost:8080/images/2021-05-31/d06a63fd-7245-4dc5-9acc-dc5e001af45e.jpg//其中images表示访问的前缀。"file:F:/uploads/"是文件真实的存储路径registry.addResourceHandler("/images/**").addResourceLocations("file:F:/uploads/");}
}
运行该工程:可以发现资源文件夹static也被放入了部署的target文件夹中;
通过以下网址均可访问相关静态资源:
http://localhost:8080/static/2021-05-31/d06a63fd-7245-4dc5-9acc-dc5e001af45e.jpg
http://localhost:8080/images/2021-05-31/d06a63fd-7245-4dc5-9acc-dc5e001af45e.jpg
2、yml配置文件方式:
spring:mvc:static-path-pattern: /images/**resources:static-locations: file:F:/upload/
file:#文件上传的绝对路径baseFilePath: F:/upload/#项目根路径下的前缀路径uploadPathPrefix: /images/
http://localhost:8080/images/2021-05-31/d06a63fd-7245-4dc5-9acc-dc5e001af45e.jpg