SZH
2022-08-09 e8a368c35fa8c6d65ca09ee023ce490bc11b2ea3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.gkhy.safePlatform.minioDemo.utils;
 
import com.gkhy.safePlatform.minioDemo.config.MinioConfig;
import io.minio.*;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.minio.http.Method;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
 
@Configuration
public class FileUtil {
 
    @Resource
    private MinioConfig minioConfig;
 
    @Autowired
    private MinioClient minioClient;
 
 
    /**
     * 文件下载
     * @param fileName
     * @param response
     */
    public void downloadFile(String fileName, HttpServletResponse response) {
        try {
            InputStream file = minioClient.getObject(GetObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .build());
            String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            ServletOutputStream servletOutputStream = response.getOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = file.read(buffer)) > 0) {
                servletOutputStream.write(buffer, 0, len);
            }
            servletOutputStream.flush();
            file.close();
            servletOutputStream.close();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 文件上传
     *
     * @param file       文件
     * @param fileName   存储的文件名称
     */
    public String upload(MultipartFile file, String fileName) {
        String url;
        try {
            PutObjectArgs objectArgs = PutObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(fileName)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build();
            //文件名称相同会覆盖
            minioClient.putObject(objectArgs);
            url = getFileUrl(fileName);
        } catch (Exception e) {
            e.printStackTrace();
            url = "";
        }
        return url;
    }
 
    /**
     * 文件访问路径
     *
     * @param objectName 存储桶里的对象名称
     * expiry :链接失效时间,秒为单位
     * @return
     */
    public String getFileUrl(String objectName) {
        String url = null;
        try {
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(minioConfig.getBucketName())
                    .object(objectName)
                    .expiry(60).build());
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | ServerException e) {
            e.printStackTrace();
        }
        return url;
    }
 
 
    /**
     * 检查存储桶是否存在
     *
     * @return
     */
    @SneakyThrows
    public boolean bucketCheck() {
        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder()
                .bucket(minioConfig.getBucketName()).build());
        return flag;
    }
 
}