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

相关推荐
DKPT8 分钟前
Java享元模式实现方式与应用场景分析
java·笔记·学习·设计模式·享元模式
Percep_gan16 分钟前
idea的使用小技巧,个人向
java·ide·intellij-idea
ん贤17 分钟前
RESTful风格
后端·go·restful
缘来是庄17 分钟前
设计模式之迭代器模式
java·设计模式·迭代器模式
Humbunklung18 分钟前
Rust方法语法:赋予结构体行为的力量
开发语言·后端·rust
Liudef0623 分钟前
基于HTML与Java的简易在线会议系统实现
java·前端·html
萧曵 丶25 分钟前
Rust 内存结构:深入解析
开发语言·后端·rust
Kookoos27 分钟前
ABP VNext + Cosmos DB Change Feed:搭建实时数据变更流服务
数据库·分布式·后端·abp vnext·azure cosmos
JosieBook33 分钟前
【Java编程动手学】Java常用工具类
java·python·mysql
oioihoii36 分钟前
C++11标准库算法:深入理解std::none_of
java·c++·算法