|
@@ -0,0 +1,266 @@
|
|
|
+package com.caimei.www.utils;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.model.GetObjectRequest;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import com.aliyun.oss.model.UploadFileRequest;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.auth.sts.AssumeRoleRequest;
|
|
|
+import com.aliyuncs.auth.sts.AssumeRoleResponse;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件的上传下载
|
|
|
+ *
|
|
|
+ * @author Administrator
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class OssUtil {
|
|
|
+ private static final String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
|
|
|
+ private static final String accessKeyId = "LTAI4GBL3o4YkWnbKYgf2Xia";
|
|
|
+ private static final String accessKeySecret = "dBjAXqbYiEPP6Ukuk2ZsXQeET7FVkK";
|
|
|
+ private static final String privateBucket = "caimei-oss";
|
|
|
+
|
|
|
+ private static String active;
|
|
|
+
|
|
|
+ @Value("${spring.cloud.config.profile}")
|
|
|
+ public void setActive(String actives) {
|
|
|
+ active = actives;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static HashMap<String, String> getToken() {
|
|
|
+ //构建一个阿里云客户端,用于发起请求。
|
|
|
+ //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
|
|
|
+// DefaultProfile.addEndpoint("oss-cn-shenzhen", "Sts", endpoint.replace("https://",""));
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("oss-cn-shenzhen", accessKeyId, accessKeySecret);
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+
|
|
|
+ //构造请求,设置参数。
|
|
|
+ AssumeRoleRequest request = new AssumeRoleRequest();
|
|
|
+ request.setRegionId(endpoint);
|
|
|
+ request.setRoleArn("acs:ram::1565565840178476:role/caimeitst");
|
|
|
+ request.setRoleSessionName("TSTtest");
|
|
|
+ AssumeRoleResponse response = null;
|
|
|
+ try {
|
|
|
+ response = client.getAcsResponse(request);
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String requestId = response.getRequestId();
|
|
|
+ String securityToken = response.getCredentials().getSecurityToken();
|
|
|
+ String accessKeySecret = response.getCredentials().getAccessKeySecret();
|
|
|
+ String accessKeyId = response.getCredentials().getAccessKeyId();
|
|
|
+ HashMap<String, String> tokenMap = new HashMap<>(5);
|
|
|
+ tokenMap.put("requestId", requestId);
|
|
|
+ tokenMap.put("accessKeyId", accessKeyId);
|
|
|
+ tokenMap.put("accessKeySecret", accessKeySecret);
|
|
|
+ tokenMap.put("securityToken", securityToken);
|
|
|
+ tokenMap.put("bucket", privateBucket);
|
|
|
+ return tokenMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*public static HashMap<String, String> getToken() throws ClientException {
|
|
|
+ // regionId表示RAM的地域ID。以华东1(杭州)地域为例,regionID填写为cn-hangzhou。也可以保留默认值,默认值为空字符串("")。
|
|
|
+ String regionId = "";
|
|
|
+ // 添加endpoint。适用于Java SDK 3.12.0及以上版本。
|
|
|
+ DefaultProfile.addEndpoint(regionId, "Sts", endpoint.replace("https://",""));
|
|
|
+ // 添加endpoint。适用于Java SDK 3.12.0以下版本。
|
|
|
+ // DefaultProfile.addEndpoint("",regionId, "Sts", endpoint);
|
|
|
+ // 构造default profile。
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
|
|
|
+ // 构造client。
|
|
|
+ DefaultAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ final AssumeRoleRequest request = new AssumeRoleRequest();
|
|
|
+ // 适用于Java SDK 3.12.0及以上版本。
|
|
|
+ request.setSysMethod(MethodType.POST);
|
|
|
+ // 适用于Java SDK 3.12.0以下版本。
|
|
|
+ //request.setMethod(MethodType.POST);
|
|
|
+ request.setRoleArn("acs:ram::1565565840178476:role/caimeitst");
|
|
|
+ request.setRoleSessionName("TSTtest");
|
|
|
+ request.setPolicy();
|
|
|
+// request.setPolicy(null); // 如果policy为空,则用户将获得该角色下所有权限。
|
|
|
+ request.setDurationSeconds(3600L); // 设置临时访问凭证的有效时间为3600秒。
|
|
|
+ final AssumeRoleResponse response = client.getAcsResponse(request);
|
|
|
+ System.out.println("Expiration: " + response.getCredentials().getExpiration());
|
|
|
+ System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
|
|
|
+ System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
|
|
|
+ System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
|
|
|
+ System.out.println("RequestId: " + response.getRequestId());
|
|
|
+
|
|
|
+ String requestId = response.getRequestId();
|
|
|
+ String securityToken = response.getCredentials().getSecurityToken();
|
|
|
+ String accessKeySecret = response.getCredentials().getAccessKeySecret();
|
|
|
+ String accessKeyId = response.getCredentials().getAccessKeyId();
|
|
|
+ HashMap<String, String> tokenMap = new HashMap<>(5);
|
|
|
+ tokenMap.put("requestId", requestId);
|
|
|
+ tokenMap.put("accessKeyId", accessKeyId);
|
|
|
+ tokenMap.put("accessKeySecret", accessKeySecret);
|
|
|
+ tokenMap.put("securityToken", securityToken);
|
|
|
+ tokenMap.put("bucket", privateBucket);
|
|
|
+ return tokenMap;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ public static String ossUpload(String fileName, File file, String contentType) {
|
|
|
+ String url = null;
|
|
|
+ try {
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ ObjectMetadata meta = new ObjectMetadata();
|
|
|
+ meta.setContentType(contentType);
|
|
|
+ fileName = active + "/" + fileName;
|
|
|
+ UploadFileRequest uploadFileRequest = new UploadFileRequest(privateBucket, fileName);
|
|
|
+ // 指定上传的本地文件。
|
|
|
+ uploadFileRequest.setUploadFile(file.toString());
|
|
|
+ // 指定上传并发线程数,默认为1。
|
|
|
+ uploadFileRequest.setTaskNum(10);
|
|
|
+ // 指定上传的分片大小,范围为100KB~5GB,默认为文件大小/10000。
|
|
|
+ uploadFileRequest.setPartSize(1024 * 1024);
|
|
|
+ // 开启断点续传,默认关闭。
|
|
|
+ uploadFileRequest.setEnableCheckpoint(true);
|
|
|
+ uploadFileRequest.setCheckpointFile(file.getAbsolutePath() + "uploadFile.ucp");
|
|
|
+ // 文件的元数据。
|
|
|
+ uploadFileRequest.setObjectMetadata(meta);
|
|
|
+ // 设置上传成功回调,参数为Callback类型。
|
|
|
+ //uploadFileRequest.setCallback("<yourCallbackEvent>");
|
|
|
+ //断点续传上传。
|
|
|
+ ossClient.uploadFile(uploadFileRequest);
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000);
|
|
|
+ url = ossClient.generatePresignedUrl(privateBucket, fileName, expiration).toString();
|
|
|
+ // 关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
+ } catch (Throwable e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件名判断并获取OSS服务文件上传时文件的contentType
|
|
|
+ */
|
|
|
+ public static String getContentType(String fileName) {
|
|
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ if (".bmp".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/bmp";
|
|
|
+ }
|
|
|
+ if (".gif".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/gif";
|
|
|
+ }
|
|
|
+ if (".jpeg".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/jpeg";
|
|
|
+ }
|
|
|
+ if (".jpg".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/jpg";
|
|
|
+ }
|
|
|
+ if (".png".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/png";
|
|
|
+ }
|
|
|
+ if (".html".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "text/html";
|
|
|
+ }
|
|
|
+ if (".txt".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "text/plain";
|
|
|
+ }
|
|
|
+ if (".vsd".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/vnd.visio";
|
|
|
+ }
|
|
|
+ if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/vnd.ms-powerpoint";
|
|
|
+ }
|
|
|
+ if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/msword";
|
|
|
+ }
|
|
|
+ if (".xml".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "text/xml";
|
|
|
+ }
|
|
|
+ if (".mp4".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "video/mp4";
|
|
|
+ }
|
|
|
+ if (".mp3".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "audio/mp3";
|
|
|
+ }
|
|
|
+ if (".pdf".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/pdf";
|
|
|
+ }
|
|
|
+ return "text/html";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void deleteFile(File... files) {
|
|
|
+ for (File file : files) {
|
|
|
+ //logger.info("File:[{}]",file.getAbsolutePath());
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static File ossUpload(MultipartFile file) throws IOException {
|
|
|
+ // 获取文件名
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ // 获取文件后缀
|
|
|
+ String prefix = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ // 用uuid作为文件名,防止生成的临时文件重复
|
|
|
+ File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
|
|
|
+ // MultipartFile to File
|
|
|
+ file.transferTo(excelFile);
|
|
|
+ //程序结束时,删除临时文件
|
|
|
+ return excelFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 授权生成签名URL临时访问
|
|
|
+ *
|
|
|
+ * @param fileName 文件名称
|
|
|
+ */
|
|
|
+ public static String getOssUrl(String fileName) {
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ // 设置URL过期时间为1个小时
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000);
|
|
|
+ String url = ossClient.generatePresignedUrl(privateBucket, active + "/" + fileName, expiration).toString();
|
|
|
+ // 关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * oss单个文件删除
|
|
|
+ *
|
|
|
+ * @param fileName 文件名称或文件夹名称
|
|
|
+ */
|
|
|
+ public static void deleteSingleFile(String fileName) {
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ // 删除文件。如需删除文件夹,请将ObjectName设置为对应的文件夹名称。
|
|
|
+ // 如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
|
|
|
+ fileName = active + "/" + fileName;
|
|
|
+ ossClient.deleteObject(privateBucket, fileName);
|
|
|
+ // 关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * oss单个文件下载
|
|
|
+ */
|
|
|
+ public static void downFile(String ossName, String fileName) {
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
|
|
|
+ ossClient.getObject(new GetObjectRequest(privateBucket, ossName), new File("./" + fileName));
|
|
|
+ // 关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+}
|