A way to save space when springboot uploads the same file (only one file is kept in the local storage path, but there can be multiple records in the database)

Problem Description

When we upload files, we will encounter these situations:

  • If the file is uploaded twice, the file name of the file may be different, but the uploaded file is the same
  • File uploaded twice

If no judgment is made, the same file may be stored multiple times, causing the storage space to be occupied.

Solutions

When a user uploads a file, there can be multiple records in the database, but only one file is allowed to be stored in the storage space.

The chosen method is to set an md5 value. When the file is being uploaded, an md5 value is obtained from the file upload, and then the md5 of the file upload is changed to the database to find the record. If there is the same md5 value, the newly uploaded file is deleted, and at the same time Write the file url with the same md5 value into this record.

Code

First, the database design is as follows:

An md5 field is required.

Configure a file path in application.yml:

 

Backend upload file interface:

Get the file path first:

Then upload the interface:

    /**
     * 文件上传接口
     * @Param file 前端传递过来的文件
     * @Return Result封装返回
     * @throws IOException
     * */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<?> upload(@RequestParam MultipartFile file) throws IOException {
        //获取文件上传时的文件名
        String originalFilename = file.getOriginalFilename();
        //获取文件类型
        String type = FileUtil.extName(originalFilename);
        //获取文件大小
        long size = file.getSize();

        //定义一个文件唯一的标识码(作为在本地存储的文件名)
        String uuid = IdUtil.fastSimpleUUID();
        String fileUUID = uuid + StrUtil.DOT + type;
        //就是文件对象的路径名    例子:D:/studyboot/qingge/images/uuid.jpg
        File uploadFile = new File(fileUploadPath + fileUUID);
        //获取父目录
        File parentFile = uploadFile.getParentFile();
        //判断配置的文件目录是否存在,若不存在就创建一个新的文件目录
        if (parentFile.exists()){
            parentFile.mkdirs();
        }

        //当文件存在的时候再去获取文件的md5
        String md5;
        String url;

        //把获取到的文件存储到磁盘目录去
        file.transferTo(uploadFile);
        //储存进去之后就能获取到文件的md5
        md5 = SecureUtil.md5(uploadFile);
        //从数据库查询文件的md5是否存在
        Files dbFile = getFileByMd5(md5);
        if (dbFile!=null){
            url = dbFile.getUrl();
            //由于文件已经存在,所以删除刚才上传的重复文件
            uploadFile.delete();
        } else {
            //若数据库不存在重复文件,则不删除刚才上传的文件
            url = "http://localhost:9090/file/"+ fileUUID;
        }
        //存储数据库中
        Files saveFile = new Files();
        saveFile.setName(originalFilename);
        saveFile.setSize(size);
        saveFile.setType(type);
        saveFile.setUrl(url);
        saveFile.setMd5(md5);
        fileMapper.insert(saveFile);
        return Result.success(url);
    }

 

test

We upload three pictures with postman

View records in the database

 

 It can be found that the md5 values ​​​​are the same

Then let's go to the local file storage to see:

In fact, there is only one picture, so this will save space for picture storage. 

Guess you like

Origin blog.csdn.net/Wannabe_hacker/article/details/126138485