package com.verse.hades.utils; import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.auth.CredentialsProvider; import com.aliyun.oss.common.auth.DefaultCredentialProvider; import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.PutObjectResult; import com.verse.hades.config.OSSConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.net.URL; import java.util.Date; @Slf4j public class OSSClientUtil { //阿里雲OSS API的內或外網域名 private static String ENDPOINT; //阿里雲OSS API的密鑰Access Key ID private static String ACCESS_KEY_ID; //阿里雲OSS API的密鑰Access Key Secret private static String ACCESS_KEY_SECRET; //阿里雲OSS API的bucket名稱 private static String BUCKET_NAME; //阿里雲OSS 上傳文件夾 private static String IMAGE_FOLDER; private static CredentialsProvider credentialsProvider; private static final long IMAGE_EXPIRE_TIME = 10 * 365 * 24 * 60 * 60 * 1000L;//圖片訪問連接的過時時間 private static OSSClient ossClient; public OSSClientUtil() { ossClient = new OSSClient(ENDPOINT, credentialsProvider, null); } public static void setConfigInfo(OSSConfig ossClientConfig) { OSSClientUtil.ENDPOINT = ossClientConfig.getEndpoint(); OSSClientUtil.ACCESS_KEY_ID = ossClientConfig.getAccessKeyId(); OSSClientUtil.ACCESS_KEY_SECRET = ossClientConfig.getAccessKeySecret(); OSSClientUtil.BUCKET_NAME = ossClientConfig.getBucketName(); OSSClientUtil.IMAGE_FOLDER = ossClientConfig.getImageFolder(); credentialsProvider = new DefaultCredentialProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET); } /** * 初始化 */ public static void init() { ossClient = new OSSClient(ENDPOINT, credentialsProvider, null); } /** * 銷燬 */ public static void destory() { ossClient.shutdown(); } /** * @param: instream * @param: fileName * @Return: java.lang.String * @Decription: 上傳圖片至OSS * @CreateDate: Created in 2018/12/11 16:36 * @Modify: */ public static String uploadImageToOSS(MultipartFile file, String fileName) { if (ossClient == null) { init(); } String ret = ""; try { //建立上傳Object的Metadata ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(file.getInputStream().available()); objectMetadata.setCacheControl("no-cache"); objectMetadata.setHeader("Pragma", "no-cache"); objectMetadata.setContentType(getcontentType(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")))); objectMetadata.setContentDisposition("inline;filename=" + fileName); //上傳文件 PutObjectResult putResult = ossClient.putObject(BUCKET_NAME, IMAGE_FOLDER + fileName, file.getInputStream(), objectMetadata); ret = putResult.getETag(); } catch (IOException e) { log.error("upload file to oss error name={}", fileName, e); } finally { try { if (file.getInputStream() != null) { file.getInputStream().close(); } } catch (Exception e) { e.printStackTrace(); } } return ret; } /** * @param: FilenameExtension * @Return: java.lang.String * @Decription: 判斷OSS服務文件上傳時文件的contentType * @CreateDate: Created in 2018/12/11 17:19 * @Modify: */ private 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/jpeg"; } 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/jpeg"; } /** * @param: fileName * @Return: java.lang.String * @Decription: 根據圖片名稱獲取圖片訪問地址 * @CreateDate: Created in 2018/12/11 17:18 * @Modify: */ public static String getImageUrl(String fileName) { // 設置URL過時時間 Date expiration = new Date(new Date().getTime() + IMAGE_EXPIRE_TIME); // 生成URL URL url = ossClient.generatePresignedUrl(BUCKET_NAME, IMAGE_FOLDER + fileName, expiration); if (url != null) { return url.toString(); } return null; } }