答案:系统基于Spring Boot实现学生提交作业、教师查看管理功能,包含用户认证、文件上传、作业管理模块;使用Spring Security控制角色权限,学生可上传文件并存储至本地,教师可查看作业列表并下载,数据通过JPA与数据库交互,初期采用H2或MySQL存储用户和作业信息,文件存于磁盘指定目录,结合Thymeleaf展示界面,实现基础但完整的作业提交流程。

开发一个小型作业提交系统,核心是实现学生上传作业、教师查看和管理作业的基本功能。Java 适合构建这类系统,尤其是结合 Spring Boot 可快速搭建 Web 应用。下面从结构设计到关键代码逐步说明。
小型作业提交系统应包含以下基本功能:
主要模块包括:用户认证、文件上传、作业管理、数据库交互。
推荐使用 Spring Boot 快速开发,简化配置:
立即学习“Java免费学习笔记(深入)”;
创建 Spring Boot 项目(可通过 Spring Initializr 添加 web、jpa、security、thymeleaf 等依赖)。
定义几个核心实体类:
@Entity
public class User {
@Id @GeneratedValue
private Long id;
private String username;
private String password;
private String role; // "STUDENT" 或 "TEACHER"
// getter/setter
}
@Entity
public class Assignment {
@Id @GeneratedValue
private Long id;
private String title;
private String course;
private String filename;
private String filepath;
private LocalDateTime submitTime;
@ManyToOne
private User student;
// getter/setter
}
学生提交作业时需上传文件。Spring MVC 支持 MultipartFile 处理上传:
@PostMapping("/submit")
public String submitAssignment(
@RequestParam String title,
@RequestParam String course,
@RequestParam("file") MultipartFile file,
Principal principal,
Model model) {
if (file.isEmpty()) {
model.addAttribute("error", "文件不能为空");
return "submit";
}
String uploadDir = "uploads/";
String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
Path path = Paths.get(uploadDir + fileName);
try {
Files.write(path, file.getBytes());
} catch (IOException e) {
model.addAttribute("error", "文件保存失败");
return "submit";
}
User student = userService.findByUsername(principal.getName());
Assignment assignment = new Assignment();
assignment.setTitle(title);
assignment.setCourse(course);
assignment.setFilename(fileName);
assignment.setFilepath(path.toString());
assignment.setSubmitTime(LocalDateTime.now());
assignment.setStudent(student);
assignmentService.save(assignment);
model.addAttribute("msg", "作业提交成功!");
return "submit";
}
确保在 application.properties 中设置上传限制:
spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB
教师登录后可查看所有提交的作业:
@GetMapping("/assignments")
public String listAssignments(Model model, Principal principal) {
List<Assignment> assignments = assignmentService.findAll();
model.addAttribute("assignments", assignments);
return "assignments";
}
前端显示表格,每行提供“下载”链接:
<a th:href="@{/download(id=${a.id})}">下载</a>
下载接口示例:
@GetMapping("/download")
public void downloadFile(@RequestParam Long id, HttpServletResponse response) {
Assignment assignment = assignmentService.findById(id);
Path path = Paths.get(assignment.getFilepath());
if (Files.exists(path)) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + assignment.getFilename() + "\"");
try (InputStream is = Files.newInputStream(path)) {
StreamUtils.copy(is, response.getOutputStream());
} catch (IOException e) {
response.setStatus(500);
}
}
}
使用 Spring Security 限制访问:
配置 SecurityConfig 类,重写 configure(HttpSecurity) 方法即可实现路径拦截。
基本上就这些。小型系统不复杂但容易忽略细节,比如文件名冲突、路径安全、用户状态保持等。初期可先跑通流程,再逐步优化界面和异常处理。
以上就是如何用Java开发小型作业提交系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号