本文主要是介绍Minio实现文件上传、下载、预览、删除 修改,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
完成项目
AjaxResult 结果返回使用的 若依。
1. pom.xml
<!-- MinIO Client --><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.2.2</version></dependency>
2. application.yml
# Spring配置 spring:# minio配置servlet:multipart:enabled: true# 单个文件大小max-file-size: 100MB# 设置总上传的文件大小max-request-size: 200MBlocation: /usr/local/minio/data_file/temp_file/
注意:配置的存放文件的路径。
# minio配置 minio:endpoint: http://120.服务器:9001accessKey: minioadminsecretKey: minioadminbucket: jx-file
注意:endpoint- ip + 端口
账号 密码
bucket-创建的桶
3. Minio工具类
package com.ruoyi.xinzhi.utils;import io.minio.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.io.InputStream;/*** MinIO 服务*/
@Component
public class MinIOUtil {// 指定MinIO服务的访问地址(包括协议、域名或IP以及端口)private static String endpoint;// MinIO的访问密钥(Access Key),用于身份验证private static String accessKey;// MinIO的秘密密钥(Secret Key),与访问密钥配对使用,也是认证的一部分。private static String secretKey;// 指定默认的存储桶(Bucket)名称,MinIO中用于组织和存储对象(文件)的基本容器。private static String bucket;@Value("${minio.endpoint}")public void setEndpoint(String endpoint) {MinIOUtil.endpoint = endpoint;}@Value("${minio.accessKey}")public void setAccessKey(String accessKey) {MinIOUtil.accessKey = accessKey;}@Value("${minio.secretKey}")public void setSecretKey(String secretKey) {MinIOUtil.secretKey = secretKey;}@Value("${minio.bucket}")public void setBucket(String bucket) {MinIOUtil.bucket = bucket;}/*** 创建并返回一个配置好的MinioClient实例* 用于与MinIO服务器交互,上传文件、下载文件、删除文件** @return 配置*/private static MinioClient getMinioClient() {return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();}/*** 上传:将一个输入流中的文件上传到MinIO服务器上指定的存储桶(bucket)里** @param objectName .object(objectName)指定了上传后对象的名称。* @param inputStream 转换为 流* @param size 文件大小* @param contentType 内容类型* @throws Exception 异常*/public static void uploadFile(String objectName, InputStream inputStream, long size, String contentType) throws Exception {MinioClient minioClient = getMinioClient();minioClient.putObject(// .bucket(bucket) 指定了目标存储桶的名称。.object(objectName)指定了上传后对象的名称。PutObjectArgs.builder().bucket(bucket).object(objectName).stream(inputStream, size, -1)// .contentType(contentType)指定了上传文件的内容类型。.contentType(contentType).build());}/*** 下载:从MinIO服务器下载指定存储桶(bucket)中的文件** @param objectName 指定要从MinIO下载的文件对象名称(即文件路径和文件名)。* @return 流 对象* @throws Exception 异常*/public static InputStream downloadFile(String objectName) throws Exception {MinioClient minioClient = getMinioClient();return minioClient.getObject(GetObjectArgs.builder()// .bucket(bucket)指定了文件所在的存储桶名称。.bucket(bucket)// .object(objectName)指定了要下载的对象名称。.object(objectName).build());}/*** 预览:生成一个预签名的URL,允许用户通过浏览器或其他HTTP客户端以GET方法访问MinIO存储桶中指定对象(文件)的临时链接** @param objectName 指定需要获取预览链接的文件对象名称(包括路径)。* @return 对象* @throws Exception .method(Method.GET)指定了请求的方法为GET,这是预览文件时的标准HTTP方法。* .bucket(bucket)指定了存储桶的名称。* .object(objectName)指定了对象(文件)的名称。*/public static String getPreviewUrl(String objectName) throws Exception {MinioClient minioClient = getMinioClient();return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucket).object(objectName).build());}/*** 删除:从MinIO服务器上的指定存储桶中删除一个文件** @param objectName 指定要删除的文件对象名称(包括路径和文件名)* @throws Exception*/public static void deleteFile(String objectName) throws Exception {MinioClient minioClient = getMinioClient();minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());}
}
4. 实体类
package com.ruoyi.xinzhi.dto;import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 文件的实体类*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class JxAttachment {/** 文件ID */@TableId(value = "fileId")private Integer fileId;/** 文件名称 */private String fileName;/** 文件大小 */private String fileSize;/** 上传人员 */private String uploadingStaff;/** 上传时间 */private String uploadTime;/** 文件新名词 */private String fileNewName;/** 文件地址 */private String fileAddress;/** 部门编号 */private String deptNumber;}
package com.ruoyi.xinzhi.dto;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 文件预览DTO*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JxAttachmentPreviewDTO {/** 文件ID */private Integer fileId;/** 文件名称 */private String fileName;/** 文件大小 */private String fileSize;/** 上传人员 */private String uploadingStaff;/** 文件地址 */private String fileAddress;/** 文件新名词 */private String fileNewName;/** 上传时间 */private String uploadTime;}
5. Mapper层
package com.ruoyi.xinzhi.mapper;import com.ruoyi.xinzhi.dto.JxAttachment;
import com.ruoyi.xinzhi.dto.JxAttachmentPreviewDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;import java.util.List;/*** 文件*/
@Mapper
public interface JxFileMapper {/** 添加上传信息 */int insertFiles(JxAttachment attachment);/** 部门编号 查询文件信息 */List<JxAttachmentPreviewDTO> previewFileByDeptNum(String deptNumber);/** 文件删除 */void deleteFile(@Param("fileName") String fileName, @Param("deptNumber") String deptNumber);/**修改**/int updateFile(@Param("fileId") Integer fileId,@Param("fileName") String fileName,@Param("deptNumber") String deptNumber);
}
6. Service业务层
只需要实现类与Controller交互
package com.ruoyi.xinzhi.service.impl;import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.xinzhi.dto.JxAttachment;
import com.ruoyi.xinzhi.dto.JxAttachmentPreviewDTO;
import com.ruoyi.xinzhi.mapper.JxFileMapper;
import com.ruoyi.xinzhi.utils.MinIOUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;@Service
public class JxFileService {@Autowiredprivate JxFileMapper jxFileMapper;// @Value("${spring.servlet.multipart.location}")
// private String tempDir;@Transactionalpublic AjaxResult uploadFile(MultipartFile file, String deptNumber) throws Exception {String filename = file.getOriginalFilename(); // 获取文件名long size = file.getSize(); // 文件大小File tempDirectory = new File("/usr/local/minio/data_file");if (!tempDirectory.exists()) {tempDirectory.mkdir();}try (InputStream inputStream = file.getInputStream()) {// 获取文件扩展名String fileExtension = FilenameUtils.getExtension(filename);// 获取不带后缀文件名String baseName = FilenameUtils.getBaseName(filename);// 生成5位时间戳String timestamp = new SimpleDateFormat("ssSSS").format(new Date());// 生成唯一文件名String fileName = baseName + "_" + timestamp + "." + fileExtension;// 文件的路径String fileAddress = "/usr/local/minio/data_file/" + fileName;// 文件上传到 MinIO 客户端MinIOUtil.uploadFile(fileName, inputStream, size, file.getContentType());// 文件大小转换String fileSize = formatFileSize(size);// 信息存入数据库JxAttachment attachment = JxAttachment.builder().fileName(filename).fileSize(fileSize).uploadingStaff("TODO: Add Staff Name").uploadTime(formatDate(new Date())).fileNewName(fileName).fileAddress(fileAddress).deptNumber(deptNumber).build();jxFileMapper.insertFiles(attachment);return AjaxResult.success("上传成功");} catch (Exception e) {throw new RuntimeException("上传有误", e);}}public AjaxResult updateFile(Integer fileId, String fileName, String deptNumber) {try {int result = jxFileMapper.updateFile(fileId, fileName, deptNumber);if (result > 0) {return AjaxResult.success("文件更新成功");} else {return AjaxResult.error("文件更新失败");}} catch (Exception e) {return AjaxResult.error("文件更新过程中出现错误");}}public AjaxResult previewFile(String deptNumber) throws Exception {List<JxAttachmentPreviewDTO> attachments = jxFileMapper.previewFileByDeptNum(deptNumber);for (JxAttachmentPreviewDTO attachment : attachments) {String fileName = attachment.getFileNewName();String fileUrl = MinIOUtil.getPreviewUrl(fileName);attachment.setFileAddress(fileUrl);}return AjaxResult.success(attachments);}public AjaxResult deleteFile(String fileName, String deptNumber) {try {jxFileMapper.deleteFile(fileName, deptNumber);MinIOUtil.deleteFile(fileName);return AjaxResult.success("文件删除成功");} catch (Exception e) {return AjaxResult.error("文件删除失败");}}private String formatFileSize(long size) {DecimalFormat decimalFormat = new DecimalFormat("0.00");if (size < 1024) {return size + "B";} else if (size < 1024 * 1024) {return decimalFormat.format((double) size / 1024) + "KB";} else {return decimalFormat.format((double) size / (1024 * 1024)) + "MB";}}private String formatDate(Date date) {SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");return formatter.format(date);}
}
7. Controller控制层
package com.ruoyi.xinzhi.controller;import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.xinzhi.service.impl.JxFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;@RestController
@RequestMapping("/file")
public class JxFileController {@Autowiredprivate JxFileService jxFileService;/** 文件上传 */@PostMapping("/upload")public AjaxResult uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("deptNumber") String deptNumber) throws Exception {return jxFileService.uploadFile(file, deptNumber);}/** 文件预览 */@GetMapping("/preview")public AjaxResult previewFile(@RequestParam("deptNumber") String deptNumber) throws Exception {return jxFileService.previewFile(deptNumber);}/** 文件删除 */@DeleteMapping("/delete")public AjaxResult deleteFile(@RequestParam("fileName") String fileName,@RequestParam("deptNumber") String deptNumber) {return jxFileService.deleteFile(fileName, deptNumber);}
// 修改@PutMapping("/update")public AjaxResult updateFile(@RequestParam("fileId") Integer fileId,@RequestParam("file") MultipartFile file,@RequestParam("deptNumber") String deptNumber) {return jxFileService.updateFile(fileId, String.valueOf(file), deptNumber);}
}
sql语句chua
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.xinzhi.mapper.JxFileMapper"><resultMap id="attachment" type="com.ruoyi.xinzhi.dto.JxAttachment"><id property="fileId" column="file_id"/><result property="fileName" column="file_name"/><result property="fileSize" column="file_size"/><result property="uploadingStaff" column="uploading_staff"/><result property="uploadTime" column="upload_time"/><result property="fileNewName" column="file_new_name"/><result property="fileAddress" column="file_address"/><result property="deptNumber" column="dept_number"/></resultMap><!-- 添加上传信息 --><insert id="insertFiles">insert into jx_attachment (file_id, file_name, file_size, uploading_staff, upload_time, file_new_name,file_address, dept_number)values (#{fileId}, #{fileName}, #{fileSize}, #{uploadingStaff}, #{uploadTime}, #{fileNewName}, #{fileAddress},#{deptNumber})</insert>
<!-- 修改 --><update id="updateFile">update jx_attachmentset file_name = #{fileName}where file_id = #{fileId} and dept_number = #{deptNumber};</update><!-- 部门编号 查询文件信息 --><select id="previewFileByDeptNum" resultType="com.ruoyi.xinzhi.dto.JxAttachmentPreviewDTO">select file_id as fileId,file_name as fileName,file_size as fileSize,uploading_staff as uploadingStaff,file_address as fileAddress,upload_time as uploadTime,file_new_name as fileNewNamefrom jx_attachmentwhere dept_number = #{deptNumber};</select><!-- 文件删除 --><delete id="deleteFile">delete from jx_attachment where dept_number = #{deptNumber} and file_new_name = #{fileName};</delete></mapper>
表
CREATE TABLE `jx_attachment` (`file_id` int NOT NULL AUTO_INCREMENT COMMENT '文件ID',`file_name` varchar(255) NOT NULL COMMENT '文件名称',`file_size` varchar(50) DEFAULT NULL COMMENT '文件大小',`uploading_staff` varchar(100) DEFAULT NULL COMMENT '上传人员',`upload_time` datetime DEFAULT NULL COMMENT '上传时间',`file_new_name` varchar(255) DEFAULT NULL COMMENT '文件新名词',`file_address` varchar(255) DEFAULT NULL COMMENT '文件地址',`dept_number` varchar(50) DEFAULT NULL COMMENT '部门编号',PRIMARY KEY (`file_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='文件表';
CREATE TABLE `JxAttachmentPreview` (`file_id` int NOT NULL AUTO_INCREMENT COMMENT '文件ID',`file_name` varchar(255) NOT NULL COMMENT '文件名称',`file_size` varchar(50) DEFAULT NULL COMMENT '文件大小',`uploading_staff` varchar(100) DEFAULT NULL COMMENT '上传人员',`file_address` varchar(255) DEFAULT NULL COMMENT '文件地址',`file_new_name` varchar(255) DEFAULT NULL COMMENT '文件新名称',`upload_time` datetime DEFAULT NULL COMMENT '上传时间',PRIMARY KEY (`file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='JxAttachmentPreview 表,用于存储文件的相关信息';
这篇关于Minio实现文件上传、下载、预览、删除 修改的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!