【SpringBoot】SpringBoot实现档案管理系统代码

以下是一个简单的Spring Boot档案管理系统的代码实现,包括模型类、服务类和控制器类:

  1. File类
java
@Entity
@Table(name = "files")
public class File {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private long size;
    private Date creationDate;

    public File() {
    }

    public File(String name, long size, Date creationDate) {
        this.name = name;
        this.size = size;
        this.creationDate = creationDate;
    }

    // getters and setters
}

2.FileService类

java
@Service
public class FileService {
    @Autowired
    private FileRepository fileRepository;

    //上传文件方法
    public void uploadFile(MultipartFile file) throws IOException {
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        File newFile = new File(filename, file.getSize(), new Date());
        fileRepository.save(newFile);

        Path uploadPath = Paths.get("uploads");
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }

        try (InputStream inputStream = file.getInputStream()) {
            Path filePath = uploadPath.resolve(filename);
            Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            throw new IOException("Could not save file: " + filename, e);
        }
    }
    //删除文件方法
    public void deleteFile(Long id) {
        Optional optionalFile = fileRepository.findById(id);
        if (optionalFile.isPresent()) {
            File file = optionalFile.get();
            fileRepository.delete(file);

            Path filePath = Paths.get("uploads").resolve(file.getName());
            try {
                Files.delete(filePath);
            } catch (IOException e) {
                System.err.println("Could not delete file: " + file.getName());
            }
        }
    }

    public List getAllFiles() {
        return fileRepository.findAll();
    }
}

3.FileController类
@RestController
@RequestMapping("/files")
public class FileController {
    @Autowired
    private FileService fileService;

    @GetMapping
    public List getAllFiles() {
        return fileService.getAllFiles();
    }

    @PostMapping
    public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        fileService.uploadFile(file);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity deleteFile(@PathVariable Long id) {
        fileService.deleteFile(id);
        return ResponseEntity.ok().build();
    }
}

4.应用程序主类

@SpringBootApplication
public class Application implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // 初始化代码
    }
}

注意:以上代码仅作为示例,实际的档案管理系统可能需要更多的功能和更多的代码,例如身份验证、文件类型限制、文件夹管理等。

你可能感兴趣的:(spring,boot,后端,java)