返回

用SpringBoot创建灵活文件系统

后端

文件上传和下载

文件上传是指将文件从本地计算机传输到服务器,文件下载是指将文件从服务器下载到本地计算机。在SpringBoot项目中,您可以使用以下代码实现文件上传:

@PostMapping("/upload")
public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file) {
    // 文件校验
    if (file.isEmpty()) {
        return ResponseEntity.badRequest().body("文件为空");
    }
    // 文件格式校验
    String fileName = file.getOriginalFilename();
    String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
    if (!fileType.equals("jpg") && !fileType.equals("png") && !fileType.equals("jpeg")) {
        return ResponseEntity.badRequest().body("文件格式不正确");
    }
    // 文件大小校验
    if (file.getSize() > 1024 * 1024) {
        return ResponseEntity.badRequest().body("文件太大");
    }
    // 文件保存
    try {
        byte[] bytes = file.getBytes();
        Path path = Paths.get("uploads/" + fileName);
        Files.write(path, bytes);
    } catch (IOException e) {
        e.printStackTrace();
        return ResponseEntity.internalServerError().body("文件保存失败");
    }
    return ResponseEntity.ok().body("文件上传成功");
}

您可以使用以下代码实现文件下载:

@GetMapping("/download")
public ResponseEntity<Object> downloadFile(@RequestParam("fileName") String fileName) {
    // 文件路径
    Path path = Paths.get("uploads/" + fileName);
    // 文件不存在
    if (!Files.exists(path)) {
        return ResponseEntity.notFound().build();
    }
    // 文件下载
    try {
        byte[] bytes = Files.readAllBytes(path);
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
                .body(bytes);
    } catch (IOException e) {
        e.printStackTrace();
        return ResponseEntity.internalServerError().body("文件下载失败");
    }
}

文件管理

SpringBoot项目的文件管理功能包括文件上传、文件下载、文件删除、文件重命名等。您可以使用以下代码实现文件删除:

@DeleteMapping("/delete")
public ResponseEntity<Object> deleteFile(@RequestParam("fileName") String fileName) {
    // 文件路径
    Path path = Paths.get("uploads/" + fileName);
    // 文件不存在
    if (!Files.exists(path)) {
        return ResponseEntity.notFound().build();
    }
    // 文件删除
    try {
        Files.delete(path);
    } catch (IOException e) {
        e.printStackTrace();
        return ResponseEntity.internalServerError().body("文件删除失败");
    }
    return ResponseEntity.ok().body("文件删除成功");
}

您可以使用以下代码实现文件重命名:

@PutMapping("/rename")
public ResponseEntity<Object> renameFile(@RequestParam("fileName") String fileName, @RequestParam("newFileName") String newFileName) {
    // 文件路径
    Path path = Paths.get("uploads/" + fileName);
    // 文件不存在
    if (!Files.exists(path)) {
        return ResponseEntity.notFound().build();
    }
    // 文件重命名
    try {
        Files.move(path, Paths.get("uploads/" + newFileName));
    } catch (IOException e) {
        e.printStackTrace();
        return ResponseEntity.internalServerError().body("文件重命名失败");
    }
    return ResponseEntity.ok().body("文件重命名成功");
}

总结

SpringBoot项目的文件管理功能可以帮助您轻松处理文件上传下载,包括格式校验、大小校验、格式准入等功能。本文为您介绍了如何使用SpringBoot创建灵活文件系统。