当前位置: 首页 > article >正文

Minio文件服务器:SpringBoot实现文件上传

在Minio文件服务器部署成功后(参考上篇文章Minio文件服务器:安装)接下来我们通过SpringBoot框架写一个接口,来实现文件的上传功能:文件通过SpringBoot接口,上传到Minio文件服务器。并且,如果上传的文件是图片类型,也要实现能够预览上传后的图片。

1 完成后的项目结构

在这里插入图片描述

2 代码

2.1 MinioserverApplication.java

package com.example.minioserver;

import com.example.minioserver.config.MinioProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@EnableConfigurationProperties(value = {MinioProperties.class})
@SpringBootApplication
public class MinioserverApplication {

	public static void main(String[] args) {
		SpringApplication.run(MinioserverApplication.class, args);
	}

}

2.2 MinioProperties.java

package com.example.minioserver.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author: xbronze
 * @date: 2024-10-26 17:04
 * @description: TODO
 */
@Data
@ConfigurationProperties(prefix="minio") //读取节点
public class MinioProperties {

    private String endpointUrl;
    private String accessKey;
    private String secreKey;
    private String bucketName;

}

2.3 ViewContentType.java

package com.example.minioserver.enums;

import cn.hutool.core.util.StrUtil;

public enum ViewContentType {

    DEFAULT("default","application/octet-stream"),
    JPG("jpg", "image/jpeg"),
    TIFF("tiff", "image/tiff"),
    GIF("gif", "image/gif"),
    JFIF("jfif", "image/jpeg"),
    PNG("png", "image/png"),
    TIF("tif", "image/tiff"),
    ICO("ico", "image/x-icon"),
    JPEG("jpeg", "image/jpeg"),
    WBMP("wbmp", "image/vnd.wap.wbmp"),
    FAX("fax", "image/fax"),
    NET("net", "image/pnetvue"),
    JPE("jpe", "image/jpeg"),
    RP("rp", "image/vnd.rn-realpix");

    private String prefix;

    private String type;

    public static String getContentType(String prefix){
        if(StrUtil.isEmpty(prefix)){
            return DEFAULT.getType();
        }
        prefix = prefix.substring(prefix.lastIndexOf(".") + 1);
        for (ViewContentType value : ViewContentType.values()) {
            if(prefix.equalsIgnoreCase(value.getPrefix())){
                return value.getType();
            }
        }
        return DEFAULT.getType();
    }

    ViewContentType(String prefix, String type) {
        this.prefix = prefix;
        this.type = type;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getType() {
        return type;
    }

}

2.4 FileUploadService.java

package com.example.minioserver.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * @author: xbronze
 * @date: 2024-10-26 16:59
 * @description: TODO
 */
public interface FileUploadService {

    public String fileUpload(MultipartFile multipartFile);

}

2.5 FileUploadServiceImpl.java

package com.example.minioserver.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.example.minioserver.config.MinioProperties;
import com.example.minioserver.enums.ViewContentType;
import com.example.minioserver.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author: xbronze
 * @date: 2024-10-26 17:00
 * @description: TODO
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String fileUpload(MultipartFile multipartFile) {
        try {
            // 创建一个Minio的客户端对象
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioProperties.getEndpointUrl())
                    .credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey())
                    .build();

            // 判断桶是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs
            	.builder()
            	.bucket(minioProperties.getBucketName())
            	.build());
            if (!found) {       // 如果不存在,那么此时就创建一个新的桶
                minioClient.makeBucket(MakeBucketArgs
                	.builder()
                	.bucket(minioProperties.getBucketName())
                	.build());
            } else {  // 如果存在打印信息
                System.out.println("Bucket " 
                	+ minioProperties.getBucketName() 
                	+ " already exists.");
            }

            // 设置存储对象名称
            String dateDir = DateUtil.format(new Date(), "yyyyMMdd");
            String uuid = UUID.randomUUID().toString().replace("-", "");
            //20230801/443e1e772bef482c95be28704bec58a901.jpg
            String fileName = dateDir+"/"+uuid+multipartFile.getOriginalFilename();
            System.out.println(fileName);

            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
                    .object(fileName)
                    // 上传时指定对应对ContentType
                    // Content-Type 用于向接收方说明传输资源的媒体类型,
                    // 从而让浏览器用指定码表去解码。
                    .contentType(ViewContentType.getContentType(fileName))
                    .build();
            minioClient.putObject(putObjectArgs) ;

            return minioProperties.getEndpointUrl() 
            + "/" + minioProperties.getBucketName() 
            + "/" + fileName ;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

