手把手搭建Java共享网盘的方法步骤

这篇文章主要介绍了手把手搭建Java共享网盘,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

项目介绍

在线共享网盘采用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代码 

3.检索重复上传的资源

 //这里上传在上面上传资源时候,将保存原始资源名字 public List findRetrieveListByOwnerId(int ownerId,int isDelete){ List fileList = 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代码  
<%@include file="common/nav.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版本后续迭代更新,持续关注

到此这篇关于手把手搭建Java共享网盘的方法步骤的文章就介绍到这了,更多相关Java搭建共享网盘内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

以上就是手把手搭建Java共享网盘的方法步骤的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java