From 2303ba3d0bcc38607dd01329a12e4f4b74b444c0 Mon Sep 17 00:00:00 2001
From: xingjinshuang <xingjs@qq.com>
Date: 星期五, 27 十二月 2024 17:49:53 +0800
Subject: [PATCH] @xingjs@20241227@优化解决获取建筑物图层方法

---
 src/main/java/com/se/simu/controller/FilesUploadController.java |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/FilesUploadController.java b/src/main/java/com/se/simu/controller/FilesUploadController.java
index 4727136..c23a4e6 100644
--- a/src/main/java/com/se/simu/controller/FilesUploadController.java
+++ b/src/main/java/com/se/simu/controller/FilesUploadController.java
@@ -41,6 +41,53 @@
 
 
     @ApiOperation("1-涓婁紶鍗曚釜鏂囦欢")
+    @PostMapping("/upload")
+    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
+        if (file.isEmpty()) {
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("鏂囦欢涓嶈兘涓虹┖");
+        }
+        String targetDir = Paths.get(uploadedFolder, "upload").toString();
+        log.info("鐩爣鐩綍: {}", targetDir);
+        createDirectoriesIfNotExists(targetDir);
+        try {
+            file.transferTo(Paths.get(targetDir, file.getOriginalFilename()));
+            return ResponseEntity.ok("鏂囦欢涓婁紶鎴愬姛");
+        } catch (IOException e) {
+            log.error("鏂囦欢涓婁紶澶辫触", e);
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("鏂囦欢涓婁紶澶辫触");
+        }
+    }
+
+    @ApiOperation("2-0涓婁紶澶氫釜鏂囦欢")
+    @PostMapping("/uploadFiles")
+    public ResponseEntity<String> uploadFiles(List<MultipartFile> files) throws IOException {
+        if (files.isEmpty()) {
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("鏂囦欢涓嶈兘涓虹┖");
+        }
+        // 涓婁紶鏂囦欢璺緞
+        String targetDir = Paths.get(uploadedFolder, "upload").toString();
+        log.info("鐩爣鐩綍: {}", targetDir);
+        createDirectoriesIfNotExists(targetDir);
+        CompletableFuture<Void> allUploadTasks = CompletableFuture.allOf(files.stream()
+                .map(file -> CompletableFuture.runAsync(() -> {
+                    try {
+                        file.transferTo(Paths.get(targetDir, file.getOriginalFilename()));
+                    } catch (IOException e) {
+                        log.error("鏂囦欢涓婁紶澶辫触", e);
+                        throw new RuntimeException("鏂囦欢涓婁紶澶辫触");
+                    }
+                })).toArray(CompletableFuture[]::new));
+        try {
+            allUploadTasks.get();
+            return ResponseEntity.ok("鎵�鏈夋枃浠朵笂浼犳垚鍔�!涓婁紶鐩綍涓猴細" + targetDir);
+        } catch (InterruptedException | ExecutionException e) {
+            log.error("鏂囦欢涓婁紶澶辫触", e);
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("鏂囦欢涓婁紶澶辫触");
+        }
+    }
+
+
+    @ApiOperation("1-1 -涓婁紶鍗曚釜鏂囦欢")
     @ApiImplicitParam(name = "filePathName", value = "鏂囦欢澶瑰悕绉�", required = true, dataType = "String", paramType = "query", example = "upload", dataTypeClass = String.class)
     @PostMapping("/uploadSingleFile")
     public ResponseEntity<String> uploadSingleFile(@RequestParam("file") MultipartFile file, @RequestParam("filePathName") String filePathName) throws IOException {
@@ -68,6 +115,9 @@
     @ApiImplicitParam(name = "filePathName", value = "鏂囦欢澶瑰悕绉�", required = true, dataType = "String", paramType = "query", example = "upload", dataTypeClass = String.class)
     @PostMapping("/uploadMultipleFiles")
     public ResponseEntity<String> uploadMultipleFiles(List<MultipartFile> files, @RequestParam("filePathName") String filePathName) throws IOException {
+        if (files.isEmpty()) {
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("鏂囦欢涓嶈兘涓虹┖");
+        }
         // 鍒ゆ柇 filePathName 鏄惁涓虹┖
         if (filePathName == null || filePathName.isEmpty()) {
             //  return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("鏂囦欢澶瑰悕绉颁笉鑳戒负绌�");

--
Gitblit v1.9.3