springboot 静态资源 替换 本地资源访问

本文介绍了如何在Spring Boot应用中,通过YAML配置和Java代码实现磁盘路径映射,以便于将大文件存储在服务器外部并提供静态资源访问。重点讲解了`webapp.data`配置和`FileUploadConfig`的使用,以及Windows和Linux环境下的路径处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原由

在实际的开发中,有可能我们会上传文件到服务器,然后返回一个url直接访问这个文件。但是当文件比较多,体积也比较大时,不可能将这些文件全部存放在jar服务中。这个时候,这种方式就特别有用,因为我们可以把磁盘上的一个位置映射为静态资源访问路径,通过访问静态资源路径就可以直接访问到磁盘上的资源. 而已 如果服务器运行jar包 就不可以新增填一些图片 文件夹等等 所以麻烦 保存到服务器的相应位置上面去

yml 配置

#文件上传路径
#/home/xxx
#文件上传绝对路径(注意:结尾“/”)
#file.upload.imgPath=D://file/upload/=> /home/xxx/upload/
webapp:
  data:
    access-path: /upload
    path-pattern: ${webapp.data.access-path}/**
    mapping-location-windows: D:/upload/
    mapping-location-not-windows: /home/xxx/upload/

config 配置

package com.yqs.config.file;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;

/**
 *
 *  * @Description 本地资源配置
 *  * @Author 小乌龟
 *  * @Date 2022/2/22 9:08
 *
 * 作用: 用于配置静态资源映射的一些属性
 * accessPath: 访问静态资源时的路径,如访问ip:port/visual/upload/xxx/xxx,其中visual是项目路径,upload便是accessPath
 * mappingLocationWindows: 指明在windows下运行时accessPath映射到服务器磁盘的绝对路径,比如映射到D:/data
 * mappingLocationNotWindows: 指明在Linux或Mac下运行时accessPath映射到服务器磁盘的绝对路径,比如映射到/usr/data
 * pathPattern: 配置静态资源映射时的参数,表示路径匹配规则,如/upload/**表示以ip:port/visual/upload/开头的url
 * 都会认为是访问静态资源,然后到mappingLocation指定的路径下去寻找资源
 * resourceLocation: 配置静态资源映射时的参数,表示磁盘的绝对路径,如file:/usr/upload/,必须加上file。
 */
@Data
@Component
public class WebAppDataProperties {

    @Value("${webapp.data.access-path}")
    private String accessPath;
    @Value("${webapp.data.path-pattern}")
    private String pathPattern;
    @Value("${webapp.data.mapping-location-windows}")
    private String mappingLocationWindows;
    @Value("${webapp.data.mapping-location-not-windows}")
    private String mapperLocationNotWindows;

    public String getMappingLocation() {
        String os = System.getProperty("os.name");
        return (os.toLowerCase().startsWith("win")) ? mappingLocationWindows : mapperLocationNotWindows;
    }

    public String getResourceLocation() {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            File file = new File(mappingLocationWindows);
            if (!file.exists()) {
                file.mkdirs();
            }
            return "file:" + mappingLocationWindows;
        } else {
            File file = new File(mapperLocationNotWindows);
            if (!file.exists()) {
                file.mkdirs();
            }
            return "file:" + mapperLocationNotWindows;
        }
    }
}
package com.yqs.config.file;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description  自定义静态资源映射
 * @Author 小乌龟
 * @Date 2022/2/18 16:13
 */

@Slf4j
@Configuration //这里特别要注意一下(@Configuration) 一定要加上 要不然springboot识别不了
public class FileUploadConfig implements WebMvcConfigurer {
    @Autowired
    private WebAppDataProperties webAppDataProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(webAppDataProperties.getPathPattern())
                .addResourceLocations(webAppDataProperties.getResourceLocation());
    }
}

然后就可以运行测试运行成果了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新生代农民工-小王八

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值