SpringBoot项目打成jar包后,上传的静态资源(图片等)如何存储和访问

1.问题描述:

使用springboot开发一个项目,开发文件上传的时候,通常会将上传的文件存储到资源目录下的static里面,然后在本地测试上传文件功能没有问题,但是将项目打成jar包放到服务器上运行的时候就会报错,找不到对应目录。或者可以将上传文件存储到和jar包同级的目录下,但是无法通过http访问到文件

2.项目打成jar包后如何设置文件资源存储路径

!!!保存上传的资源文件路径,路径在部署jar包同级目录

java 复制代码
//这里 /img/uploadFile/ 可以更改为不同层级的目录,可以跟开发时的静态目录统一
    String path = System.getProperty("user.dir")+"/static/img/uploadFile/";
    File dir = new File(path);
// 如果不存在则创建目录
    if(!dir.exists()){
        dir.mkdirs();
      }

3.设置http访问上传的静态资源文件

在项目中创建一个config包,然后新建一个java类uploadConfig,用来设置上传的路径

java 复制代码
package com.curry.config;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class uploadConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/","file:static/");
    }
}

接下来就可以通过http://ip:port/img/uploadFile/test.png来访问该文件了

如果static的目录结构和项目里面的一样的话,就跟访问打jar包前一样访问即可,相当于通过目录结构访问相应的文件。

完整代码

java 复制代码
package com.xxx.xxx.xxxx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

@Controller
@RequestMapping("/admin")
public class UploadController {

    @RequestMapping("/upload")
    public String upload(MultipartFile file,
                         Model model) throws IOException {

        //校验文件是否为空
        if (file.isEmpty()){
            model.addAttribute("path","请选择文件上传!");
            return "admin/upload";
        }
        //图片重命名,防止图片名称重复,可以使用时间戳或者UUID生成,我这里不需要担心文件名重复,所以没做
        String originalFilename = file.getOriginalFilename();
        String filename = originalFilename;

        //上传图片,并判断是否有年份文件夹
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
//        String pre = System.getProperty("user.dir") + "/src/main/resources/static/img/paper/" + year +"/";
        String pre = System.getProperty("user.dir") + "/static/img/paper/" + year +"/";

        //file.mkdir创建一个文件夹,成功则返回true,失败则返回false。失败表明File对象指定的路径已经存在,或者由于整个路径还不存在,该文件夹不能被创建。
        File file1 = new File(pre);
        file1.mkdir();


        String path = pre + filename;
        file.transferTo(new File(path));

        model.addAttribute("path",path);
        return "admin/upload";
    }

}
相关推荐
程序员老乔5 分钟前
Java 新纪元 — JDK 25 + Spring Boot 4 全栈实战(二):Valhalla落地,值类型如何让电商DTO内存占用暴跌
java·spring boot·c#
SuniaWang11 分钟前
《Spring AI + 大模型全栈实战》学习手册系列· 专题二:《Milvus 向量数据库:从零开始搭建 RAG 系统的核心组件》
java·人工智能·分布式·后端·spring·架构·typescript
张小洛14 分钟前
Spring 常用类深度剖析(工具篇 02):ReflectionUtils——优雅操作反射的利器
java·后端·spring·工具类·spring常用类
夕颜11128 分钟前
Skill 与 MCP Function:傻傻分不清楚?
后端
古城小栈32 分钟前
Go 底层代码的完整分类
开发语言·后端·golang
码界奇点32 分钟前
基于Spring Boot和MyBatis的图书管理系统设计与实现
spring boot·后端·车载系统·毕业设计·mybatis·源代码管理
轩情吖38 分钟前
MySQL之事务管理
android·后端·mysql·adb·事务·隔离性·原子性
李长鸿1 小时前
基于Docker的多重内网穿透方案:构建高可用备份架构
后端
程序员爱钓鱼1 小时前
Go PDF处理利器: github.com/pdfcpu/pdfcpu 深度指南
后端·面试·go
Luna-player1 小时前
第3章 Spring Boot的Web应用支持,个人学习笔记
前端·spring boot·学习