chenjiahe
2022-06-16 0cbabb6f4b9e00c1c659674a93db9feb5e62b33b
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
126
package com.hx.util;
 
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
 
public class OSSUtil {
 
    private static OSSClient ossClient;
 
    public static OSSClient getOSSClient(String keyId, String keySecret, String endPoint)
    {
        if(ossClient == null)
        {
            ossClient = new OSSClient(endPoint, keyId, keySecret);
        }
        
        return ossClient;
    }
    
    /** 后端调用上传图片 */
    public static String uploadImg(String fileName, File imgFile, String keyId, String keySecret, String endPoint, String bucket) throws IOException {
 
        // // 压缩到文件夹
        // Thumbnails.of(imgFile).scale(1f).outputQuality(0.8f).toFile(SimpleToof.getRealpath("/upload/image/")
        // + imgName);
        // File file = new File(SimpleToof.getRealpath("/upload/image/") + imgName);
 
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret);
        // 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
        // 判断压缩后的大小
        // if (file.length() > imgFile.length()) {
        ossClient.putObject(bucket, fileName, imgFile);
        // } else {
        // ossClient.putObject(BUCKET, fileName, file);
        // }
        // 关闭OSSClient。
        ossClient.shutdown();
        
        return "https://" + bucket + "." + endPoint + "/" + fileName;
    }
 
    /** 后端调用上传图片 */
    public static String uploadImg(String fileName, MultipartFile imgFile, String keyId, String keySecret, String endPoint, String bucket) {
 
        try {
            // 创建OSSClient实例。
            OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret);
            ossClient.putObject(bucket, fileName, imgFile.getInputStream());
            ossClient.shutdown();
 
            return "https://" + bucket + "." + endPoint + "/" + fileName;
        }catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
 
    /** 后端调用上传图片 */
    public static String uploadImg(String fileName, InputStream is, String keyId, String keySecret, String endPoint, String bucket) {
 
        try {
            // 创建OSSClient实例。
            OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret);
            ossClient.putObject(bucket, fileName, is);
            ossClient.shutdown();
 
            return "https://" + bucket + "." + endPoint + "/" + fileName;
        }catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
 
    public static byte[] getObject(String key, String keyId, String keySecret, String endPoint, String bucket) {
        
        byte[] resultByte = null;
        InputStream is = null;
        ByteArrayOutputStream swapStream = null;
        try {
            OSSObject ossObject = getOSSClient(keyId, keySecret, endPoint).getObject(bucket, key);
            is = ossObject.getObjectContent();
            swapStream = new ByteArrayOutputStream(); 
            byte[] buff = new byte[1024]; //buff用于存放循环读取的临时数据 
            int rc = 0; 
            while ((rc = is.read(buff, 0, 1024)) > 0) { 
                swapStream.write(buff, 0, rc); 
            } 
            
            resultByte = swapStream.toByteArray(); //in_b为转换之后的结果
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null)
            {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                is = null;
            }
            
            if(swapStream != null)
            {
                try {
                    swapStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                swapStream = null;
            }
        }
        
        return resultByte;
    }
}