mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
122
123
124
125
package com.panzhihua.grid_app.config;
 
import io.minio.MinioClient;
import io.minio.ObjectStat;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;
 
/**
 * program 攀枝花智慧社区项目 description minio工具类
 *
 * @author manailin Date 2021-08-19 15:30
 **/
@Component
@EnableConfigurationProperties(MinioConfig.class)
public class MinioUtil {
 
    @Resource
    private MinioConfig minioProperties;
 
    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
 
    /**
     * 文件上传
     *
     * @param file
     *            要上传的文件
     * @return
     */
    public String upload(MultipartFile file, String name) {
        if (null != file) {
            try {
                UUID uuid = UUID.randomUUID();
                StringBuilder s = new StringBuilder();
                s.append(uuid.toString().replace("-", "")).append("/");
                MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(),
                    minioProperties.getSecretKey());
                // bucket 不存在,创建
                if (!minioClient.bucketExists(minioProperties.getBucket())) {
                    minioClient.makeBucket(minioProperties.getBucket());
                }
                // 得到文件流
                InputStream input = file.getInputStream();
                // 文件名
                // String fileName = uuid + "/images." + FilenameUtils.getExtension(file.getOriginalFilename());
                String fileName = s.append(name).toString();
                String contentType = file.getContentType();
                minioClient.putObject(minioProperties.getBucket(), fileName, input, contentType);
                StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl());
                String url = fileUrl.append(fileName).toString();
                return url;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    /**
     * 文件下载
     *
     * @param response
     * @param url
     */
    public void download(HttpServletResponse response, String url) {
        // 从链接中得到文件名
        String replace = url.replace(minioProperties.getBucket() + "/", "#");
        String fileName = replace.split("#")[1];
        InputStream inputStream;
        try {
            MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
            ObjectStat stat = minioClient.statObject(minioProperties.getBucket(), fileName);
            inputStream = minioClient.getObject(minioProperties.getBucket(), fileName);
            response.setContentType(stat.contentType());
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            IOUtils.copy(inputStream, response.getOutputStream());
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * 文件下载
     *
     * @param response
     * @param url
     */
    public byte[] getBytes(HttpServletResponse response, String url) {
        // 从链接中得到文件名
        String replace = url.replace(minioProperties.getBucket() + "/", "#");
        String fileName = replace.split("#")[1];
        InputStream inputStream;
        byte[] bytes = new byte[0];
        try {
            MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
            inputStream = minioClient.getObject(minioProperties.getBucket(), fileName);
            bytes = toByteArray(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;
    }
}