|
|
@@ -0,0 +1,199 @@
|
|
|
+package com.mokasz.sample.manager;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
+import com.aliyun.oss.model.DownloadFileRequest;
|
|
|
+import com.aliyun.oss.model.DownloadFileResult;
|
|
|
+import com.aliyun.oss.model.PutObjectRequest;
|
|
|
+import com.mokasz.sample.common.properties.OssConfig;
|
|
|
+import com.mokasz.sample.common.util.OssUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * OSS 文件上传服务
|
|
|
+ *
|
|
|
+ * @author ljj12094
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class OssClient {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OssConfig ossConfig;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件到 OSS(保存到 yyyyMM 目录下,文件名随机生成)
|
|
|
+ *
|
|
|
+ * @param file 文件
|
|
|
+ * @return 文件地址
|
|
|
+ */
|
|
|
+ public String upload(MultipartFile file) {
|
|
|
+ String result = "";
|
|
|
+ String date = DateUtil.format(new Date(), DatePattern.SIMPLE_MONTH_PATTERN);
|
|
|
+ String filename = UUID.randomUUID().toString().replaceAll("-", "") + OssUtils.getExtension(file, true);
|
|
|
+
|
|
|
+ OSS ossClient = null;
|
|
|
+ try {
|
|
|
+ ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+
|
|
|
+ if (file.getSize() != 0 && StringUtils.isNotEmpty(file.getName())) {
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),
|
|
|
+ date + "/" + filename, file.getInputStream());
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+ result = String.format("%s/%s/%s", ossConfig.getDomain(), date, filename);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("[ 上传文件 ] >> result:{}", result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String download(String dirPath, String mediaUrl) {
|
|
|
+ OSS ossClient = null;
|
|
|
+ try {
|
|
|
+ dirPath = StrUtil.trim(dirPath);
|
|
|
+ String filePath = getFilePathFromMediaUrl(mediaUrl);
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(dirPath) || StringUtils.isEmpty(filePath)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (filePath.startsWith("\\") || filePath.startsWith("/")) {
|
|
|
+ filePath = filePath.substring(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ Path path = Paths.get(dirPath, filePath);
|
|
|
+ File dir = path.getParent().toFile();
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ String downloadFilePath = path.toString();
|
|
|
+
|
|
|
+ ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
+ if (ossClient.doesObjectExist(ossConfig.getBucketName(), filePath)) {
|
|
|
+ DownloadFileRequest downloadFileRequest = new DownloadFileRequest(ossConfig.getBucketName(), filePath);
|
|
|
+ downloadFileRequest.setDownloadFile(downloadFilePath);
|
|
|
+
|
|
|
+ DownloadFileResult downloadFileResult = ossClient.downloadFile(downloadFileRequest);
|
|
|
+ if (downloadFileResult == null || downloadFileResult.getObjectMetadata() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (downloadByUrl(mediaUrl, downloadFilePath) == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return downloadFilePath;
|
|
|
+ } catch (Throwable e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载网络文件
|
|
|
+ *
|
|
|
+ * @param strUrl
|
|
|
+ * @param downloadFilePath
|
|
|
+ */
|
|
|
+ public String downloadByUrl(String strUrl, String downloadFilePath) {
|
|
|
+ int byteSum = 0;
|
|
|
+ int byteRead;
|
|
|
+
|
|
|
+ URL url;
|
|
|
+ try {
|
|
|
+ url = new URL(strUrl);
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ log.error(String.format("文件URL格式不正确(%s)", strUrl), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ URLConnection conn;
|
|
|
+ try {
|
|
|
+ conn = url.openConnection();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(String.format("打开文件URL出错(%s)", strUrl), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Path path = Paths.get(downloadFilePath);
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (Files.exists(path)) {
|
|
|
+ Files.delete(path);
|
|
|
+ }
|
|
|
+ Files.createFile(path);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(String.format("创建本地文件出错(%s)", downloadFilePath), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try (InputStream inStream = conn.getInputStream();
|
|
|
+ FileOutputStream fs = new FileOutputStream(downloadFilePath);) {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ while ((byteRead = inStream.read(buffer)) != -1) {
|
|
|
+ byteSum += byteRead;
|
|
|
+ System.out.println(byteSum);
|
|
|
+ fs.write(buffer, 0, byteRead);
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ log.error(String.format("不存在本地文件(%s)", downloadFilePath), e);
|
|
|
+ return null;
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(String.format("下载文件失败(%s => %s)", strUrl, downloadFilePath), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return downloadFilePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件路径
|
|
|
+ *
|
|
|
+ * @param mediaUrl
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getFilePathFromMediaUrl(String mediaUrl) {
|
|
|
+ URI uri = URI.create(mediaUrl);
|
|
|
+ String path = uri.getPath();
|
|
|
+ path = StrUtil.trim(path);
|
|
|
+ if (org.springframework.util.StringUtils.isEmpty(path)) {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ if (path.startsWith("/")) {
|
|
|
+ path = path.substring(1);
|
|
|
+ }
|
|
|
+ return StrUtil.trim(path);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|