From 5f3010ab3a04238e2b1cdca48b3b447d441caed0 Mon Sep 17 00:00:00 2001 From: welsir <1824379011@qq.com> Date: Sat, 23 Dec 2023 20:11:18 +0800 Subject: [PATCH] =?UTF-8?q?[DEV:=E6=96=87=E4=BB=B6=E6=9C=8D=E5=8A=A1]=20?= =?UTF-8?q?=E4=B8=8A=E7=BA=BF=E6=B5=8B=E8=AF=95=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?1=20-e:test=20-m:file-service=20-type:single=20-v:1.4=20-?= =?UTF-8?q?rp:8089=20-de:<-e=20ACTIVE=3Ddev=20-e=20UNIQUE=5FID=3D0=20-e=20?= =?UTF-8?q?SERVER=5FPORT=3D8089>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/OSSOperatorController.java | 30 +++++++++++++++++++ .../exception/GlobalExceptionHandler.java | 2 +- .../example/filesystem/pojo/vo/OSSFileVO.java | 1 + .../filesystem/util/ConcurrentUtil.java | 29 ++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/filesystem/util/ConcurrentUtil.java diff --git a/src/main/java/com/example/filesystem/controller/OSSOperatorController.java b/src/main/java/com/example/filesystem/controller/OSSOperatorController.java index 99e4b76..1138187 100644 --- a/src/main/java/com/example/filesystem/controller/OSSOperatorController.java +++ b/src/main/java/com/example/filesystem/controller/OSSOperatorController.java @@ -5,16 +5,23 @@ import com.example.filesystem.pojo.vo.DownloadFileVO; import com.example.filesystem.pojo.vo.OSSFileVO; import com.example.filesystem.pojo.vo.UploadFileVO; +import com.example.filesystem.util.ConcurrentUtil; import org.apache.tomcat.jni.OS; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; /** * @Description @@ -28,6 +35,8 @@ public class OSSOperatorController { @Resource AbstractOSSFileStrategy strategy; + private static final ExecutorService executorService = Executors.newFixedThreadPool(50); + @PostMapping("/upload") public Result upload(@Validated OSSFileVO commonFileVO){ UploadFileVO res = strategy.upload(commonFileVO); @@ -40,4 +49,25 @@ public Result download(@Validated DownloadFileVO commonFileVO){ return Result.success(res); } + @PostMapping("/upload/list") + public Result upload(MultipartFile[] files,String[] md5List,String[] pathList,String bucket){ + ArrayList res = new ArrayList<>(); + OSSFileVO ossFileVO = new OSSFileVO(); + ossFileVO.setFile(files[0]); + ossFileVO.setPath(pathList[0]); + ossFileVO.setMd5(md5List[0]); + ossFileVO.setBucket(bucket); + Callable file1 = ()->strategy.upload(ossFileVO); + ossFileVO.setFile(files[1]); + ossFileVO.setPath(pathList[1]); + ossFileVO.setMd5(md5List[1]); + Callable file2 = ()->strategy.upload(ossFileVO); + + Future res1 = ConcurrentUtil.doJob(executorService, file1); + Future res2 = ConcurrentUtil.doJob(executorService, file2); + + res.add(ConcurrentUtil.futureGet(res1)); + res.add(ConcurrentUtil.futureGet(res2)); + return Result.success(res); + } } diff --git a/src/main/java/com/example/filesystem/exception/GlobalExceptionHandler.java b/src/main/java/com/example/filesystem/exception/GlobalExceptionHandler.java index e3ce73c..94547c2 100644 --- a/src/main/java/com/example/filesystem/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/example/filesystem/exception/GlobalExceptionHandler.java @@ -31,7 +31,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(BaseException.class) public Result handleException(HttpServletRequest request, Exception ex) { - logger.error("Handle Exception Request Url:{},Exception:{}", request.getRequestURL(), ex); + logger.error("Handle Exception Request Url:%s,Exception:%s", request.getRequestURL(), ex); Result result; //系统异常 if (ex instanceof BaseException) { diff --git a/src/main/java/com/example/filesystem/pojo/vo/OSSFileVO.java b/src/main/java/com/example/filesystem/pojo/vo/OSSFileVO.java index d74fd84..6bc0919 100644 --- a/src/main/java/com/example/filesystem/pojo/vo/OSSFileVO.java +++ b/src/main/java/com/example/filesystem/pojo/vo/OSSFileVO.java @@ -19,4 +19,5 @@ public class OSSFileVO extends CommonFileVO{ @NotBlank(message = "bucket不能为空") private String bucket; + } diff --git a/src/main/java/com/example/filesystem/util/ConcurrentUtil.java b/src/main/java/com/example/filesystem/util/ConcurrentUtil.java new file mode 100644 index 0000000..c68064b --- /dev/null +++ b/src/main/java/com/example/filesystem/util/ConcurrentUtil.java @@ -0,0 +1,29 @@ +package com.example.filesystem.util; + +import com.example.filesystem.common.BaseException; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; + +/** + * @Description + * @Author welsir + * @Date 2023/12/23 20:03 + */ +public class ConcurrentUtil { + + public static Future doJob(ExecutorService executorService, Callable callable) { + return executorService.submit(callable); + } + + public static T futureGet(Future future) { + try { + return future.get(); + } catch (InterruptedException | ExecutionException e) { + throw new BaseException(e.toString()); + } + } + +}