package com.ycl.utils; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.PutObjectResult; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Date; import java.util.UUID; @Component public class AliyunUtils { private static String endpoint; private static String keyId; private static String keySecret; private static String bucketName; @Value("${aliyun.oss.endpoint}") public void setEndpoint(String endpoint) { AliyunUtils.endpoint = endpoint; } @Value("${aliyun.oss.keyId}") public void setKeyId(String keyId) { AliyunUtils.keyId = keyId; } @Value("${aliyun.oss.keySecret}") public void setKeySecret(String keySecret) { AliyunUtils.keySecret = keySecret; } @Value("${aliyun.oss.bucketName}") public void setBucketName(String bucketName) { AliyunUtils.bucketName = bucketName; } // public static String upload(InputStream inputStream, String orginalFileName){ // // 创建OSSClient实例。 // OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret); // // 文件名 // String fileName = UUID.randomUUID().toString(); // //文件扩展名 // String fileExtention = orginalFileName.substring(orginalFileName.lastIndexOf(".")); // //最终的路径 类似avatar/2021/12/05/xxxxxxxxx.jpg // String objectName = fileName+fileExtention; // ossClient.putObject(bucketName, objectName, inputStream); // // 关闭OSSClient。 // ossClient.shutdown(); // return "https://"+bucketName+"."+endpoint+"/"+objectName; // } public String upload(MultipartFile file) { String accessKeyId = keyId; String accessKeySecret = keySecret; // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件流 try { // 获取文件的名称 String fileName = file.getOriginalFilename(); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf(".")))); // 调用oss的方法实现长传 // 第一个参数 bucketName // 第二个参数 上传到oss的文件路径和文件名称 ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()),objectMetadata); // 关闭OSSClient。 ossClient.shutdown(); // 把上传的文件路径返回 (手动拼接) // 这里设置图片有效时间 我设置了30年 Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000); String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString(); return url; } catch (Exception e) { e.printStackTrace(); return null; } } public static void delete(String link){ if (link == null || link.equals("")) { return; } OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret); try { ossClient.deleteObject(bucketName, link); } 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()); } 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()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } // 实现图片的预览功能 public static String getcontentType(String FilenameExtension) { if (FilenameExtension.equalsIgnoreCase(".bmp")) { return "image/bmp"; } if (FilenameExtension.equalsIgnoreCase(".gif")) { return "image/gif"; } if (FilenameExtension.equalsIgnoreCase(".jpeg") || FilenameExtension.equalsIgnoreCase(".jpg") || FilenameExtension.equalsIgnoreCase(".png")) { return "image/jpg"; } if (FilenameExtension.equalsIgnoreCase(".html")) { return "text/html"; } if (FilenameExtension.equalsIgnoreCase(".txt")) { return "text/plain"; } if (FilenameExtension.equalsIgnoreCase(".vsd")) { return "application/vnd.visio"; } if (FilenameExtension.equalsIgnoreCase(".pptx") || FilenameExtension.equalsIgnoreCase(".ppt")) { return "application/vnd.ms-powerpoint"; } if (FilenameExtension.equalsIgnoreCase(".docx") || FilenameExtension.equalsIgnoreCase(".doc")) { return "application/msword"; } if (FilenameExtension.equalsIgnoreCase(".xml")) { return "text/xml"; } return "image/jpg"; } }