博主主页:Java旅途
简介:分享计算机知识、学习路线、系统源码及教程
文末获取源码
一、项目介绍
在线论坛是由SpringBoot+Mybatis开发的,论坛提供用户注册,整体分为管理员和普通用户两种角色。管理员端可以生产邀请码,用户注册时提供邀请码进行注册,不输入邀请码也可以注册。
管理员功能如下:
- 邀请码管理
- 用户查询
- 用户禁言
- 用户解禁
- 网盘管理
- 发帖
- 看帖
- 删帖
- 回复贴
普通用户功能如下:
- 注册登录
- 发帖
- 回复贴
- 删除自己的贴子
- 看帖
- 网盘管理
二、技术框架
- 后端:SpringBoot,Mybatis
- 前端:bootstrap,jquery
三、安装教程
- 用idea打开项目
- 在idea中配置jdk环境
- 配置maven环境并下载依赖
- 新建数据库,导入数据库文件
- 在application.yml文件中将数据库账号密码改成自己本地的
- 网盘上传的资源存储在E盘,如果你的电脑没有E盘,则需要改成其他盘,具体位置在application.yml文件,将文件里面的E:\\file改成你本地的即可。
- 启动运行, 管理员账号密码 admin/123456
四、项目截图
五、相关代码
IndexController
package com.zzx.controller;import com.zzx.service.PostService;
import com.zzx.service.UserService;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpSession;
import java.util.HashMap;import java.util.Map;@Controller
public class IndexController {@Autowiredprivate PostService postService;@Autowiredprivate UserService userService;@RequestMapping(value = "/", method = RequestMethod.GET)public String index(HttpSession session, Model model, @RequestParam(value = "page", required = false) Long page) {Map<String, Long> map = new HashMap<>();map.put("startPage", page == null ? 0 : page - 1);model.addAttribute("page", postService.findPostByPage(map));return "index";}}
NetdiskController
package com.zzx.controller;import com.zzx.config.FileUploadProperteis;
import com.zzx.config.NetdiskConfig;
import com.zzx.model.File;
import com.zzx.model.User;
import com.zzx.service.FileService;
import com.zzx.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;@Controller
public class NetdiskController {@Autowiredprivate FileService fileService;@Autowiredprivate FileUploadProperteis fileUploadProperteis;/*** 页面跳转** @return*/@RequestMapping(value = "/netdisk.do", method = RequestMethod.GET)public String netdisk(Model model, HttpSession session) {User user = (User)session.getAttribute("user");if (user != null) {model.addAttribute("fileList", fileService.findFileByUid(user.getUid()));model.addAttribute("size", fileService.getAvailableSizeByUid(user.getUid()) * 1.0 / NetdiskConfig.GB_1);return "netdisk";} elsereturn "redirect:/";}/*** 删除自己上传的文件** @param fileId* @param session* @return*/@RequestMapping(value = "/deletefile/{fileId}", method = RequestMethod.GET)public String deleteFile(@PathVariable Integer fileId, HttpSession session) {User user = (User)session.getAttribute("user");com.zzx.model.File file = fileService.findFileById(fileId);if (user != null && user.getUid() == file.getUser().getUid()) {fileService.delete(fileId);return "redirect:/netdisk.do";}return "redirect:/";}/*** 处理文件上传** @param file* @param session* @return*/@PostMapping(value = "/upload.do")public String upload(MultipartFile file, HttpSession session, Model model) {User user = (User)session.getAttribute("user");if (user == null) { //未登录return "redirect:/";}if (file.getSize() > fileService.getAvailableSizeByUid(user.getUid())) {model.addAttribute("message", "你的剩余容量不足,充钱才能变得更强");return "error";}if (file.getSize() <= 0 || file.getSize() > NetdiskConfig.GB_1) //文件大小不符合范围{return "redirect:/netdisk.do";}String fileName = file.getOriginalFilename(); //获取文件名if (!fileName.contains(".")) //源文件无格式,避免后续处理出错fileName = fileName + ".unknow";String[] formatNames = fileName.split("\\.");//获取文件格式String formatName = "." + formatNames[formatNames.length - 1];String filePath = fileUploadProperteis.getUploadFolder();// 获取配置E:\\filejava.io.File folder = new java.io.File(filePath); //检测文件夹是否存在if (!folder.exists())folder.mkdirs();String uuid = UUIDUtils.generateUUID();java.io.File dest = new java.io.File(filePath + "/" + uuid + formatName);try {file.transferTo(dest);File fileInfo = new com.zzx.model.File();fileInfo.setFileName(fileName);fileInfo.setFilePath(fileUploadProperteis.getStaticAccessPath().replaceAll("\\*", "") + uuid + formatName);fileInfo.setFileSize(file.getSize());fileInfo.setUploadTime(new Date());fileInfo.setState(1);fileInfo.setUser(user);fileService.saveFileInfo(fileInfo);return "redirect:/netdisk.do";} catch (IOException e) {e.printStackTrace();return "error";}}@RequestMapping(value = "/download")public void download(Integer fileId, HttpServletResponse response, HttpSession session) throws IOException {Object obj = session.getAttribute("user");if (null == obj)response.sendRedirect("/");User user = (User)obj;try {File file = fileService.findFileById(fileId);if (file.getState() == 0 || file.getUser().getUid() != user.getUid())response.sendRedirect("/error");response.reset();response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getFileName(), "UTF-8"));response.setHeader("Connection", "close");response.setHeader("Content-Type", "application/octet-stream");OutputStream os = response.getOutputStream();java.io.File diskFile = new java.io.File(fileUploadProperteis.getUploadFolder() + "/" + file.getFilePath().split("/")[2]);FileInputStream fis = new FileInputStream(diskFile);response.setHeader("Content-Length", String.valueOf(diskFile.length()));byte[] buf = new byte[(int)diskFile.length()];fis.read(buf);os.write(buf);} catch (IOException e) {e.printStackTrace();response.sendRedirect("/error");}}}
大家点赞、收藏、关注、评论啦 、👇🏻点开下方卡片👇🏻关注后回复 102