springboot url路径映射本地静态资源

有两种方式

第一种在配置文件中

XML 复制代码
upload-path: /filename1/upload/
spring:
  mvc:
    static-path-pattern: /static/**
  resources:
    static-locations: classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classpath:/templates/,classpath:/public/,file:${upload-path}

第二种在java 代码

java 复制代码
package com.hwh.communitymanage.config;

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

import java.io.File;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadPath = "file:D:/upload-dir/";
      	registry.addResourceHandler("/static/**").addResourceLocations(uploadPath );
    }

}

注意:映射的路径static-path-pattern尽量不要设置为/**,设置为/**可能会出现找不到静态资源的情况

相关推荐
sailing-data3 分钟前
【SE】接口标准化
java·开发语言
t***p93516 分钟前
idea创建springBoot的五种方式
java
+VX:Fegn089517 分钟前
计算机毕业设计|基于springboot+vue的学校课程管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
S***267540 分钟前
【监控】spring actuator源码速读
java·spring boot·spring
Pluchon42 分钟前
硅基计划4.0 算法 FloodFill算法
java·算法·leetcode·决策树·逻辑回归·深度优先·图搜索算法
上进小菜猪1 小时前
魔珐星云让AI拥有“身体“的具身智能开发平台实战评测
后端
阿拉斯攀登1 小时前
深入微服务配置中心:Nacos注册中心的实操细节
java·微服务·云原生·springcloud
f***24111 小时前
springboot系列--自动配置原理
java·spring boot·后端
一 乐1 小时前
水果销售|基于springboot + vue水果商城系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端