005-SpringSecurity-Demo 配置外部文件映射

SpringSecurity-Demo 配置外部文件映射

    • 一、需求说明
    • 二、实现步骤
      • [1. 定义配置属性类(读取yml配置)](#1. 定义配置属性类(读取yml配置))
      • [2. 编写资源映射配置](#2. 编写资源映射配置)
      • [3. application.yml 配置(路径可自定义)](#3. application.yml 配置(路径可自定义))
    • 三、原理讲解(一看就懂)
    • 四、使用示例
    • 五、路径写法说明
      • [1. 相对路径](#1. 相对路径)
      • [2. Windows 绝对路径](#2. Windows 绝对路径)
      • [3. Linux 绝对路径](#3. Linux 绝对路径)
    • 六、常见问题
      • [1. 访问404](#1. 访问404)
      • [2. 不想用profile作为URL前缀](#2. 不想用profile作为URL前缀)
      • [3. 映射网络路径?](#3. 映射网络路径?)

在 Spring Boot 项目开发中,我们经常需要上传图片、文档等文件,并希望通过浏览器地址直接访问(比如头像、附件预览)。

但直接把文件存在项目内部会导致打包过大、更新不便,所以最佳实践是:文件存在外部文件夹,通过 Spring MVC 资源映射实现外网访问

本篇博客就带你一步步实现:外部文件目录 → 浏览器在线访问,完全适配 Spring Boot 3.2.4 + JDK 21,代码可直接复制用于生产环境。


一、需求说明

我们要实现的功能非常简单实用:

  1. 把文件存在项目根目录的 ./file 文件夹(实际使用可配置其它目录)
  2. 通过地址 http://localhost:5826/profile/xxx.png 直接访问图片
  3. 路径可配置,不写死代码

二、实现步骤

代码已推送至 Gitee 仓库:springdecurity-demo

1. 定义配置属性类(读取yml配置)

专门用来读取外部配置,代码解耦、规范优雅。

java 复制代码
package com.junjiu.springboot3.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * program: security-demo
 * ClassName: JunjiuCustomProperties
 * description:
 *
 * @author: 君九
 * @create: 2026-04-03 14:35
 * @version: 1.0
 **/
@Data
@Component
@ConfigurationProperties(prefix = "junjiu.custom")
public class JunjiuCustomProperties {

    /**
     * 外部资源存储路径
     * 例如:./file
     */
    private String resourcePath;
}

2. 编写资源映射配置

继承 WebMvcConfigurer,重写 addResourceHandlers 实现外部文件夹映射到URL

java 复制代码
package com.junjiu.springboot3.config;

import com.junjiu.springboot3.config.properties.JunjiuCustomProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 外部资源映射配置
 * 实现:外部文件夹 -> 浏览器URL访问
 */
@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    @Autowired
    private JunjiuCustomProperties junjiuCustomProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 从配置文件读取路径
        String resourcePath = junjiuCustomProperties.getResourcePath();

        // 确保路径以 / 结尾,避免拼接错误
        if (!resourcePath.endsWith("/")) {
            resourcePath += "/";
        }

        System.out.println("外部资源路径:" + resourcePath);

        // 映射规则:
        // 浏览器访问 /profile/**  →  找本地 file:xxx 目录
        registry.addResourceHandler("/profile/**")
                .addResourceLocations("file:" + resourcePath);
    }
}

3. application.yml 配置(路径可自定义)

yaml 复制代码
junjiu:
  custom:
    resource-path: ./file  # 相对路径,项目根目录下的 file 文件夹

三、原理讲解(一看就懂)

java 复制代码
registry.addResourceHandler("/profile/**")
        .addResourceLocations("file:" + resourcePath);

作用:

  • 当你访问:http://localhost:5826/profile/avatar.jpg
  • Spring Boot 就会去:项目根目录/file/avatar.jpg 找图片
  • 找到就直接显示,找不到返回404

✅ 支持:

  • 图片(png/jpg/gif)
  • 文档(pdf/txt)
  • 视频(mp4)
  • 音频(mp3)

四、使用示例

  1. 在项目根目录创建 file 文件夹

  2. 放入一张图片 xr.jpg

  3. 启动项目

  4. 浏览器访问:

    http://localhost:5826/profile/xr.jpg

✅ 直接显示图片!


五、路径写法说明

1. 相对路径

yaml 复制代码
resource-path: ./file

表示:项目根目录/file

2. Windows 绝对路径

yaml 复制代码
resource-path: D:/upload/file

3. Linux 绝对路径

yaml 复制代码
resource-path: /home/server/upload

六、常见问题

1. 访问404

  • 检查文件夹是否创建
  • 检查文件名大小写
  • 检查路径是否以 / 结尾
  • 重启项目

2. 不想用profile作为URL前缀

修改这里即可:

java 复制代码
addResourceHandler("/files/**")

访问地址变成:

复制代码
http://localhost:5826/files/test.jpg

3. 映射网络路径?

支持,但需要权限和共享目录。


相关推荐
Gent_倪2 小时前
Quartz 入门指南(二)Spring Boot + Quartz 示例
java·spring boot·quartz
唐不是营养物质2 小时前
无头浏览器chromedriver使用(目前不支持国产操作系统)
java·pdf
zhishidi3 小时前
使用python给pdf文档自动添加目录书签
java·python·pdf
WiChP11 小时前
【V0.1B5】从零开始的2D游戏引擎开发之路
java·服务器·数据库
cch891811 小时前
汇编与Java:底层与高层的编程对决
java·开发语言·汇编
荒川之神12 小时前
拉链表概念与基本设计
java·开发语言·数据库
cch891812 小时前
汇编与Go:底层到高层的编程差异
java·汇编·golang
chushiyunen12 小时前
python中的@Property和@Setter
java·开发语言·python