|
@@ -77,86 +77,86 @@ public class UploadTencentCOSController {
|
|
|
// https://cloud.tencent.com/document/product/436/7749
|
|
|
// https://github.com/tencentyun/cos-java-sdk-v5/blob/master/src/main/java/com/qcloud/cos/demo/PutObjectDemo.java
|
|
|
|
|
|
-// /**
|
|
|
-// * 上传文件 (旧)
|
|
|
-// */
|
|
|
-// @PreAuthorize("@ss.hasPermi(1.1)")
|
|
|
-// @PostMapping("/api/tencent/cosUpload")
|
|
|
-// public Result uploadTencentCOS(@RequestParam("file") MultipartFile file) {
|
|
|
-//
|
|
|
-// // 检查上传的文件是否为空
|
|
|
-// if (file.isEmpty()) {
|
|
|
-// return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能为空");
|
|
|
-// }
|
|
|
-// // 判断文件大小是否超过
|
|
|
-// if (file.getSize() > maxSize) {
|
|
|
-// return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能大于 " + maxSize/1024/1024 + " MB");
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 按日期作为文件目录 (腾讯云使用 /,所以不需要配置 File.separator)
|
|
|
-// String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date()) + '/';
|
|
|
-//
|
|
|
-// // 生成后缀名
|
|
|
-// String originalFileName = file.getOriginalFilename();
|
|
|
-// String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
|
|
|
-//
|
|
|
-// // 生成新的文件名
|
|
|
-// String newFileName = CommonUtil.generateFilename(suffix);
|
|
|
-//
|
|
|
-// //
|
|
|
-// String objectName = dateDir + newFileName;
|
|
|
-//
|
|
|
-// // -- COS ------------------------------------------------------------------
|
|
|
-// // 初始化用户身份信息(secretId, secretKey)
|
|
|
-// COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
|
|
-//
|
|
|
-// // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
|
|
|
-// Region region1 = new Region(region);
|
|
|
-// ClientConfig clientConfig = new ClientConfig(region1);
|
|
|
-// clientConfig.setRegion(region1);
|
|
|
-//
|
|
|
-// // 生成cos客户端
|
|
|
-// COSClient cosClient = new COSClient(cred, clientConfig);
|
|
|
-//
|
|
|
-// try {
|
|
|
-//
|
|
|
-// // 获取文件内容
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
-//
|
|
|
-// // 将文件内容写入临时文件
|
|
|
-// File tempFile = File.createTempFile("temp", null);
|
|
|
-// try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
|
|
-// IOUtils.copy(inputStream, outputStream);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 创建 PutObjectRequest 对象。
|
|
|
-// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, tempFile);
|
|
|
-//
|
|
|
-// ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
-// putObjectRequest.withMetadata(objectMetadata);
|
|
|
-//
|
|
|
-// // 创建 PutObject 请求。(异步)
|
|
|
-// PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
-//
|
|
|
-// } catch (IOException e) {
|
|
|
-// System.out.println("IOException e:");
|
|
|
-// System.out.println(e);
|
|
|
-// } finally {
|
|
|
-// if (cosClient != null) {
|
|
|
-// cosClient.shutdown();
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 返回值 结构
|
|
|
-// Map<String, Object> map = new LinkedHashMap<>();
|
|
|
-// map.put("filename", newFileName);
|
|
|
-// map.put("url", accessibleDomain + "/" + objectName); // File.separator
|
|
|
-// map.put("type", suffix.replace(".", ""));
|
|
|
-// map.put("size", file.getSize());
|
|
|
-//
|
|
|
-// return Result.success(map);
|
|
|
-//
|
|
|
-// }
|
|
|
+ /**
|
|
|
+ * 上传文件 (旧)
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi(1.1)")
|
|
|
+ @PostMapping("/api/tencent/cosUpload")
|
|
|
+ public Result uploadTencentCOS(@RequestParam("file") MultipartFile file) {
|
|
|
+
|
|
|
+ // 检查上传的文件是否为空
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能为空");
|
|
|
+ }
|
|
|
+ // 判断文件大小是否超过
|
|
|
+ if (file.getSize() > maxSize) {
|
|
|
+ return Result.error(ResultEnum.INTERNAL_ERROR.getCode(), "文件不能大于 " + maxSize/1024/1024 + " MB");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按日期作为文件目录 (腾讯云使用 /,所以不需要配置 File.separator)
|
|
|
+ String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date()) + '/';
|
|
|
+
|
|
|
+ // 生成后缀名
|
|
|
+ String originalFileName = file.getOriginalFilename();
|
|
|
+ String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
|
|
|
+
|
|
|
+ // 生成新的文件名
|
|
|
+ String newFileName = CommonUtil.generateFilename(suffix);
|
|
|
+
|
|
|
+ //
|
|
|
+ String objectName = dateDir + newFileName;
|
|
|
+
|
|
|
+ // -- COS ------------------------------------------------------------------
|
|
|
+ // 初始化用户身份信息(secretId, secretKey)
|
|
|
+ COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
|
|
+
|
|
|
+ // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
|
|
|
+ Region region1 = new Region(region);
|
|
|
+ ClientConfig clientConfig = new ClientConfig(region1);
|
|
|
+ clientConfig.setRegion(region1);
|
|
|
+
|
|
|
+ // 生成cos客户端
|
|
|
+ COSClient cosClient = new COSClient(cred, clientConfig);
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 获取文件内容
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+
|
|
|
+ // 将文件内容写入临时文件
|
|
|
+ File tempFile = File.createTempFile("temp", null);
|
|
|
+ try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
|
|
+ IOUtils.copy(inputStream, outputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建 PutObjectRequest 对象。
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, tempFile);
|
|
|
+
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ putObjectRequest.withMetadata(objectMetadata);
|
|
|
+
|
|
|
+ // 创建 PutObject 请求。(异步)
|
|
|
+ PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println("IOException e:");
|
|
|
+ System.out.println(e);
|
|
|
+ } finally {
|
|
|
+ if (cosClient != null) {
|
|
|
+ cosClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回值 结构
|
|
|
+ Map<String, Object> map = new LinkedHashMap<>();
|
|
|
+ map.put("filename", newFileName);
|
|
|
+ map.put("url", accessibleDomain + "/" + objectName); // File.separator
|
|
|
+ map.put("type", suffix.replace(".", ""));
|
|
|
+ map.put("size", file.getSize());
|
|
|
+
|
|
|
+ return Result.success(map);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|