手把手搭建Java共享网盘
在线共享网盘采用jsp+servlet搭建项目结构实现共享网盘,项目分为管理员,普通用户和付费用户三种角色,根据不同角色控制不同权限,实现不同用户对个人文件文件,所有文件,共享文件的增删改查操作。
项目介绍
在线共享网盘采用jsp+servlet搭建项目结构实现共享网盘,项目分为管理员,普通用户和付费用户三种角色,根据不同角色控制不同权限,实现不同用户对个人文件文件,所有文件,共享文件的增删改查操作。
项目适用人群
正在做毕设的学生,或者需要项目实战练习的Java学习者
开发环境:
- jdk 8
- intellij idea
- tomcat 8.5.40
- mysql 5.7
所用技术:
- jsp+servlet
- js+ajax
- layUi
- jdbc直连
项目访问地址
http://localhost:8090
项目结构
项目截图
- 注册
- 我的网盘
我的共享
回收站
会员充值
管理员-所有文件
管理员-共享申请
关键代码:
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代码×上传文件关闭//后端入库处理 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { User user = (User) request.getSession().getAttribute(Const.SESSION_USER); String from=""; try { ListfileItemLists = 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("上传出错!!"); } }
3.检索重复上传的资源
//这里上传在上面上传资源时候,将保存原始资源名字 public ListfindRetrieveListByOwnerId(int ownerId,int isDelete){ ListfileList = new ArrayList(); Connection conn = db.getConnection(); PreparedStatement ps = null; ResultSet rs = null; UserFile userFile = null; String sql="select * from file where oldfilename in ( " + " select a.oldfilename from (select oldfilename,count(id) counts from file GROUP BY oldfilename HAVING counts>1) a" + " ) and ownerid=? and isDelete=?"; ps = conn.prepareStatement(sql); ps.setInt(1, ownerId); ps.setInt(2, isDelete); rs = ps.executeQuery(); while(rs.next()){ userFile = new UserFile(); userFile.setId(rs.getInt(1)); userFile.setFilename(rs.getString(2)); userFile.setPath(rs.getString(3)); userFile.setCreateTime(rs.getTimestamp(4)); userFile.setIsShared(rs.getInt(5)); userFile.setOwnerId(rs.getInt(6)); userFile.setFileSize(rs.getString(7)); userFile.setCounts(rs.getInt(8)); userFile.setSharedReason(rs.getString("SharedReason")); userFile.setSharedTime(rs.getString("SharedTime")); fileList.add(userFile); } return fileList; }
4.平台会员充值
//前端jsp代码 以下是微信付款码,扫码即可支付${msgSuccess } ${msgFail } //js实现,采用定时跳转模拟真实用户支付流程,后续进行改动用户会员状态 var test1 = setTimeout(function(){ $("#div1").css("display","none"); $("#div2").css("display","block"); layer.msg('恭喜您,完成扫码支付!', {time: 4000, icon:6},function () { window.location.href="user?action=doTopUp"; }); clearTimeout(test1); },5000); //后端代码 public void doTopUp(User user) { Connection conn = db.getConnection(); PreparedStatement ps = null; ResultSet rs = null; try { //members=1为会员状态 ps = conn.prepareStatement("update user set members = 1 where id = ?"); ps.setInt(1, user.getId()); ps.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (ps != null) ps.close(); if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }
项目后续
其他ssh,ssm,springboot版本后续迭代更新,持续关注

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
jsp--MVC案例
三层优化 1.加入接口 建议面向接口开发:先接口-再实现类 –service、dao加入接口 –接口与实现类的命名规范 接口:interface, 起名 I实体类Service IStudentService IStudentDao 实现类:implements 起名 实体类ServiceImpl StudentServiceImpl StudentDaoImpl 接口: I实体类层所在包名 IStudentService、IStudentDao 接口所在的包: xxx.service xx.dao 实现类: 实体类层所在包名Impl StudentServiceImpl、StudentDaoImpl 实现类所在的包:xxx.service.impl xx.dao.impl 以后使用接口/实现类时,推荐写法: 接口 x = new 实现类(); IStudentDao studentDao = new StudentDaoImpl(); 案例:实现用户登录验证 login.jsp <%@ page language="java" contentType="text/html; c...
- 下一篇
【DB宝44】Oracle rac集群中的IP类型简介
Oracle rac集群中的IP类型简介 在Oracle RAC中,Public IP、Private IP、Virtual IP、SCAN IP、GNS VIP及HAIP的作用分别是什么? 从Oracle 11g开始,安装RAC至少需要7个IP地址,两块网卡(一块公网网卡,一块私网网卡),其中public、vip和scan都在同一个网段,使用的是公网网卡,private在另一个网段,使用的是私网网卡。主机名不能包含下横线,如:RAC_01是不允许的。通过执行ifconfig -a检查两个节点的网络设备名称是否一致。另外,在配置了/etc/hosts文件后,在安装RAC之前,公网、私网共4个IP可以ping通,其它3个IP不能ping通才是正常的。 从Oracle 18c开始,scan建议至少为3个。 在安装RAC时,其IP地址的规划类似于下表所示: 其在/etc/hosts文件中的配置如下所示: [root@raclhr-19c-n1~]#more/etc/hosts 127.0.0.1localhostlocalhost.localdomainlocalhost4localhost...
相关文章
文章评论
共有0条评论来说两句吧...