From e468088052b2dea9837645eeed689ee9c5f0948d Mon Sep 17 00:00:00 2001
From: zhanghua <314079846@qq.com>
Date: 星期五, 27 十月 2023 15:58:11 +0800
Subject: [PATCH] 油烟
---
ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java | 96 +++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 90 insertions(+), 6 deletions(-)
diff --git a/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java b/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java
index 47a4f18..bc84db9 100644
--- a/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java
+++ b/ycl-common/src/main/java/com/ycl/service/oss/impl/OssServiceImpl.java
@@ -5,18 +5,28 @@
import com.aliyun.oss.model.ObjectMetadata;
import com.ycl.service.oss.OssService;
import com.ycl.utils.ConstantPropertiesUtils;
+import com.ycl.utils.common.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
import java.util.Date;
-import java.util.UUID;
@Service
public class OssServiceImpl implements OssService {
@Override
public String uploadImages(MultipartFile file) {
+ DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
+ System.out.println("寮�濮嬩笂浼�--" + dtf2.format(LocalDateTime.now()));
+ if (file == null) {
+ return "涓婁紶鏂囦欢涓虹┖";
+ }
String endpoint = ConstantPropertiesUtils.END_POINT;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
@@ -38,13 +48,33 @@
ossClient.shutdown();
// 鎶婁笂浼犵殑鏂囦欢璺緞杩斿洖 锛堟墜鍔ㄦ嫾鎺ワ級
// 杩欓噷璁剧疆鍥剧墖鏈夋晥鏃堕棿 鎴戣缃簡30骞�
- Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000);
+ Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000);
String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString();
+
+ System.out.println("涓婁紶缁撴潫--" + dtf2.format(LocalDateTime.now()));
return url;
} catch (Exception e) {
- e.printStackTrace();
+ System.out.println("uploadImages涓婁紶鍥剧墖澶辫触锛�");
+ // e.printStackTrace();
return null;
}
+ }
+
+ private byte[] readInputStream(InputStream inStream) throws Exception {
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+ //鍒涘缓涓�涓狟uffer瀛楃涓�
+ byte[] buffer = new byte[1024];
+ //姣忔璇诲彇鐨勫瓧绗︿覆闀垮害锛屽鏋滀负-1锛屼唬琛ㄥ叏閮ㄨ鍙栧畬姣�
+ int len = 0;
+ //浣跨敤涓�涓緭鍏ユ祦浠巄uffer閲屾妸鏁版嵁璇诲彇鍑烘潵
+ while ((len = inStream.read(buffer)) != -1) {
+ //鐢ㄨ緭鍑烘祦寰�buffer閲屽啓鍏ユ暟鎹紝涓棿鍙傛暟浠h〃浠庡摢涓綅缃紑濮嬭锛宭en浠h〃璇诲彇鐨勯暱搴�
+ outStream.write(buffer, 0, len);
+ }
+ //鍏抽棴杈撳叆娴�
+ inStream.close();
+ //鎶妎utStream閲岀殑鏁版嵁鍐欏叆鍐呭瓨
+ return outStream.toByteArray();
}
@Override
@@ -76,7 +106,10 @@
// 涓婁紶鏂囦欢娴�
try {
// 鑾峰彇鏂囦欢鐨勫悕绉�
- String fileName = "sczf/" + String.valueOf(UUID.randomUUID());
+ LocalDate date = LocalDate.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
+ String fileName = "sczf/" + date.format(formatter) + RandomUtils.generateRandomInt(8) + "." + ext;
+ System.out.println("鏂囦欢鍦板潃锛�" + fileName);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(getcontentType(ext));
// 璋冪敤oss鐨勬柟娉曞疄鐜伴暱浼�
@@ -87,18 +120,69 @@
ossClient.shutdown();
// 鎶婁笂浼犵殑鏂囦欢璺緞杩斿洖 锛堟墜鍔ㄦ嫾鎺ワ級
// 杩欓噷璁剧疆鍥剧墖鏈夋晥鏃堕棿 鎴戣缃簡30骞�
- Date expiration = new Date(System.currentTimeMillis() + 946080000 * 1000);
+ Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000);
String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString();
- // System.out.println("oss鎴愬姛锛�" + url);
+ System.out.println("oss涓婁紶鎴愬姛锛�" + url);
+
+ // URL urlO = new URL(url);
+ // HttpURLConnection conn = (HttpURLConnection) urlO.openConnection();
+ // conn.setRequestMethod("GET");
+ // conn.setConnectTimeout(10 * 1000);
+ // conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+ //
+ // InputStream inputStream2 = conn.getInputStream();// 閫氳繃杈撳叆娴佽幏鍙栧浘鐗囨暟鎹�
+ //
+ // byte[] data = readInputStream(inputStream2);
+ //
+ // System.out.println("鑾峰彇鍥剧墖鎴愬姛:" + url);
return url;
} catch (Exception e) {
System.out.println("oss寮傚父锛�" + e.getMessage());
+ // e.printStackTrace();
+ return this.uploadImages(inputStream, ext);
+ }
+ }
+
+ @Override
+ public String uploadVideo(MultipartFile file) {
+ if (file == null) {
+ return "涓婁紶瑙嗛涓虹┖";
+ }
+ String endpoint = ConstantPropertiesUtils.END_POINT;
+ String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
+ String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
+ String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
+ // 鍒涘缓OSSClient瀹炰緥
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+ // 涓婁紶鏂囦欢娴�
+ try {
+ // 鑾峰彇鏂囦欢鐨勫悕绉�
+ String fileName = "sczf/" + file.getOriginalFilename();
+ ObjectMetadata objectMetadata = new ObjectMetadata();
+ objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
+ // 璋冪敤oss鐨勬柟娉曞疄鐜伴暱浼�
+ // 绗竴涓弬鏁� bucketName
+ // 绗簩涓弬鏁� 涓婁紶鍒皁ss鐨勬枃浠惰矾寰勫拰鏂囦欢鍚嶇О
+ ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()), objectMetadata);
+ // 鍏抽棴OSSClient銆�
+ ossClient.shutdown();
+ // 鎶婁笂浼犵殑鏂囦欢璺緞杩斿洖 锛堟墜鍔ㄦ嫾鎺ワ級
+ // 杩欓噷璁剧疆鍥剧墖鏈夋晥鏃堕棿 鎴戣缃簡30骞�
+ Date expiration = new Date(System.currentTimeMillis() + (long) 946080000 * 1000);
+ String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString();
+
+ return url;
+ } catch (Exception e) {
+ System.out.println("uploadImages涓婁紶瑙嗛澶辫触锛�");
// e.printStackTrace();
return null;
}
}
public static String getcontentType(String FilenameExtension) {
+ if (FilenameExtension.equalsIgnoreCase(".mp4")||FilenameExtension.equalsIgnoreCase(".mov")) {
+ return "video/mp4";
+ }
if (FilenameExtension.equalsIgnoreCase(".bmp")) {
return "image/bmp";
}
--
Gitblit v1.8.0