搭建Java共享网盘

项目介绍在线共享网盘采用jsp+servlet搭建项目结构实现共享网盘,项目分为管理员,普通用户和付费用户三种角色,根据不同角色控制不同权限,实现不同用户对个人文件文件,所有文件,共享文件的增删改查操作 。
项目适用人群正在做毕设的学生,或者需要项目实战练习的JAVA学习者
开发环境:

  1. jdk 8
  2. intellij idea
  3. Tomcat 8.5.40
  4. MySQL 5.7
所用技术:
  1. jsp+servlet
  2. js+ajax
  3. layUi
  4. jdbc直连
项目访问地址http://localhost:8090项目结构
搭建Java共享网盘

文章插图
 
项目截图
  • 注册

搭建Java共享网盘

文章插图
 
  • 我的网盘

搭建Java共享网盘

文章插图
 
  • 我的共享

搭建Java共享网盘

文章插图
 
  • 回收站

搭建Java共享网盘

文章插图
 
  • 会员充值

搭建Java共享网盘

文章插图
 
  • 管理员-所有文件

搭建Java共享网盘

文章插图
 
  • 管理员-共享申请

搭建Java共享网盘

文章插图
 
关键代码:1.初始化工作
//数据库连接初始化public class DBInfo {String url = null;String username = null;String password = null;String driverClass = null;private static DBInfo db = new DBInfo();public static DBInfo getInstance(){return db;}private DBInfo() {InputStream in = this.getClass().getClassLoader().getResourceAsStream("db.properties");Properties pp = new Properties();try {pp.load(in);url = pp.getProperty("jdbc.url");username = pp.getProperty("jdbc.username");password = pp.getProperty("jdbc.password");driverClass = pp.getProperty("jdbc.driver");Class.forName(driverClass);} catch (Exception e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}}public Connection getConnection(){Connection conn = null;try {conn = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();}return conn;}}//上传资源初始化public void init() throws ServletException {super.init();//servlet启动时,读取配置文件中关于上传的信息InputStream in = this.getClass().getClassLoader().getResourceAsStream("ini.properties");Properties pp = new Properties();try {pp.load(in);UPLOAD_ROOT_PATH = pp.getProperty("upload.path");String tmpPath = pp.getProperty("tmp.path");//配置上传临时目录factory = new DiskFileItemFactory(1024*1024*10,new File(tmpPath));stu = new ServletFileUpload(factory);} catch (Exception e) {e.printStackTrace();}finally{try {in.close();} catch (IOException e) {e.printStackTrace();}}}2.资源上传
//前端JSP代码<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadFile" aria-hidden="true"><form action="upload" method="post" enctype="multipart/form-data"><input type="hidden" name="from" value=https://www.isolves.com/it/cxkf/yy/JAVA/2020-12-31/"user">//后端入库处理protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsIOException {User user = (User) request.getSession().getAttribute(Const.SESSION_USER);String from="";try {List fileItemLists = stu.parseRequest(request);for(FileItem fileItem : fileItemLists){if(fileItem.isFormField()){from = fileItem.getString();}else{//上传文件名String fileName = fileItem.getName();String oldfilename = fileItem.getName();int index = fileName.lastIndexOf("\");if(index != -1) {fileName = fileName.substring(index+1);}String root = UPLOAD_ROOT_PATH+user.getUsername();//获取文件大小long size = fileItem.getSize();String sizeString = StringUtil.computeSize(size);Timestamp upTime = new Timestamp(new Date().getTime());File file = new File(root,fileName);//解决文件同名int cnt = 1;while(file.exists()){StringBuffer sb = new StringBuffer(fileName);sb.insert(sb.lastIndexOf("."), "("+cnt+")");file = new File(root,sb.toString());cnt++;}//文件路径是否存在if(!file.getParentFile().exists()){file.getParentFile().mkdirs();}try {fileItem.write(file);//上传成功,数据库保存记录UserFile userFile = new UserFile();userFile.setCreateTime(upTime);userFile.setFilename(file.getName());userFile.setFilename(file.getName());userFile.setFileSize(sizeString);userFile.setIsShared(0);userFile.setOwnerId(user.getId());userFile.setPath(file.getAbsolutePath());userFile.setOldfilename(oldfilename);userFileDao.save(userFile);response.sendRedirect(from+"?action=mydisk");} catch (Exception e) {e.printStackTrace();response.getWriter().print("上传出错");}}}} catch (FileUploadException e) {e.printStackTrace();response.setContentType("text/html; charset=utf8");response.getWriter().print("上传出错!!");}}


推荐阅读