After the SpringBoot project is packaged as a jar package, how to store and access the uploaded static resources (pictures, etc.)

1. Problem description:

Use springboot to develop a project. When developing a file upload, the uploaded file is usually stored in the static under the resource directory, and then there is no problem in testing the function of uploading the file locally, but the project is packaged into a jar package and run on the server. Sometimes an error will be reported, and the corresponding directory cannot be found. Or the uploaded file can be stored in the same directory as the jar package, but the file cannot be accessed through http

insert image description here

2. How to set the file resource storage path after the project is packaged into a jar package

!!!Save the uploaded resource file path, the path is in the same level directory as the deployment jar package

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

insert image description here

3. Set http access to uploaded static resource files

Create a config package in the project, and then create a new java class uploadConfig to set the upload path

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/");
    }
}
  • Then you can http://ip:port/img/uploadFile/test.pngaccess the file by
  • If the static directory structure is the same as that in the project, you can access it just like before accessing the jar package, which is equivalent to accessing the corresponding files through the directory structure.

full code

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";
    }

}

Guess you like

Origin blog.csdn.net/weixin_45277161/article/details/129858829