SysFileServiceImpl.java 3.45 KB
package com.pashanhoo.qys.service.impl;

import com.pashanhoo.common.minio.FileServerOptUtil;
import com.pashanhoo.common.minio.MinioConfig;
import com.pashanhoo.common.minio.MinioUtil;
import com.pashanhoo.qys.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author CAIYONGSONG
 * @commpany www.pashanhoo.com
 * @date 2022/7/20
 */
@Service
@Slf4j
public class SysFileServiceImpl implements SysFileService {

    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private MinioUtil minioUtil;

    @Override
    public String uploadFileMinio(MultipartFile[] files){
        Long st = System.currentTimeMillis();
        log.info("开始上传批量文件" + files.length);
        // 判断存储桶是否存在
        if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
            minioUtil.makeBucket(minioConfig.getBucketName());
        }
        StringBuilder sbf = new StringBuilder();
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                throw new RuntimeException("文件不存在!");
            }
            log.info("循环开始上传单个文件,文件大小:" + file.getSize());

            // 生成文件名  按照要求-带文件夹路径的文件的全路径
            String fineName = FileServerOptUtil.extractFilename(file);
            try {
                // 上传文件
                minioUtil.upload(file, fineName, minioConfig.getBucketName());
            } catch (Exception e) {
                System.out.println("上传minio服务器期出错了 " + e.getMessage());
                e.printStackTrace();
                return null;
            }
            sbf.append(minioConfig.getMinioUrl()).append("/").append( minioConfig.getBucketName())
                    .append("/").append(fineName).append(",");
        }
        //String url = minioConfig.getMinioUrl() + "/" + minioConfig.getBucketName() + "/" + fineName;
        String resUrl = sbf.toString().substring(0,(sbf.length()-1));
        log.info("上传文件结束,总耗时:"+(System.currentTimeMillis() - st) +"s, 文件路径:" + resUrl  );
        return resUrl;

    }

    @Override
    public String uploadFileMinio(MultipartFile file) {
        Long st = System.currentTimeMillis();
        if (file.isEmpty()) {
            throw new RuntimeException("文件不存在!");
        }
        log.info("开始上传单个文件,文件大小:" + (file.getSize()/1024) + "KB");
        // 判断存储桶是否存在
        if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
            minioUtil.makeBucket(minioConfig.getBucketName());
        }
        // 生成文件名  按照要求-带文件夹路径的文件的全路径
        String fineName = FileServerOptUtil.extractFilename(file);
        try {
            // 上传文件
            minioUtil.upload(file, fineName, minioConfig.getBucketName());
        } catch (Exception e) {
            System.out.println("上传minio服务器期出错了 " + e.getMessage());
            e.printStackTrace();
            return null;
        }

        String url = minioConfig.getMinioUrl() + "/" + minioConfig.getBucketName() + "/" + fineName;

        log.info("上传文件结束,总耗时:"+(System.currentTimeMillis() - st) +"ms, 文件路径:" + url  );
        return url;

    }
}