SysFileController.java 3.27 KB
package com.pashanhoo.qys.controller;


import com.pashanhoo.common.Result;
import com.pashanhoo.qys.service.SysFileService;
import io.minio.GetObjectArgs;
import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.messages.Item;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @author CAIYONGSONG
 * @commpany www.pashanhoo.com
 * @date 2022/7/20
 */

@Api(tags = "Minio文件上传")
@RestController
@RequestMapping("/system/file")
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;

    @Autowired
    private MinioClient minioClient;

    /**
     * 图片上传minio
     *
     * @param file 图片文件
     * @return 返回
     */
    @ApiOperation("点选文件-文件上传")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Result uploadFileMinio(MultipartFile file) {
        String url = sysFileService.uploadFileMinio(file);
        if (!StringUtils.isEmpty(url)) {
            return Result.ok(url);
        }
        return Result.error(-1,"上传失败");
    }

    @PostMapping(value = "/upload-files" )
    @ApiOperation(value = "文件上传(支持批量)", notes = "文件上传(支持批量)")
    public Result uploadFiles(@RequestPart MultipartFile[] files ) throws IOException {
        String url = sysFileService.uploadFileMinio(files);
        if (!StringUtils.isEmpty(url)) {
            return Result.ok(url);
        }
        return Result.error(-1,"上传失败");
    }

    // 暂未实现
    @ApiOperation("文件下载")
    @RequestMapping(value = "/download", method = RequestMethod.POST)
    public String download(HttpServletResponse response) {
        try {
            Iterable<io.minio.Result<Item>> results = minioClient.listObjects(
                    ListObjectsArgs.builder().bucket("eci-bucket").build()//获取bucket里所有文件信息
            );
            String fileName = null;
            for (io.minio.Result<Item> result : results) {
                Item item = result.get();
                fileName = item.objectName();
                System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
            }

            InputStream object = minioClient.getObject(GetObjectArgs.builder().bucket("eci-bucket").object(fileName).build());
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            IOUtils.copy(object, bos);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            bos.flush();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

}