20 | * 前端控制器 21 | *
22 | * 23 | * @author IronmanJay 24 | * @since 2022-10-21 25 | */ 26 | @RestController 27 | @RequestMapping("/comment") 28 | public class CommentController { 29 | 30 | @Resource 31 | private ICommentService commentService; 32 | 33 | // 新增或者更新 34 | @PostMapping 35 | public Result save(@RequestBody Comment comment) { 36 | if (comment.getId() == null) { // 新增评论 37 | comment.setUserId(TokenUtils.getCurrentUser().getId()); 38 | comment.setTime(DateUtil.now()); 39 | if (comment.getPid() != null) { // 判断如果是回复,进行处理 40 | Integer pid = comment.getPid(); 41 | Comment pComment = commentService.getById(pid); 42 | if (pComment.getOriginId() != null) { // 如果当前回复的父级有祖宗,那么就设置相同的祖宗 43 | comment.setOriginId(pComment.getOriginId()); 44 | } else { // 否则就设置父级为当前回复的祖宗 45 | comment.setOriginId(comment.getPid()); 46 | } 47 | } 48 | } 49 | commentService.saveOrUpdate(comment); 50 | return Result.success(); 51 | 52 | } 53 | 54 | @DeleteMapping("/{id}") 55 | public Result delete(@PathVariable Integer id) { 56 | commentService.removeById(id); 57 | return Result.success(); 58 | } 59 | 60 | @PostMapping("/del/batch") 61 | public Result deleteBatch(@RequestBody List15 | * 前端控制器 16 | *
17 | * 18 | * @author IronmanJay 19 | * @since 2022-10-04 20 | */ 21 | @RestController 22 | @RequestMapping("/course") 23 | public class CourseController { 24 | 25 | @Resource 26 | private ICourseService courseService; 27 | 28 | // 新增或者更新 29 | @PostMapping 30 | public Result save(@RequestBody Course course) { 31 | courseService.saveOrUpdate(course); 32 | return Result.success(); 33 | } 34 | 35 | @PostMapping("/studentCourse/{courseId}/{studentId}") 36 | public Result studentCourse(@PathVariable Integer courseId, @PathVariable Integer studentId) { 37 | courseService.setStudentCourse(courseId, studentId); 38 | return Result.success(); 39 | } 40 | 41 | @DeleteMapping("/{id}") 42 | public Result delete(@PathVariable Integer id) { 43 | courseService.removeById(id); 44 | return Result.success(); 45 | } 46 | 47 | @PostMapping("/del/batch") 48 | public Result deleteBatch(@RequestBody List- >() {
111 | }, true);
112 | }
113 | return Result.success(files);
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/src/main/java/com/ironmanjay/springboot/controller/FileController.java:
--------------------------------------------------------------------------------
1 | package com.ironmanjay.springboot.controller;
2 |
3 | import cn.hutool.core.io.FileUtil;
4 | import cn.hutool.core.util.IdUtil;
5 | import cn.hutool.core.util.StrUtil;
6 | import cn.hutool.crypto.SecureUtil;
7 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
8 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
9 | import com.ironmanjay.springboot.common.Result;
10 | import com.ironmanjay.springboot.entity.Files;
11 | import com.ironmanjay.springboot.mapper.FileMapper;
12 | import org.springframework.beans.factory.annotation.Value;
13 | import org.springframework.data.redis.core.StringRedisTemplate;
14 | import org.springframework.web.bind.annotation.*;
15 | import org.springframework.web.multipart.MultipartFile;
16 |
17 | import javax.annotation.Resource;
18 | import javax.servlet.ServletOutputStream;
19 | import javax.servlet.http.HttpServletResponse;
20 | import java.io.File;
21 | import java.io.IOException;
22 | import java.net.URLEncoder;
23 | import java.util.List;
24 |
25 | /**
26 | * 文件上传与下载相关接口
27 | */
28 | @RestController
29 | @RequestMapping("/file")
30 | public class FileController {
31 |
32 | @Value("${files.upload.path}")
33 | private String fileUploadPath;
34 |
35 | @Resource
36 | private FileMapper fileMapper;
37 |
38 | @Resource
39 | private StringRedisTemplate stringRedisTemplate;
40 |
41 | public static final String FILES_KEY = "FILES_FRONT_ALL";
42 |
43 | /**
44 | * 文件上传接口
45 | *
46 | * @param file 前端传递过来的文件
47 | * @return 返回文件url
48 | * @throws IOException 异常处理
49 | */
50 | @PostMapping("/upload")
51 | public String upload(@RequestParam MultipartFile file) throws IOException {
52 | String originalFilename = file.getOriginalFilename();
53 | String type = FileUtil.extName(originalFilename);
54 | long size = file.getSize();
55 | // 定义一个文件唯一的标识码
56 | String uuid = IdUtil.fastSimpleUUID();
57 | String fileUUID = uuid + StrUtil.DOT + type;
58 | File uploadFile = new File(fileUploadPath + fileUUID);
59 | // 判断配置的文件目录是否存在,若不存在则创建一个新的文件目录
60 | File parentFile = uploadFile.getParentFile();
61 | if (!parentFile.exists()) {
62 | parentFile.mkdirs();
63 | }
64 | String url;
65 | // 获取文件的md5,通过对比md5避免重复上传相同内容的文件
66 | String md5 = SecureUtil.md5(file.getInputStream());
67 | // 从数据库查询是否存在相同的记录
68 | Files dbFiles = getFileByMd5(md5);
69 | if (dbFiles != null) { // 文件已存在
70 | url = dbFiles.getUrl();
71 | } else {
72 | // 上传文件到磁盘
73 | file.transferTo(uploadFile);
74 | // 数据库若不存在重复文件,则不删除刚才上传的文件
75 | url = "http://localhost:9090/file/" + fileUUID;
76 | }
77 | // 存储数据库
78 | Files saveFile = new Files();
79 | saveFile.setName(originalFilename);
80 | saveFile.setType(type);
81 | saveFile.setSize(size / 1024);
82 | saveFile.setUrl(url);
83 | saveFile.setMd5(md5);
84 | fileMapper.insert(saveFile);
85 | flushRedis(FILES_KEY);
86 | return url;
87 | }
88 |
89 | /**
90 | * 文件下载接口
91 | *
92 | * @param fileUUID 文件唯一标识符
93 | * @param response 请求响应
94 | * @throws IOException 异常处理
95 | */
96 | @GetMapping("/{fileUUID}")
97 | public void download(@PathVariable String fileUUID, HttpServletResponse response) throws IOException {
98 | // 根据文件的唯一标识码获取文件
99 | File uploadFile = new File(fileUploadPath + fileUUID);
100 | // 设置输出流的格式
101 | ServletOutputStream os = response.getOutputStream();
102 | response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileUUID, "UTF-8"));
103 | response.setContentType("application/octetcd ream");
104 | // 读取文件的字节流
105 | os.write(FileUtil.readBytes(uploadFile));
106 | os.flush();
107 | os.close();
108 | }
109 |
110 | /**
111 | * 通过文件的md5查询文件
112 | *
113 | * @param md5
114 | * @return 返回文件的md5
115 | */
116 | private Files getFileByMd5(String md5) {
117 | // 查询文件的md5是否存在
118 | QueryWrapper
19 | * 前端控制器 20 | *
21 | * 22 | * @author IronmanJay 23 | * @since 2022-09-14 24 | */ 25 | @RestController 26 | @RequestMapping("/menu") 27 | public class MenuController { 28 | 29 | @Resource 30 | private IMenuService menuService; 31 | 32 | @Resource 33 | private DictMapper dictMapper; 34 | 35 | // 新增或者更新 36 | @PostMapping 37 | public boolean save(@RequestBody Menu menu) { 38 | return menuService.saveOrUpdate(menu); 39 | } 40 | 41 | @DeleteMapping("/{id}") 42 | public Boolean delete(@PathVariable Integer id) { 43 | return menuService.removeById(id); 44 | } 45 | 46 | @PostMapping("/del/batch") 47 | public boolean deleteBatch(@RequestBody List