一、项目简述
功能:用户的邮箱注册、验证码验证以及用户登录。
不需要注册账号,也可以上传满足条件的临时文件,但是只4小时内有效。
文件的管理,上传、下载、重命名、删除、查看统计数据、分类管理等。
文件夹的管理,创建、删除、重命名。
文件的分享,支持通过链接和二维码的分享方式等等,以及管理员对用户的管理等等。
二、项目运行
环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
JSP +Springboot+ SpringMVC + MyBatis + ThymeLeaf + FTP+ JavaScript + JQuery + Ajax + maven等等
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
//包含的子文件夹
List<FileFolder> folders = null;
//包含的文件
List<MyFile> files = null;
//当前文件夹信息
FileFolder nowFolder = null;
//当前文件夹的相对路径
List<FileFolder> location = new ArrayList<>();
if (fId == null || fId <= 0) {
//代表当前为根目录
fId = 0;
folders = fileFolderService.getRootFoldersByFileStoreId(loginUser.getFileStoreId());
files = myFileService.getRootFilesByFileStoreId(loginUser.getFileStoreId());
nowFolder = FileFolder.builder().fileFolderId(fId).build();
location.add(nowFolder);
} else {
//当前为具体目录,访问的文件夹不是当前登录用户所创建的文件夹
FileFolder folder = fileFolderService.getFileFolderByFileFolderId(fId);
}
}
return false;
}
}
登录控制器:
/**
* @Description 登录控制器
* @return
**/
@Controller
public class LoginController extends BaseController {
private Logger logger = LogUtils.getInstance(LoginController.class);
/**
* @Description 免登陆用户入口,用于本地开发测试,上线运营为了安全请删除此方法
* @Author xw
* @Date 15:17 2020/2/26
* @Param []
* @return java.lang.String
**/
@GetMapping("/admin")
public String adminLogin(){
User user = userService.getUserByOpenId("123456");
user = User.builder()
.openId(openID).userName(removeNonBmpUnicode(userInfoBean.getNickname()))
.imagePath(userInfoBean.getAvatar().getAvatarURL100()).
registerTime(new Date()).build();
if (userService.insert(user)){
logger.info("注册用户成功!当前注册用户" + user);
FileStore store = FileStore.builder().userId(user.getUserId()).build();
if (fileStoreService.addFileStore(store) == 1){
user.setFileStoreId(store.getFileStoreId());
userService.update(user);
logger.info("注册仓库成功!当前注册仓库" + store);
}
} else {
logger.error("注册用户失败!");
}
}else {
user.setUserName(removeNonBmpUnicode(userInfoBean.getNickname()));
user.setImagePath(userInfoBean.getAvatar().getAvatarURL100());
userService.update(user);
}
logger.info("QQ用户登录成功!"+user);
session.setAttribute("loginUser", user);
return "redirect:/index";
if (error == 1) {
map.put("error", "添加失败!当前已存在同名文件夹");
}
if (error == 2) {
map.put("error", "重命名失败!文件夹已存在");
}
}
//包含的子文件夹
List<FileFolder> folders = null;
//包含的文件
List<MyFile> files = null;
//当前文件夹信息
FileFolder nowFolder = null;
//当前文件夹的相对路径
List<FileFolder> location = new ArrayList<>();
if (fId == null || fId <= 0) {
//代表当前为根目录
fId = 0;
folders = fileFolderService.getRootFoldersByFileStoreId(loginUser.getFileStoreId());
files = myFileService.getRootFilesByFileStoreId(loginUser.getFileStoreId());
nowFolder = FileFolder.builder().fileFolderId(fId).build();
location.add(nowFolder);
} else {
//当前为具体目录,访问的文件夹不是当前登录用户所创建的文件夹
String path = "temp/"+dateStr +"/"+UUID.randomUUID();
try {
if (FtpUtil.uploadFile("/"+path, name, file.getInputStream())){
//上传成功
logger.info("临时文件上传成功!"+name);
String size = String.valueOf(file.getSize());
TempFile tempFile = TempFile.builder().fileName(name).filePath(path).size(size).uploadTime(new Date()).build();
if (tempFileService.insert(tempFile)) {
try {
String id = UUID.randomUUID().toString();
String p = request.getSession().getServletContext().getRealPath("/user_img/");
Long t = tempFile.getUploadTime().getTime();
url = url+"/file/share?t="+ UUID.randomUUID().toString().substring(0,10) +"&f="+tempFile.getFileId()+"&p="+size+"&flag=2";
File targetFile = new File(p, "");
return map;
}
/**
* @Description 网盘的文件下载
* @Author xw
* @Date 23:13 2020/2/10
* @Param [fId]
* @return void
**/
@GetMapping("/downloadFile")
public String downloadFile(@RequestParam Integer fId){
if (fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission() == 2){
logger.error("用户没有下载文件的权限!下载失败...");
return "redirect:/error401Page";
}
//获取文件信息
MyFile myFile = myFileService.getFileByFileId(fId);
String remotePath = myFile.getMyFilePath();
String fileName = myFile.getMyFileName()+myFile.getPostfix();
try {
String remotePath = myFile.getMyFilePath();
String fileName = myFile.getMyFileName()+myFile.getPostfix();
try {
//去FTP上拉取
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("utf-8");
// 设置返回类型
response.setContentType("multipart/form-data");
// 文件名转码一下,不然会出现中文乱码
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
boolean flag = FtpUtil.downloadFile("/" + remotePath, fileName, os);
if (flag) {
myFileService.updateFile(
MyFile.builder().myFileId(myFile.getMyFileId()).downloadTime(myFile.getDownloadTime() + 1).build());
os.flush();
os.close();
logger.info("文件下载成功!" + myFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return void
* @Description 向注册邮箱发送验证码, 并验证邮箱是否已使用
* @Author xw
* @Date 19:32 2020/1/29
* @Param [userName, email, password]
**/
@ResponseBody
@RequestMapping("/sendCode")
public String sendCode(String userName, String email, String password) {
User userByEmail = userService.getUserByEmail(email);
if (userByEmail != null) {
logger.error("发送验证码失败!邮箱已被注册!");
return "exitEmail";
}
return "redirect:/error400Page";
}
if(flag == 1){
//获取文件信息
MyFile myFile = myFileService.getFileByFileId(f);
if (myFile == null){
return "redirect:/error404Page";
}
String pwd = myFile.getUploadTime().getTime()+""+myFile.getSize();
if (!pwd.equals(p)){
return "redirect:/error400Page";
}
remotePath = myFile.getMyFilePath();
fileName = myFile.getMyFileName()+myFile.getPostfix();
}else if(flag == 2){
TempFile tempFile = tempFileService.queryById(f);
if (tempFile == null){
return "redirect:/error404Page";
}
Long test = tempFile.getUploadTime().getTime();
String pwd = tempFile.getSize();
if (!pwd.equals(p)){
return "redirect:/error400Page";
}
remotePath = tempFile.getFilePath();
fileName = tempFile.getFileName();
}else {
return "redirect:/error400Page";
}
}
}
}
}
return "redirect:/files?fId="+myFile.getParentFolderId();
}
/**
* @Description 获得二维码
* @Author xw
* @Date 15:20 2020/2/12
* @Param [id, url]
* @return java.util.Map<java.lang.String,java.lang.Object>
**/
@GetMapping("getQrCode")
@ResponseBody
public Map<String,Object> getQrCode(@RequestParam Integer id,@RequestParam String url){
Map<String,Object> map = new HashMap<>();
map.put("imgPath","https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2654852821,3851565636&fm=26&gp=0.jpg");
if (id != null){
MyFile file = myFileService.getFileByFileId(id);
if (file != null){
try {
String path = request.getSession().getServletContext().getRealPath("/user_img/");
url = url+"/file/share?t="+ UUID.randomUUID().toString().substring(0,10) +"&f="+file.getMyFileId()+"&p="+file.getUploadTime().getTime()+""+file.getSize()+"&flag=1";
File targetFile = new File(path, "");
if (!targetFile.exists()) {
targetFile.mkdirs();