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

相关推荐
J***51681 分钟前
SpringSecurity的配置
java
面汤放盐3 分钟前
软件架构指南 Software Architecture Guide
java·微服务·devops
tkevinjd3 分钟前
JUC5(线程池)
java·线程池·多线程·juc
武子康3 分钟前
大数据-210 如何在Scikit-Learn中实现逻辑回归及正则化详解(L1与L2)
大数据·后端·机器学习
Tao____4 分钟前
如何对接Modbus-tcp协议(使用Thinlinks物联网平台)
java·物联网·网络协议·tcp/ip·modbus
鱼跃鹰飞8 分钟前
经典面试题:K8S的自动缩扩容和崩溃恢复
java·容器·kubernetes
Coder_Boy_12 分钟前
Spring Boot 事务回滚异常 UnexpectedRollbackException 详解(常见问题集合)
java·spring boot·后端
青云交14 分钟前
Java 大视界 -- 基于 Java+Redis Cluster 构建分布式缓存系统:实战与一致性保障(444)
java·redis·缓存·缓存穿透·分布式缓存·一致性保障·java+redis clus
风象南14 分钟前
SpringBoot 实现网络限速
后端
不知疲倦的仄仄15 分钟前
第五天:深度解密 Netty ByteBuf:高性能 IO 的基石
java·开源·github