本系统是一个模拟百度网盘的系统,通过实现了图片/文本文件/视频等资源上传,并且分类管理、资源分享,实现了资源的在线管理。
(文末查看完整源码)
预登录
登录
用户管理
文件管理
网盘首页
文件夹
视频
文本文件
资源分享
本系统实现了以下功能:
1、注册登录
2、文件管理
3、资源管理
4、资源分类查看
5、创建文件夹
6、资源分享
7、用户管理
等
数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:Spring+Mybatis+springmvc+Maven
java端
实体类
Files.java
package com.cloud.pojo;
public class Files {
private int file_id;//文件id
private String file_name;//文件名
private String file_path;//文件路径
private int user_id;//上传文件用户id
public int getFile_id() {
return file_id;
}
public void setFile_id(int file_id) {
this.file_id = file_id;
}
public String getFile_name() {
return file_name;
}
public void setFile_name(String file_name) {
this.file_name = file_name;
}
public String getFile_path() {
return file_path;
}
public void setFile_path(String file_path) {
this.file_path = file_path;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
@Override
public String toString() {
return "Files [file_id=" + file_id + ", file_name=" + file_name + ", file_path=" + file_path + ", user_id="
+ user_id + "]";
}
}
dao层
FileDao.java
package com.cloud.dao;
import java.util.List;
import com.cloud.pojo.Files;
public interface FileDao {
/*
* 2019-2-23 csh
* 文件添加到数据库
*/
public void insertFile(Files files);
/* 2019-2-24 csh
* 查询所有文件名
*/
public List<Files> selectFileName(Integer user_id);
//查询images下的文件名
public List<Files> selectImagesFileName(Integer user_id);
//查询video下的文件名
public List<Files> selectVideoFileName(Integer user_id);
//查询txt下的文件名
public List<Files> selectTxtFileName(Integer user_id);
//查询music下的文件名
public List<Files> selectMusicFileName(Integer user_id);
//删除文件
public int deleteFile(Integer file_id);
//查询文件所在路径
public String selectFilePath(Integer file_id);
//修改文件路径
public int updateFilePath(Files files);
//文件查询
public List<Files> selectFileById(Integer file_id);
}
mapper
FileDaoMapper.xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloud.dao.FileDao">
<insert id="insertFile" parameterType="Files">
insert into cloud_file (file_name,file_path,user_id)
values(#{file_name},#{file_path},#{user_id})
insert>
<select id="selectFileName" resultType="Files">
select * from cloud_file where user_id=#{user_id}
select>
<select id="selectImagesFileName" resultType="Files">
select * from cloud_file
where file_path like '%images%' and user_id=#{user_id}
select>
<select id="selectVideoFileName" resultType="Files">
select * from cloud_file
where file_path like '%video%' and user_id=#{user_id}
select>
<select id="selectTxtFileName" resultType="Files">
select * from cloud_file
where file_path like '%txt%' and user_id=#{user_id}
select>
<select id="selectMusicFileName" resultType="Files">
select * from cloud_file
where file_path like '%music%' and user_id=#{user_id}
select>
<delete id="deleteFile" parameterType="Integer">
delete from cloud_file
where file_id=#{file_id}
delete>
<select id="selectFilePath" resultType="String">
select file_path from cloud_file
where file_id=#{file_id}
select>
<update id="updateFilePath" parameterType="Files">
update cloud_file set file_path=#{file_path}
where file_id=#{file_id}
update>
<select id="selectFileById" resultType="Files">
select * from cloud_file
where file_id=#{file_id}
select>
mapper>
service层
FileServiceImpl.java
package com.cloud.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cloud.dao.FileDao;
import com.cloud.pojo.Files;
import com.cloud.service.FileService;
@Service
public class FileServiceImpl implements FileService {
@Autowired
private FileDao fileDao;
@Override
public void insertFile(Files files) {
fileDao.insertFile(files);
}
@Override
public List<Files> selectFileName(Integer user_id) {
return fileDao.selectFileName(user_id);
}
@Override
public List<Files> selectImagesFileName(Integer user_id) {
List<Files> imagesFileName =fileDao.selectImagesFileName(user_id);
return imagesFileName;
}
@Override
public List<Files> selectVideoFileName(Integer user_id) {
List<Files> videoFileName = fileDao.selectVideoFileName(user_id);
return videoFileName;
}
@Override
public List<Files> selectTxtFileName(Integer user_id) {
List<Files> txtFileName = fileDao.selectTxtFileName(user_id);
return txtFileName;
}
@Override
public List<Files> selectMusicFileName(Integer user_id) {
List<Files> musicFileName = fileDao.selectMusicFileName(user_id);
return musicFileName;
}
@Override
public int deleteFile(Integer file_id) {
int i = fileDao.deleteFile(file_id);
return i;
}
@Override
public String selectFilePath(Integer file_id) {
String filePath = fileDao.selectFilePath(file_id);
return filePath;
}
@Override
public int updateFilePath(Files files) {
int i = fileDao.updateFilePath(files);
return i;
}
@Override
public List<Files> selectFileById(Integer file_id) {
List<Files> files = fileDao.selectFileById(file_id);
return files;
}
}
controller层
FolderController.java
package com.cloud.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.cloud.pojo.Files;
import com.cloud.pojo.Folder;
import com.cloud.pojo.User;
import com.cloud.service.FolderService;
@Controller
public class FolderController {
@Autowired
private FolderService folderService;
/*
* 2019/2/22
* 在用户目录下新建文件夹
*/
@RequestMapping("/createDir")
public String creatDir(Folder folder, User user,HttpServletRequest request,HttpServletResponse response) throws IOException {
String user_id = request.getParameter("user_id");
String folderName = request.getParameter("folderName");
String userPath = "D:\\cloud"+File.separator+user_id+File.separator+folderName;
File userdir=new File(userPath);
if(!userdir.exists()){//如果文件夹不存在
userdir.mkdirs();//创建文件夹
}
int userid = Integer.parseInt(user_id);
folder.setDir_name(folderName);
folder.setDir_path(userPath);
folder.setUser_id(userid);
folderService.addFolder(folder);
System.out.println(folderName+"创建成功");
response.setContentType("text/html; charset=utf-8");
PrintWriter out;
out = response.getWriter();
out.flush();
out.println("");
return "main";
}
/*
* 2019-2-26
* 文件夹展示
*/
@RequestMapping("/showFolder")
public String showFolder(Folder folder,Map<String,Object> map,@RequestParam("user_id") Integer user_id) {
List<Folder> folders = folderService.showFolder(user_id);
map.put("folders", folders);
return "showFolder";
}
/*
* 2019-2-27
* 文件夹内文件展示
*/
@RequestMapping("/showFiles")
public String showFiles(@RequestParam("dir_id") Integer dir_id,Map<String,Object> map) {
List<Files> files = folderService.selectFiles(dir_id);
map.put("files", files);
return "files";
}
/*
*
* 查询一个文件夹
*/
@RequestMapping("/selectOne/{dir_id}")
public String selectOne(@RequestParam("dir_id") Integer dir_id,Map<String,Object> map) {
Folder folder = folderService.selectOne(dir_id);
map.put("folder", folder);
return null;
}
/*
* 删除文件夹
*/
@RequestMapping("/deleteFolder/{dir_id}")
public String deleteFolder(Folder folder,@RequestParam("dir_id") Integer dir_id,HttpServletResponse response) throws IOException{
System.out.println(dir_id);
String dir_path = folderService.selectDirPath(dir_id);
try{
FileUtils.deleteDirectory(new File(dir_path));
System.out.println("ok");
}catch(IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
int i = folderService.deleteFolder(dir_id);
System.out.println("成功删除"+ i +"个文件夹");
response.setContentType("text/html; charset=utf-8");
PrintWriter out;
out = response.getWriter();
out.flush();
out.println("");
return "main";
}
}
觉得有用,记得一键三连哦!