chenjiahe
2022-12-16 ca698f88c0632a85bca91c7ea12267c83889f673
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import com.aliyun.oss.OSSClient;
4 import com.aliyun.oss.model.OSSObject;
5 import org.springframework.web.multipart.MultipartFile;
6
7 import java.io.ByteArrayOutputStream;
8 import java.io.File;
9 import java.io.IOException;
10 import java.io.InputStream;
11
12 public class OSSUtil {
13
14     private static OSSClient ossClient;
15
16     public static OSSClient getOSSClient(String keyId, String keySecret, String endPoint)
17     {
18         if(ossClient == null)
19         {
20             ossClient = new OSSClient(endPoint, keyId, keySecret);
21         }
22         
23         return ossClient;
24     }
25     
26     /** 后端调用上传图片 */
27     public static String uploadImg(String fileName, File imgFile, String keyId, String keySecret, String endPoint, String bucket) throws IOException {
28
29         // // 压缩到文件夹
30         // Thumbnails.of(imgFile).scale(1f).outputQuality(0.8f).toFile(SimpleToof.getRealpath("/upload/image/")
31         // + imgName);
32         // File file = new File(SimpleToof.getRealpath("/upload/image/") + imgName);
33
34         // 创建OSSClient实例。
35         OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret);
36         // 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
37         // 判断压缩后的大小
38         // if (file.length() > imgFile.length()) {
39         ossClient.putObject(bucket, fileName, imgFile);
40         // } else {
41         // ossClient.putObject(BUCKET, fileName, file);
42         // }
43         // 关闭OSSClient。
44         ossClient.shutdown();
45         
46         return "https://" + bucket + "." + endPoint + "/" + fileName;
47     }
48
49     /** 后端调用上传图片 */
50     public static String uploadImg(String fileName, MultipartFile imgFile, String keyId, String keySecret, String endPoint, String bucket) {
51
52         try {
53             // 创建OSSClient实例。
54             OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret);
55             ossClient.putObject(bucket, fileName, imgFile.getInputStream());
56             ossClient.shutdown();
57
58             return "https://" + bucket + "." + endPoint + "/" + fileName;
59         }catch (Exception e)
60         {
61             e.printStackTrace();
62             return null;
63         }
64     }
65
66     /** 后端调用上传图片 */
67     public static String uploadImg(String fileName, InputStream is, String keyId, String keySecret, String endPoint, String bucket) {
68
69         try {
70             // 创建OSSClient实例。
71             OSSClient ossClient = new OSSClient("https://" + endPoint, keyId, keySecret);
72             ossClient.putObject(bucket, fileName, is);
73             ossClient.shutdown();
74
75             return "https://" + bucket + "." + endPoint + "/" + fileName;
76         }catch (Exception e)
77         {
78             e.printStackTrace();
79             return null;
80         }
81     }
82
83     public static byte[] getObject(String key, String keyId, String keySecret, String endPoint, String bucket) {
84         
85         byte[] resultByte = null;
86         InputStream is = null;
87         ByteArrayOutputStream swapStream = null;
88         try {
89             OSSObject ossObject = getOSSClient(keyId, keySecret, endPoint).getObject(bucket, key);
90             is = ossObject.getObjectContent();
91             swapStream = new ByteArrayOutputStream(); 
92             byte[] buff = new byte[1024]; //buff用于存放循环读取的临时数据 
93             int rc = 0; 
94             while ((rc = is.read(buff, 0, 1024)) > 0) { 
95                 swapStream.write(buff, 0, rc); 
96             } 
97             
98             resultByte = swapStream.toByteArray(); //in_b为转换之后的结果
99             
100         } catch (Exception e) {
101             e.printStackTrace();
102         } finally {
103             if(is != null)
104             {
105                 try {
106                     is.close();
107                 } catch (IOException e) {
108                     e.printStackTrace();
109                 }
110                 is = null;
111             }
112             
113             if(swapStream != null)
114             {
115                 try {
116                     swapStream.close();
117                 } catch (IOException e) {
118                     e.printStackTrace();
119                 }
120                 swapStream = null;
121             }
122         }
123         
124         return resultByte;
125     }
126 }