From 885290e4d0d0c7fad3f538d901c616e49c3d6985 Mon Sep 17 00:00:00 2001
From: 胡仁荣 <897853850@qq.com>
Date: 星期二, 18 七月 2023 18:15:58 +0800
Subject: [PATCH] oss分块上传
---
src/main/java/com/qxueyou/scc/controller/HweiYunOBSController.java | 79 +++++++++++++++++++++++++++++++--------
1 files changed, 63 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/qxueyou/scc/controller/HweiYunOBSController.java b/src/main/java/com/qxueyou/scc/controller/HweiYunOBSController.java
index 02cd660..9c8516a 100644
--- a/src/main/java/com/qxueyou/scc/controller/HweiYunOBSController.java
+++ b/src/main/java/com/qxueyou/scc/controller/HweiYunOBSController.java
@@ -6,11 +6,16 @@
import cn.hutool.core.util.StrUtil;
//import cn.hutool.core.io.FileUtil;
+import com.aliyun.oss.OSSException;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectResult;
import com.qxueyou.scc.base.model.FileMeta;
import com.qxueyou.scc.base.model.Result;
+import com.qxueyou.scc.base.util.ClientUtils;
import com.qxueyou.scc.teach.res.service.HweiYunOBSService;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -35,6 +40,7 @@
@RestController
@RequestMapping("/file")// @RequestMapping("/file")
public class HweiYunOBSController {
+ private final Logger log = LogManager.getLogger("FileController");
@Resource
private HweiYunOBSService hweiYunOBSService;
@@ -45,7 +51,6 @@
if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
return null;
}
-
// if (FileUtil.isEmpty(file)) {
// return new Result(false,"鏂囦欢涓虹┖");
// }
@@ -54,21 +59,63 @@
return test;
}
-// @RequestMapping(value = "fenpian", method = RequestMethod.POST)
-// public Map<String, Object> fpsave(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
-//
-// if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
-// return null;
-// }
-//
-//// if (FileUtil.isEmpty(file)) {
-//// return new Result(false,"鏂囦欢涓虹┖");
-//// }
-//// final List<FileMeta> test = hweiYunOBSService.fileUpload(file, file.getOriginalFilename());
-// Map<String, Object> fnepian = hweiYunOBSService.fnepian(file, file.getOriginalFilename());
-//// return ResponseVO.ok("鎵ц鎴愬姛",test);
-// return fnepian;
-// }
+ /**
+ * 鏂囦欢涓婁紶鎺ュ彛:涓哄鎴风鎻愪緵澶氱嚎绋嬪垎鐗囦笂浼犳帴鍙�
+ * 杩斿洖鐘舵�佸��:200浠h〃鎴愬姛锛�500浠h〃涓婁紶澶辫触锛�205浠h〃闇�閲嶆柊灏濊瘯
+ *
+ * @param identifier 鍞竴id锛岄渶瀹㈡埛绔繚璇佸敮涓�鎬э紝鐩稿悓鏂囦欢淇濊瘉鍞竴鍗冲彲锛岄潪MD5
+ * @return
+ * @TODO : 鐩墠缂哄皯瀵� 鍚勬ā鍧楁枃浠朵釜鏁般�佹枃浠舵牸寮忋�佹枃浠跺ぇ灏忕殑涓婁紶鎺у埗,缁熶竴澧炲姞鍦╟onfigFileModule琛ㄩ噷闈㈠嵆鍙疄鐜� 1 鍥剧墖锛� 2 鏂囨。 3 瑙嗛 4 闊抽
+ */
+ @RequestMapping(value = "/uploadChunk", headers = "content-type=multipart/*", method = RequestMethod.POST)
+ public @ResponseBody FileMeta doUploadPublic(MultipartFile file, HttpServletResponse response,
+ Integer chunkNumber, Integer totalChunks, long chunkSize, long totalSize, String identifier, String filename, Integer currentChunkSize) {
+
+ FileMeta fileMeta = new FileMeta();
+ try {
+
+ Result uploadResult = null;
+
+ uploadResult = hweiYunOBSService.uploadChunk(file.getInputStream(), identifier.concat(ClientUtils.getUserId()), chunkNumber, currentChunkSize, totalChunks, filename);
+
+
+ fileMeta.setFileId(uploadResult.getDataT("fileId"));
+ fileMeta.setPath(uploadResult.getDataT("path"));
+
+ if (!uploadResult.isSuccess()) {
+ response.setStatus(205);
+ }
+
+ } catch (OSSException e) {
+ response.setStatus(500);
+ log.error(e, e);
+ } catch (Exception e) {
+ log.error("涓婁紶妯″潡瑙f瀽鍑洪敊锛�" + e.getMessage(), e);
+ response.setStatus(500);
+ }
+
+ fileMeta.setFileName(filename);
+ fileMeta.setFileSize(String.valueOf(totalSize));
+ fileMeta.setFileType(null);
+
+ return fileMeta;
+ }
+
+ /**
+ * 鍒濆鍖栧垎鐗囦笂浼犱换鍔�
+ *
+ * @return
+ */
+ @RequestMapping(value = "/uploadChunk/init", method = RequestMethod.POST)
+ public @ResponseBody
+ Result initUploadChunk(String identifier, String fileName,String md5) {
+
+ //濡傛灉MD5鐨勫搴旀枃浠跺瓨鍦ㄥ垯鐩存帴杩斿洖
+// Result result = fileService.checkMd5(md5,fileName);
+
+ return hweiYunOBSService.initUploadChunk(identifier.concat(ClientUtils.getUserId()), fileName,md5);
+
+ }
@RequestMapping(value = "delete", method = RequestMethod.POST)
public Result delete(@RequestParam(value = "fileName", required = false) String fileName) {
--
Gitblit v1.8.0