跳至主要內容

切片上传

程序员李某某大约 1 分钟

切片上传

后端


@PostMapping("/verFileIsExist")
public Object verFileIsExist(@RequestBody Map<String, Object> payload) {
    String fileHash = (String) payload.get("fileHash");
    String suffix = (String) payload.get("suffix");
    Map<String, Object> body = new HashMap<>();
    try {
        log.info("fileHash:{},suffix:{}", fileHash, suffix);
        // 检查文件夹是否存在,不存在则创建
        Path path = Paths.get("./filelist");
        if (!Files.exists(path)) {
            Files.createDirectory(path);
        }

        // 检查文件是否存在,若存在,则无需上传
        Path file = Paths.get("./filelist/" + fileHash + "." + suffix);
        if (Files.exists(file)) {
            body.put("code", 200);
            body.put("shouldUpload", false);
            return ResponseEntity.ok(body);
        } else {
            // 获取已上传的文件列表
            Path fileDir = Paths.get("./filelist/" + fileHash);
            if (!Files.exists(fileDir)) {
                body.put("code", 200);
                body.put("shouldUpload", true);
                body.put("uploadedChunkList", new ArrayList<String>());
                return ResponseEntity.ok(body);
            }


            if (Files.isDirectory(fileDir)) {
                List<String> nameList = Files.list(fileDir).map(f -> f.getFileName().toString()).collect(Collectors.toList());
                body.put("code", 200);
                body.put("shouldUpload", true);
                body.put("uploadedChunkList", nameList);
                return ResponseEntity.ok(body);
            }

            body.put("code", 500);
            return ResponseEntity.ok(body);
        }
    } catch (IOException e) {
        e.printStackTrace();
        body.put("code", 500);
        return ResponseEntity.ok(body);
    }
}

@PostMapping("/upload")
public Object upload(@RequestParam("chunk") MultipartFile chunk,
                        @RequestParam("hash") String hash,
                        @RequestParam("suffix") String suffix) {

    Map<String, Object> body = new HashMap<>();
    String fileName = hash.split("-")[0];
    try {

        String directoryPath = "D:\\IdeaProjects\\SpringBoot\\filelist\\" + fileName;
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            directory.mkdirs(); // 创建目录,如果父目录不存在,也一同创建
        }

        String filePath = directoryPath + "\\" + hash + "." + suffix;
        File file = new File(filePath);
        chunk.transferTo(file);

        return ResponseEntity.ok("received file chunk");
    } catch (IOException e) {
        e.printStackTrace();
        body.put("code", 500);
        return ResponseEntity.ok(body);
    }
}

@PostMapping("/merge")
public Object merge(@RequestBody Map<String, Object> payload) throws IOException {
    String fileHash = (String) payload.get("fileHash");
    String suffix = (String) payload.get("suffix");
    Integer size = (Integer) payload.get("size");
    Map<String, Object> body = new HashMap<>();

    Path fileDir = Paths.get("./filelist/" + fileHash);
    if (Files.isDirectory(fileDir)) {
        // 获取并排序文件
        List<Path> files = Files.list(fileDir)
                .filter(Files::isRegularFile)
                .sorted(Comparator.comparingInt(a -> Integer.parseInt(a.getFileName().toString().split("-")[1].split("\\.")[0])))
                .collect(Collectors.toList());

        // 创建输出文件
        File outputFile = new File("./filelist/" + fileHash + "." + suffix);
        if (!outputFile.exists()) {
            outputFile.createNewFile();
        }


        try (OutputStream os = new FileOutputStream(outputFile, true)) {
            for (Path file : files) {
                Files.copy(file, os);
                Files.delete(file);
            }
            Files.delete(fileDir);
        }

        body.put("code", 200);
        body.put("message", "success");
        return ResponseEntity.ok(body);
    }
    body.put("code", 500);
    return ResponseEntity.ok(body);
}


前端

上次编辑于:
贡献者: ext.liyuanhao3