|
|
@@ -0,0 +1,121 @@
|
|
|
+package com.model.util;
|
|
|
+
|
|
|
+import com.aliyun.oss.ClientException;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.OSSException;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 上传阿里云oos
|
|
|
+ */
|
|
|
+public class ossFileUtil {
|
|
|
+ /**
|
|
|
+ * 上传
|
|
|
+ * @param file 文件
|
|
|
+ * @param fileName 文件名称
|
|
|
+ * @return 返回路径
|
|
|
+ * @throws IOException 异常
|
|
|
+ */
|
|
|
+ public static Map<String, Object> uploadAliyun(MultipartFile file, String fileName) throws IOException {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ // 1 获取上传需要的固定值
|
|
|
+ String endpoint = "oss-cn-beijing.aliyuncs.com"; //你的站点
|
|
|
+ String accessKeyId = "LTAI5tGNYXMJNMWa1pBVqCYt"; //你的acess_key_id
|
|
|
+ String accessKeySecret = "M8f45Cysl13t0eAMzyd1vey2FvDaAz"; //你的acess_key_secret
|
|
|
+ String bucketName = "lidaotech"; //你的bucket_name
|
|
|
+ String objectName = "tuya/" + fileName;
|
|
|
+ //外面获取文件输入流,最后方便关闭
|
|
|
+ InputStream in = file.getInputStream();
|
|
|
+ //2 创建OssClient对象
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ try {
|
|
|
+ //3 获取文件信息,为了上传
|
|
|
+ // meta设置请求头
|
|
|
+// ObjectMetadata meta = new ObjectMetadata();
|
|
|
+// meta.setContentType("image/png");
|
|
|
+ //4 设置知道文件夹
|
|
|
+ ossClient.putObject(bucketName, objectName, in);
|
|
|
+ //5 关闭
|
|
|
+ ossClient.shutdown();
|
|
|
+ //6 返回上传之后地址,拼接地址
|
|
|
+ String uploadUrl = "https://" + bucketName + "." + endpoint + "/" + objectName;
|
|
|
+ map.put("data", uploadUrl);
|
|
|
+ map.put("msg", "200");
|
|
|
+ return map;
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
+ System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
+ System.out.println("Request ID:" + oe.getRequestId());
|
|
|
+ System.out.println("Host ID:" + oe.getHostId());
|
|
|
+ map.put("msg", "500");
|
|
|
+ return map;
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
+ + "such as not being able to access the network.");
|
|
|
+ System.out.println("Error Message:" + ce.getMessage());
|
|
|
+ map.put("msg", "500");
|
|
|
+ return map;
|
|
|
+ } finally {
|
|
|
+ //5 关闭ossClient
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ * @param fileName 文件名称
|
|
|
+ * @return 返回路径
|
|
|
+ * @throws IOException 异常
|
|
|
+ */
|
|
|
+ public static Map<String, Object> delUploadAliyun(String fileName) throws IOException {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ // 1 获取上传需要的固定值
|
|
|
+ String endpoint = "oss-cn-beijing.aliyuncs.com"; //你的站点
|
|
|
+ String accessKeyId = "LTAI5tGNYXMJNMWa1pBVqCYt"; //你的acess_key_id
|
|
|
+ String accessKeySecret = "M8f45Cysl13t0eAMzyd1vey2FvDaAz"; //你的acess_key_secret
|
|
|
+ String bucketName = "lidaotech"; //你的bucket_name
|
|
|
+ String objectName = "tuya/" + fileName;
|
|
|
+ //2 创建OssClient对象
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ try {
|
|
|
+ //3 文件删除
|
|
|
+ ossClient.deleteObject(bucketName, objectName);
|
|
|
+ //4 关闭
|
|
|
+ ossClient.shutdown();
|
|
|
+ map.put("msg", "200");
|
|
|
+ return map;
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ System.out.println("Error Message:" + oe.getErrorMessage());
|
|
|
+ System.out.println("Error Code:" + oe.getErrorCode());
|
|
|
+ System.out.println("Request ID:" + oe.getRequestId());
|
|
|
+ System.out.println("Host ID:" + oe.getHostId());
|
|
|
+ map.put("msg", "500");
|
|
|
+ return map;
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ System.out.println("Caught an ClientException, which means the client encountered "
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
+ + "such as not being able to access the network.");
|
|
|
+ System.out.println("Error Message:" + ce.getMessage());
|
|
|
+ map.put("msg", "500");
|
|
|
+ return map;
|
|
|
+ } finally {
|
|
|
+ //5 关闭ossClient
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|