张洋洋
2025-01-09 6cb2134a53422f471f4f9b77c34d67e1fb4d31db
src/main/java/com/se/simu/controller/FilesUploadController.java
@@ -1,5 +1,8 @@
package com.se.simu.controller;
import com.se.simu.domain.vo.R;
import com.se.simu.utils.FileUtil;
import com.se.simu.utils.ZipUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@@ -15,6 +18,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@@ -25,7 +32,7 @@
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/v1/files")
public class FilesUploadController {
public class FilesUploadController extends BaseController {
    @Value("${simu-app.filePath}")
@@ -42,15 +49,73 @@
    @ApiOperation("1-上传单个文件")
    @PostMapping("/upload")
    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
    public R<Object> upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空");
            return success("文件不能为空");
        }
        String targetDir = Paths.get(uploadedFolder, "upload").toString();
        // 获取当前年月日
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String targetDir = Paths.get(uploadedFolder, date).toString();
        log.info("目标目录: {}", targetDir);
        createDirectoriesIfNotExists(targetDir);
        try {
            file.transferTo(Paths.get(targetDir, file.getOriginalFilename()));
            // 获取当前时分秒
            String time = LocalTime.now().format(DateTimeFormatter.ofPattern("HHmmss"));
            // 文件地址全称
            Path filePath = Paths.get(targetDir, time + "_" + file.getOriginalFilename());
            // 文件名
            file.transferTo(filePath);
            return success(targetDir + "\\" + time + "_" + file.getOriginalFilename(), "文件上传成功");
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return fail("文件上传失败");
        }
    }
    @ApiOperation("1-上传单个shp文件")
    @PostMapping("/uploadShp")
    public R<Object> uploadShp(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return success("文件不能为空");
        }
        // 获取当前年月日
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String targetDir = Paths.get(uploadedFolder, date).toString();
        log.info("目标目录: {}", targetDir);
        createDirectoriesIfNotExists(targetDir);
        try {
            // 文件地址全称
            Path filePath = Paths.get(targetDir, file.getOriginalFilename());
            // 文件名
            file.transferTo(filePath);
            ZipUtils.unzip(targetDir + "\\" + file.getOriginalFilename(),targetDir);
            File zipfile=new File(targetDir + "\\" + file.getOriginalFilename());
            zipfile.delete();
            return success(FileUtil.getShpPath(targetDir), "文件上传成功");
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return fail("文件上传失败");
        }
    }
    @ApiOperation("1-上传单个文件")
    @PostMapping("/uploads")
    public ResponseEntity<String> uploads(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空");
        }
        // 获取当前年月日
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String targetDir = Paths.get(uploadedFolder, date).toString();
        log.info("目标目录: {}", targetDir);
        createDirectoriesIfNotExists(targetDir);
        try {
            // 获取当前时分秒
            String time = LocalTime.now().format(DateTimeFormatter.ofPattern("HHmmss"));
            // 文件地址全称
            Path filePath = Paths.get(targetDir, time + "_" + file.getOriginalFilename());
            // 文件名
            file.transferTo(filePath);
            return ResponseEntity.ok("文件上传成功");
        } catch (IOException e) {
            log.error("文件上传失败", e);
@@ -58,6 +123,35 @@
        }
    }
    @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")