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尽量不要设置为/**,设置为/**可能会出现找不到静态资源的情况

相关推荐
她的男孩10 分钟前
三人会签只批了一个就往下走:Forge Admin 把 all / any / ratio 写成 completionCondition
spring boot·低代码·工作流引擎
装不满的克莱因瓶38 分钟前
【SpringAI Alibaba】ReactAgent记忆管理MessageTrimmingHook与MessageDeletionHook
java·人工智能·ai·ai编程·springai·springaialibaba
the局外人39 分钟前
LangChain ToolRuntime 入门:context、state 与 store 的实际用法
后端·langchain·agent
回家路上绕了弯44 分钟前
当 AI 越来越能干,我们如何理解自己的价值?
后端
文丑颜不良啊1 小时前
LeetCode2241~2260题目解析
java·leetcode
陈随易1 小时前
Node.js 2026上半年都有哪些性能提升?
前端·后端·程序员
文丑颜不良啊1 小时前
LeetCode2321~2340题目解析
java·leetcode
hhcgchpspk1 小时前
flask服务端页面预防被爬措施
后端·python·flask·网络爬虫·防爬
vipxieliang1 小时前
Python collections:高效容器与实战案例
后端·python
驭渊的小故事1 小时前
Spring AOP 核心概念与通知类型详解
java·后端·spring