diff --git a/project2/file-folder.lnk b/project2/file-folder.lnk new file mode 100644 index 0000000..bb92307 Binary files /dev/null and b/project2/file-folder.lnk differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class index 92aa92d..c48bc68 100644 Binary files a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/OutputController.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/ScoreController.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/ScoreController.class index 21467d9..3fb6a87 100644 Binary files a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/ScoreController.class and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/ScoreController.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/SystemController.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/SystemController.class new file mode 100644 index 0000000..d83105d Binary files /dev/null and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/controller/SystemController.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/helper/CsvHelper.class b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/helper/CsvHelper.class new file mode 100644 index 0000000..82bd420 Binary files /dev/null and b/project2/out/artifacts/project2_war_exploded/WEB-INF/classes/cyou/chenx221/helper/CsvHelper.class differ diff --git a/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreQueryResult.jsp b/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreQueryResult.jsp index 887312e..cf0ae15 100644 --- a/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreQueryResult.jsp +++ b/project2/out/artifacts/project2_war_exploded/WEB-INF/views/scoreQueryResult.jsp @@ -130,7 +130,19 @@ style="background-image: url('${pageContext.request.contextPath}/resources/img/jason-blackeye-nyL-rzwP-Mk-unsplash.jpg'); margin-top: -58.59px;">
- +
+
+
+ +
+
+
+
+ +
+
+
@@ -172,7 +184,30 @@ + diff --git a/project2/src/main/java/cyou/chenx221/controller/OutputController.java b/project2/src/main/java/cyou/chenx221/controller/OutputController.java index 27260c4..72c53e8 100644 --- a/project2/src/main/java/cyou/chenx221/controller/OutputController.java +++ b/project2/src/main/java/cyou/chenx221/controller/OutputController.java @@ -15,10 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; +import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; @@ -124,4 +121,30 @@ public class OutputController { } return data; } + + @GetMapping("/get-csv") + public void getCSV(HttpServletResponse response, @RequestParam("filename") String filename) throws IOException { + String filePath = "file" + File.separator + filename; + + File file = new File(filePath); + if (file.exists()) { + // 设置响应头 + response.setHeader("Content-Disposition", "attachment; filename=" + file.getName()); + response.setContentType("text/csv"); + + // 读取文件内容并写入响应流 + try (InputStream inputStream = new FileInputStream(file); + OutputStream outputStream = response.getOutputStream()) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } catch (IOException e) { + // 处理异常 + } + } else { + System.out.println("异常"); + } + } } \ No newline at end of file diff --git a/project2/src/main/java/cyou/chenx221/controller/ScoreController.java b/project2/src/main/java/cyou/chenx221/controller/ScoreController.java index 44b8f67..c1588da 100644 --- a/project2/src/main/java/cyou/chenx221/controller/ScoreController.java +++ b/project2/src/main/java/cyou/chenx221/controller/ScoreController.java @@ -1,5 +1,6 @@ package cyou.chenx221.controller; +import cyou.chenx221.helper.CsvHelper; import cyou.chenx221.helper.UsernameHelper; import cyou.chenx221.pojo.Course; import cyou.chenx221.pojo.Score; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import java.io.IOException; import java.util.List; @Controller @@ -59,7 +61,7 @@ public class ScoreController { @RequestParam(value = "min", defaultValue = "-1", required = false) int min,//最低分 @RequestParam(value = "max", defaultValue = "-1", required = false) int max,//最高分 @RequestParam(value = "range_enabler", defaultValue = "false", required = false) boolean range_enabler,//是否启用分数范围 - Model model) { + Model model) throws IOException { //username helper start String username = new UsernameHelper().getCurrentUsername(); @@ -86,9 +88,8 @@ public class ScoreController { score = new Score((score_id == -1 ? null : score_id), ((student_id == -1 && name == null) ? null : (new Student((student_id == -1 ? null : student_id), name))), (course_id == -1 && course_name == null) ? null : (new Course((course_id == -1 ? null : course_id), course_name)), range_enabler ? (min == -1 ? null : min) : null, range_enabler ? (max == -1 ? null : max) : null); scores = scoreService.getQueryScores(score); - //TODO: query result download(csv) -// courses = courseService.getQueryCourses(course); -// model.addAttribute("score", score); + String download = new CsvHelper().generateScoreCSVFile(scores); + model.addAttribute("download", download); model.addAttribute("scores", scores); return "scoreQueryResult"; } diff --git a/project2/src/main/java/cyou/chenx221/controller/SystemController.java b/project2/src/main/java/cyou/chenx221/controller/SystemController.java new file mode 100644 index 0000000..88709ff --- /dev/null +++ b/project2/src/main/java/cyou/chenx221/controller/SystemController.java @@ -0,0 +1,9 @@ +package cyou.chenx221.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/system") +public class SystemController { +} diff --git a/project2/src/main/java/cyou/chenx221/helper/CsvHelper.java b/project2/src/main/java/cyou/chenx221/helper/CsvHelper.java new file mode 100644 index 0000000..e367634 --- /dev/null +++ b/project2/src/main/java/cyou/chenx221/helper/CsvHelper.java @@ -0,0 +1,133 @@ +package cyou.chenx221.helper; + +import cyou.chenx221.pojo.Course; +import cyou.chenx221.pojo.Score; +import cyou.chenx221.pojo.Student; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CsvHelper { + public String generateScoreCSVFile(List scores) throws IOException { + List headers; + headers = Arrays.asList("成绩ID", "学生ID", "学生姓名", "课程ID", "课程名称", "成绩"); + List> data = new ArrayList<>(); + for (Score score : scores) { + List rowData = Arrays.asList( + String.valueOf(score.getScoreID()), + String.valueOf(score.getStudent().getId()), + score.getStudent().getName(), + String.valueOf(score.getCourse().getCourseID()), + score.getCourse().getCourseName(), + String.valueOf(score.getScore()) + ); + data.add(rowData); + } + String directoryPath = "file"; + File directory = new File(directoryPath); + + if (!directory.exists()) { + boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin) + if (created) { + System.out.println("目录已创建"); + } else { + System.out.println("无法创建目录"); + } + } + + File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv"); + try (PrintWriter writer = new PrintWriter(csvFile); + CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { + csvPrinter.printRecord(headers); + for (List row : data) { + csvPrinter.printRecord(row); + } + csvPrinter.flush(); + } + return csvFile.getName(); + } + + public String generateStudentCSVFile(List students) throws IOException { + List headers; + headers = Arrays.asList("学生ID", "学生姓名", "性别", "出生日期", "联系方式", "班级"); + List> data = new ArrayList<>(); + for (Student student : students) { + List rowData = Arrays.asList( + String.valueOf(student.getId()), + student.getName(), + student.getSex(), + student.getBirthday().toString(), + String.valueOf(student.getPhone()), + String.valueOf(student.getClasses()) + ); + data.add(rowData); + } + String directoryPath = "file"; + File directory = new File(directoryPath); + + if (!directory.exists()) { + boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin) + if (created) { + System.out.println("目录已创建"); + } else { + System.out.println("无法创建目录"); + } + } + + File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv"); + try (PrintWriter writer = new PrintWriter(csvFile); + CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { + csvPrinter.printRecord(headers); + for (List row : data) { + csvPrinter.printRecord(row); + } + csvPrinter.flush(); + } + return csvFile.getName(); + } + + public String generateCourseCSVFile(List courses) throws IOException { + List headers; + headers = Arrays.asList("课程ID", "课程名", "课程描述"); + List> data = new ArrayList<>(); + for (Course course : courses) { + List rowData = Arrays.asList( + String.valueOf(course.getCourseID()), + course.getCourseName(), + course.getDescription() + ); + data.add(rowData); + } + String directoryPath = "file"; + File directory = new File(directoryPath); + + if (!directory.exists()) { + boolean created = directory.mkdir(); //Be careful, path: tomcat's installation path (D:\softs\res\apache-tomcat-9.0.75-windows-x64\apache-tomcat-9.0.75\bin) + if (created) { + System.out.println("目录已创建"); + } else { + System.out.println("无法创建目录"); + } + } + + File csvFile = new File("file/export-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".csv"); + try (PrintWriter writer = new PrintWriter(csvFile); + CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { + csvPrinter.printRecord(headers); + for (List row : data) { + csvPrinter.printRecord(row); + } + csvPrinter.flush(); + } + return csvFile.getName(); + } + +} diff --git a/project2/src/main/resources/spring-security.xml b/project2/src/main/resources/spring-security.xml index 55b44eb..af4a99b 100644 --- a/project2/src/main/resources/spring-security.xml +++ b/project2/src/main/resources/spring-security.xml @@ -25,6 +25,7 @@ +
- +
+
+
+ +
+
+
+
+ +
+
+
@@ -172,7 +184,30 @@ +