SpringBoot整合Hutool实现文件上传下载

时间:2025-03-10 11:51:52

前言

我相信我们在日常开发中,难免会遇到对各种媒体文件的操作,由于业务需求的不同对文件操作的代码实现也大不相同

数据库设计

/*
 Navicat Premium Data Transfer

 Source Server         : MySQL 5.5
 Source Server Type    : MySQL
 Source Server Version : 50554 (5.5.54)
 Source Host           : localhost:3306
 Source Schema         : tgadmin

 Target Server Type    : MySQL
 Target Server Version : 50554 (5.5.54)
 File Encoding         : 65001

 Date: 20/06/2023 03:07:47
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sys_file
-- ----------------------------
DROP TABLE IF EXISTS `sys_file`;
CREATE TABLE `sys_file`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '文件id',
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件名',
  `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件类型',
  `size` bigint(20) NULL DEFAULT NULL COMMENT '文件大小(kb)',
  `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '访问路径',
  `location` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件地址',
  `download` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '下载地址',
  `md5` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '文件md5',
  `is_Delete` tinyint(1) NULL DEFAULT 0 COMMENT '是否删除 0:未删除 1:删除',
  `enable` tinyint(1) NULL DEFAULT 1 COMMENT '是否禁用链接  1:可用  0:禁用用',
  `upload_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上传时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uni_md5`(`md5`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 64 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '文件表' ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

yaml配置我们的上传路径

# 文件上传配置
files:
  ip: localhost
  upload:
    location: file:F:/项目/SpringBoot+vue/tg-admin/server/files/
    path: /img/**

上传

maven配置

<!--        hutool-->
        <dependency>
            <groupId></groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.15</version>
        </dependency>
<!--        MybatisPlus-->
        <dependency>
            <groupId></groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>

文件类

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
 * @Program: admin
 * @ClassName File
 * @Author: liutao
 * @Description: 文件
 * @Create: 2023-03-16 18:51
 * @Version 1.0
 **/

@Data
@TableName("sys_file")
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("文件表")
public class Files implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("文件id")
    @TableId(type = )
    private Integer id;

    @ApiModelProperty("文件名称")
    private String name;

    @ApiModelProperty("文件类型")
    private String type;

    @ApiModelProperty("文件大小")
    private Long size;

    @ApiModelProperty("文件地址")
    private String location;

    @ApiModelProperty("访问url")
    private String url;

    @ApiModelProperty("开启状态")
    private String download;

    @ApiModelProperty("是否删除")
    private Boolean isDelete;

    @ApiModelProperty("文件md5")
    private String md5;

    @ApiModelProperty("开启状态")
    private Boolean enable;

    @ApiModelProperty("上传时间")
    private Timestamp uploadTime;
}

文件接口 

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import org.;
import org.;
import ;
import ;
import .*;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 * @Program: admin
 * @ClassName: FileController
 * @Author: liutao
 * @Description: 文件处理
 * @Create: 2023-03-16 18:15
 * @Version 1.0
 **/
@Api(tags = "文件接口")
@RestController
@RequestMapping("/file")
public class FileController {
    private static final Logger log = ();
    @Value("${}")
    private String ip;
    @Value("${}")
    private String port;
    @Value("${}")
    private String path;
    @Value("${}")
    private String fileUploadPath;

    @Autowired
    private FileService fileService;

    @ApiOperation("分页查询所有文件信息")
    @GetMapping("/page")
    public Result<Files> findPage(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String name,
                                  @RequestParam String type) {
        IPage<Files> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Files> queryWrapper = new QueryWrapper<>();
        ("is_Delete", false);
        if (!"".equals(name)) {
            ("name", name);
        }
        if (!"".equals(type)) {
            ("type", type);
        }
        User currentUser = ();
        ("当前用户------{}", currentUser);
        return ((page, queryWrapper));
    }


    @ApiOperation("根据id删除文件")
    @DeleteMapping("/{id}")
    public Result<Files> delete(@PathVariable Integer id) {
        Files files = (id);
        (true);
        (files);
        return ();
    }


    @ApiOperation("根据id批量删除文件")
    @PostMapping("del/batch")
    public Result<Files> deleteBatch(@RequestBody List<Integer> ids) {
        QueryWrapper<Files> queryWrapper = new QueryWrapper<Files>();
        ("id", ids);
        List<Files> files = (queryWrapper);
        (file -> {
            (true);
            (file);
        });
        return ();
    }

    @ApiOperation(value = "更新或新增", httpMethod = "POST")
    @PostMapping("/update")
    public Result<Files> save(@RequestBody Files files) {
        return ((files));
    }


    /**
     * @MethodName: upload
     * @description: 文件上传
     * @Author: LiuTao
     * @Param: [file]
     * @UpdateTime: 2023/3/16 18:39
     * @Return: 
     * @Throw: IOException
     **/
    @ApiOperation("上传文件接口")
    @PostMapping("/upload")
    public Result<Files> upload(@RequestParam MultipartFile file,
                                HttpServletRequest request) throws IOException {
        // 文件原始名
        String originalFilename = ();
        // 文件类型
        String type = (originalFilename);
        // 文件大小
        long size = ();
        String today = ().replace("-", "/");
        // 定义一个文件唯一的标识码
        String fileUUID = () +  + type;
        // 下载地址
        String download = () + "://" + ip + ":" + port + "/file/" + fileUUID;
        // 重命名文件
        fileUploadPath = ("file:", "");
        path = ("**", "") + today + StrUtil.C_SLASH;
        // 判断目录是否存在。不存在就创建
        if (!(fileUploadPath + today)) {
            (fileUploadPath + today);
        }
        // 上传的文件
        File uploadFile = new File(fileUploadPath + today + StrUtil.C_SLASH + fileUUID);
        (fileUploadPath);
        // 文件存入磁盘
        (uploadFile);
        // 获取文件md5
        String md5 = SecureUtil.md5(uploadFile);
        // 查询数据库有没有当前md5
        Files one = getFileMd5(md5);
        String url;
        if (one != null) {
            ();
            return ("文件重复", ());
        } else {
            url = () + "://" + ip + ":" + port + path + fileUUID;
        }
        // 存入数据库
        Files saveFile = new Files();
        (originalFilename);
        (type);
        (size / 1024);
        (());
        (url);
        (download);
        saveFile.setMd5(md5);
        (saveFile);
        return (url);
    }

    @ApiOperation("下载文件接口")
    @GetMapping("/{fileUUID}")
    public void download(@PathVariable String fileUUID,
                         HttpServletResponse response) throws IOException {
        Files one = fileService
                .lambdaQuery()
                .like(Files::getLocation, fileUUID)
                .one();
        File uploadFile = new File(());
        ServletOutputStream outputStream = ();
        ("Content-Disposition", "attachment;filename =" + (fileUUID, "UTF-8"));
        ("application/octet-stream");
        byte[] bytes = (uploadFile);
        (bytes);
        ();
        ();
    }

    private Files getFileMd5(String md5) {
        QueryWrapper<Files> wrapper = new QueryWrapper<>();
        ("md5", md5);
        List<Files> list = (wrapper);
        return () == 0 ? null : (0);
    }
}

配置静态资源映射

    @Value("${}")
    private String filePath;
    @Value("${}")
    private String fileLocation;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //注册配置类,使用addResourceHandlers方法,将本地路径fileLocation映射到filePath路由上。
        (filePath).addResourceLocations(fileLocation);
        (registry);
    }