2.6 FileUploadController.java

package com.example.minioserver.controller;

import com.example.minioserver.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: xbronze
 * @date: 2024-10-26 16:59
 * @description: TODO
 */
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService ;

    @PostMapping(value = "/upload")
    public Map<String,String> fileUploadService(
    		@RequestParam(value = "file") MultipartFile multipartFile) {
        Map<String, String> map = new HashMap<>();
        String fileUrl = fileUploadService.fileUpload(multipartFile);
        map.put("fileurl",fileUrl);
        return map;
    }

}

2.7 application.yml

spring:
  application:
    name: minioserver
server:
  port: 8091

# 部署的minio服务
minio:
  endpointUrl: http://192.168.72.135:9100
  accessKey: admin
  secreKey: admin123
  bucketName: test

2.8 pom.xml

这里只展示引入的依赖

<dependencies>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
	    <groupId>org.projectlombok</groupId>
	    <artifactId>lombok</artifactId>
	    <optional>true</optional>
	</dependency>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-test</artifactId>
	    <scope>test</scope>
	</dependency>

	<!-- https://mvnrepository.com/artifact/io.minio/minio -->
	<dependency>
		<groupId>io.minio</groupId>
		<artifactId>minio</artifactId>
		<version>8.5.10</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
	<dependency>
	    <groupId>cn.hutool</groupId>
	    <artifactId>hutool-all</artifactId>
	    <version>5.8.32</version>
	</dependency>

</dependencies>

3 运行

访问文件上传接口,选择一张图片上传,接口请求成功后,会返回一个url链接。

在这里插入图片描述

通过链接可预览上传的图片。

在这里插入图片描述


http://www.kler.cn/news/368259.html

相关文章:

  • 【gRPC】什么是RPC——介绍一下RPC
  • nodejs 基础
  • 在Spring Boot中配置Map类型数据
  • JCSA-Journal of Consumer Affairs
  • 解读数字化转型的敏捷架构:从理论到实践的深度分析
  • 编写一个简单的Iinput_dev框架
  • 程序设计基础I-单元测试4(机测+编程题)
  • Oracle SQL练习题,从小白到入门 - 上
  • uniapp通过id获取dom的宽度,高度,位置等(应该是 任意平台都通用 )
  • member access within null pointer of type ‘ListNode‘
  • 在浏览器里就可以运行的本地AI模型 - 一键去除图片背景AI
  • Handler、Looper、message进阶知识
  • Tkinter包文件对话框模块中的FileDialog类简介
  • C语言:水仙花树,要求三位以上的N位整数每位的N次方等于数本身,全部输出出来
  • 标题:机器学习实战:从理论到应用的深度探索
  • react18中的useEffect和useLayoutEffect的原理分析
  • 多楼层智能穿梭:转运机器人助力制造业转型升级
  • Golang | Leetcode Golang题解之第513题找树左下角的值
  • ASP.NET Core开发Chatbot API
  • 算法2—八大常用排序算法(下)
  • 深度探索C++对象模型
  • Unity编辑器制作多级下拉菜单
  • C++二级2023.9题及答案 -- 部分题
  • PHP短视频实训平台系统小程序源码
  • c# Solidworks二次开发---添加属性
  • 【mod分享】极品飞车10魔改模组,全新UI,全新道路,全新建筑,高清植被,全新的道路围栏,全新的天空,体验另一种速度与激情