|
@@ -0,0 +1,209 @@
|
|
|
+package com.caimei365.tools.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 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.UUID;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文件的上传下载
|
|
|
+ *
|
|
|
+ * @author Administrator
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class OssUtils {
|
|
|
+ private static String endpoint;
|
|
|
+
|
|
|
+ @Value("${aliyunConfig.endpoint}")
|
|
|
+ public void setEndpoint(String endpoints) {
|
|
|
+ endpoint = endpoints;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${aliyunConfig.accessKeyId}")
|
|
|
+ public void setAccessKeyId(String accessKeyIds) {
|
|
|
+ accessKeyId = accessKeyIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${aliyunConfig.accessKeySecret}")
|
|
|
+ public void setAccessKeySecret(String accessKeySecrets) {
|
|
|
+ accessKeySecret = accessKeySecrets;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String privateBucket;
|
|
|
+
|
|
|
+ @Value("${aliyunConfig.bucketName}")
|
|
|
+ public void setPrivateBucket(String bucketName) {
|
|
|
+ privateBucket = bucketName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String active;
|
|
|
+
|
|
|
+ @Value("${spring.cloud.config.profile}")
|
|
|
+ public void setActive(String actives) {
|
|
|
+ active = actives;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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, 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();
|
|
|
+ }
|
|
|
+}
|