基于C/S的网盘设计(Java)

开发 后端 项目管理
我希望有时间的其他朋友可以继续工作,虽然网络上有很多现成的网盘代码,不过还是希望自己能做一个,并借鉴一些优秀的思想来实现,下面说下实现过程,有些部分需要改进

由于有其他的工作,网盘做了一部分不得不放手了,我希望有时间的其他朋友可以继续工作,虽然网络上有很多现成的网盘代码,不过还是希望自己能做一个,并借鉴一些优秀的思想来实现,下面说下实现过程,有些部分需要改进

一、数据库的设计,目前只涉及到用户表,当然还有其他的,你可以根据需要来增加

用户表

  1. create table m_user(  
  2.  id int primary key auto_increment,  
  3.  name varchar(32) not null unique,  
  4.  password char(32) not null,  
  5.  `gender` enum('男','女'NOT NULL DEFAULT '男',  
  6.  phone varchar(20),  
  7.  email varchar(50) not null,  
  8.  reg_date char(16) not null,  
  9.  reg_ip varchar(15) not null,  
  10.  last_login_date char(16),  
  11.  last_login_ip varchar(15)  
  12. );  

二、数据源的设置,我这里使用c3p0数据源,当然你可以使用dbcp或者其他的

配置c3p0-config.xml文件就可以了,网络上有详细的配置项,或者在我源码里面下载,在最后公布下载地址

在这里我写一个简单的JdbcUtil,当然还可以编写一些复杂的操作,工作不允许我继续往下写了,你可以集成一些操作,就像hibernate那样

  1. public class JdbcUtil {  
  2.     /**  
  3.      * 数据库连接管理器  
  4.      */ 
  5. //    private static Logger log = Logger.getLogger(JdbcUtil.class);  
  6.     /*初始化数据库连接池*/ 
  7.     private static DataSource dataSource = new ComboPooledDataSource();  
  8.       
  9.     /*获取数据源*/ 
  10.     public DataSource getDataSource(){  
  11.         return dataSource;  
  12.     }  
  13.       
  14.     /*获取连接*/ 
  15.     public static Connection getConnection() throws SQLException{  
  16.         return dataSource.getConnection();  
  17.     }  
  18.       
  19.     /*释放连接*/ 
  20.     public static void free(ResultSet rs,PreparedStatement ps,Connection conn){  
  21.         if(null != rs){  
  22.             try {  
  23.                 rs.close();  
  24.             } catch (SQLException e) {}  
  25.         }  
  26.         if(null != ps){  
  27.             try {  
  28.                 ps.close();  
  29.             } catch (SQLException e) {}  
  30.         }  
  31.         if(null != conn){  
  32.             try {  
  33.                 conn.close();  
  34.             } catch (SQLException e) {}  
  35.         }  
  36.     }  
  37.     public static void free(PreparedStatement ps,Connection conn){  
  38.         if(null != ps){  
  39.             try {  
  40.                 ps.close();  
  41.             } catch (SQLException e) {}  
  42.         }  
  43.         if(null != conn){  
  44.             try {  
  45.                 conn.close();  
  46.             } catch (SQLException e) {}  
  47.         }  
  48.     }  

#p#

三、我这里先说说服务端

1.socket线程池

池的作用想必大家都知道,循环利用资源,我这里的这个池只是简单的池,没有时间再完成一个复杂的工作了

cn.mike.server.ServerThread是一个负责处理用户请求的线程,我们要创建一批这样的线程,并由cn.mike.server.ServerThreadPool管理,代码如下:

  1. public class ServerThreadPool {  
  2.     /**  
  3.      * 服务端线程池  
  4.      */ 
  5.     private final static Logger log = Logger.getLogger(ServerThreadPool.class);  
  6.     //线程组  
  7.     public static LinkedList<ServerThread> threadPool = new LinkedList<ServerThread>();  
  8.     private static int maxPoolSize;//最大连接数  
  9.     private static int minPoolSize;//最小连接数  
  10.     private static int initialPoolSize;//初始化连接数  
  11.     private static int maxIdleTime;//连接的最大空闲时间,单位:秒  
  12.     private static int acquireIncrement;//在当前连接数耗尽的时候,一次获取的新的连接数  
  13.     static int maxWaitUserTime;//线程等待用户操作的最大时间,到达最大时间未传送数据,则进行线程释放  
  14.       
  15.     public ServerThreadPool(){  
  16.         initProperties();  
  17.         initThreadPool();  
  18.     }  
  19.       
  20.     /*  
  21.      * 初始化配置  
  22.      */ 
  23.     public void initProperties(){  
  24.         System.out.println("正在启动线程池...");  
  25.         System.out.println("正在加载线程池配置文件...");  
  26.         Properties pro = new Properties();  
  27.         HashMap<String, String> propertiesMap = new HashMap<String, String>();  
  28.         try {  
  29.             pro.load(ServerThreadPool.class.getClassLoader().getResourceAsStream(ServerThreadPoolConfig.PROPS_FILE_RSRC_PATH));  
  30.             propertiesMap.put(ServerThreadPoolConfig.MAX_POOL_SIZE, pro.getProperty(ServerThreadPoolConfig.MAX_POOL_SIZE));  
  31.             propertiesMap.put(ServerThreadPoolConfig.MIN_POOL_SIZE, pro.getProperty(ServerThreadPoolConfig.MIN_POOL_SIZE));  
  32.             propertiesMap.put(ServerThreadPoolConfig.INITIAL_POOL_SIZE, pro.getProperty(ServerThreadPoolConfig.INITIAL_POOL_SIZE));  
  33.             propertiesMap.put(ServerThreadPoolConfig.MAX_IDLE_TIME, pro.getProperty(ServerThreadPoolConfig.MAX_IDLE_TIME));  
  34.             propertiesMap.put(ServerThreadPoolConfig.ACQUIRE_INCREMENT, pro.getProperty(ServerThreadPoolConfig.ACQUIRE_INCREMENT));  
  35.             propertiesMap.put(ServerThreadPoolConfig.MAX_WAIT_USER_TIME, pro.getProperty(ServerThreadPoolConfig.MAX_WAIT_USER_TIME));  
  36.             if(null != propertiesMap.get(ServerThreadPoolConfig.MAX_POOL_SIZE)){  
  37.                 ServerThreadPool.maxPoolSize = Integer.parseInt(propertiesMap.get(ServerThreadPoolConfig.MAX_POOL_SIZE));  
  38.             }else{  
  39.                 ServerThreadPool.maxPoolSize = 100;  
  40.             }  
  41.               
  42.             if(null != propertiesMap.get(ServerThreadPoolConfig.MIN_POOL_SIZE)){  
  43.                 ServerThreadPool.minPoolSize = Integer.parseInt(propertiesMap.get(ServerThreadPoolConfig.MIN_POOL_SIZE));  
  44.             }else{  
  45.                 ServerThreadPool.minPoolSize = 5;  
  46.             }  
  47.               
  48.             if(null != propertiesMap.get(ServerThreadPoolConfig.INITIAL_POOL_SIZE)){  
  49.                 ServerThreadPool.initialPoolSize = Integer.parseInt(propertiesMap.get(ServerThreadPoolConfig.INITIAL_POOL_SIZE));  
  50.             }else{  
  51.                 ServerThreadPool.initialPoolSize = 5;  
  52.             }  
  53.               
  54.             if(null != propertiesMap.get(ServerThreadPoolConfig.MAX_IDLE_TIME)){  
  55.                 ServerThreadPool.maxIdleTime = Integer.parseInt(propertiesMap.get(ServerThreadPoolConfig.MAX_IDLE_TIME));  
  56.             }else{  
  57.                 ServerThreadPool.maxIdleTime = 10;  
  58.             }  
  59.               
  60.             if(null != propertiesMap.get(ServerThreadPoolConfig.ACQUIRE_INCREMENT)){  
  61.                 ServerThreadPool.acquireIncrement = Integer.parseInt(propertiesMap.get(ServerThreadPoolConfig.ACQUIRE_INCREMENT));  
  62.             }else{  
  63.                 ServerThreadPool.acquireIncrement = 1;  
  64.             }  
  65.             if(null != propertiesMap.get(ServerThreadPoolConfig.MAX_WAIT_USER_TIME)){  
  66.                 ServerThreadPool.maxWaitUserTime = Integer.parseInt(propertiesMap.get(ServerThreadPoolConfig.MAX_WAIT_USER_TIME));  
  67.             }else{  
  68.                 ServerThreadPool.maxWaitUserTime = 60000;  
  69.             }  
  70.               
  71.         } catch (Exception e) {  
  72.             log.error("线程池配置文件加载出错,请确保文件threadPool.properties存在,并正确配置!");  
  73.             System.exit(1);  
  74.         }  
  75.         System.out.println("线程池配置加载成功,配置信息如下:");  
  76.         System.out.println("#################################");  
  77.         System.out.println("最大连接数:"+ServerThreadPool.maxPoolSize);  
  78.         System.out.println("最小连接数:"+ServerThreadPool.minPoolSize);  
  79.         System.out.println("初始化连接数:"+ServerThreadPool.initialPoolSize);  
  80.         System.out.println("连接的最大空闲时间:"+ServerThreadPool.maxIdleTime+" 秒");  
  81.         System.out.println("在当前连接数耗尽的时候,一次获取的新的连接数:"+ServerThreadPool.acquireIncrement);  
  82.         System.out.println("线程等待用户操作的最大时间:"+ServerThreadPool.maxWaitUserTime+" 毫秒");  
  83.         System.out.println("#################################");  
  84.     }  
  85.     /*  
  86.      * 初始化服务线程  
  87.      */ 
  88.     public void initThreadPool(){  
  89.         for(int i=0;i<ServerThreadPool.initialPoolSize;i++){  
  90.             ServerThread st = new ServerThread();  
  91.             st.start();  
  92.             threadPool.add(st);  
  93.         }  
  94.     }  
  95.       
  96.     /*  
  97.      * 线程池动态调整器  
  98.      */ 
  99.     public void poolAdjust(){  
  100.           
  101.     }  

一些配置规范我把它放在cn.mike.server.ServerThreadPoolConfig里

  1. public class ServerThreadPoolConfig {  
  2.     /*  
  3.      * 线程池配置  
  4.      */ 
  5.       
  6.     //配置文件路径  
  7.     public final static String PROPS_FILE_RSRC_PATH     = "threadPool.properties";  
  8.     //最大连接数  
  9.     public final static String MAX_POOL_SIZE = "maxPoolSize";  
  10.     //最小连接数  
  11.     public final static String MIN_POOL_SIZE = "minPoolSize";  
  12.     //初始化连接数  
  13.     public final static String INITIAL_POOL_SIZE= "initialPoolSize";  
  14.     //连接的最大空闲时间,单位:秒  
  15.     public final static String MAX_IDLE_TIME = "maxIdleTime";  
  16.     //在当前连接数耗尽的时候,一次获取的新的连接数  
  17.     public final static String ACQUIRE_INCREMENT = "acquireIncrement";  
  18.     //线程等待用户操作的最大时间,到达最大时间未传送数据,则进行线程释放  
  19.     public final static String MAX_WAIT_USER_TIME = "maxWaitUserTime";  
  20.       

threadPool.properties文件用于配置线程池的一些选项,我这里的设置可能不够完整,你可以根据需要增加

还有一个重要问题,这里需要开启一个线程来管理线程池里线程的数量,实现动态调整,这工作我并没完成,希望你能把它完成

cn.mike.server.Server是一个用于接收用户请求的分配器,处理一些初始化工作

  1. public class Server {  
  2.     /**  
  3.      * 25米 网盘 服务端  
  4.      */ 
  5.     private final static Logger log = Logger.getLogger(Server.class);  
  6.     private int listenPort = 8594;//监听端口  
  7.     private static ServerSocket ss;  
  8.     static LinkedList<Socket> taskQueue = new LinkedList<Socket>();//任务队列  
  9.     public static Map<String,Session> sessionMap = new HashMap<String,Session>();  
  10.     /*初始化线程池*/ 
  11.     ServerThreadPool stp = new ServerThreadPool();  
  12.     /*数据库连接工具*/ 
  13.     public JdbcUtil jdbcUtil = new JdbcUtil();  
  14.     private int maxWaitUserTime = ServerThreadPool.maxWaitUserTime;//最大等待操作时间  
  15.       
  16.     public static void main(String[] args) {  
  17.         System.out.println("正在启动服务器...");  
  18.         Server server = new Server();  
  19.         new TaskHandle().start();  
  20.         server.init();  
  21.     }  
  22.     public void init(){  
  23. //        初始化数据库连接池  
  24.         System.out.println("正在初始化数据库连接池...");  
  25.         try {  
  26.             System.out.println("#################################");  
  27.             JdbcUtil.getConnection().close();  
  28.             System.out.println("#################################");  
  29.             System.out.println("数据库连接池创建成功!");  
  30.         } catch (SQLException e1) {  
  31.             log.error("数据库连接池创建失败!");  
  32.             System.exit(1);  
  33.         }  
  34.           
  35.         /*开启监听服务*/ 
  36.         try {  
  37.             ss = new ServerSocket(listenPort);  
  38.             System.out.println("服务器启动成功,正在监听端口:"+listenPort);  
  39.             while(true){  
  40.                 Socket socket = ss.accept();  
  41.                 socket.setSoTimeout(maxWaitUserTime);//设置最大连接时长  
  42.                 System.out.println("发现客户端连接,IP:"+socket.getRemoteSocketAddress());  
  43.                 process(socket);//转入线程池处理  
  44.             }  
  45.         } catch (IOException e) {  
  46.             System.out.println("服务器启动失败,请确保端口:"+listenPort+"不被其他程序占用!");  
  47.         }  
  48.     }  
  49.       
  50.     /*  
  51.      * 服务线程处理客户端请求  
  52.      */ 
  53.     public void process(Socket socket){  
  54.         if(ServerThreadPool.threadPool.size()>0){//如果池中还有服务线程  
  55.             ServerThreadPool.threadPool.removeFirst().startWork(socket);  
  56.         }  
  57.         else if(taskQueue.size()<1000){//若没有,并且队列长度小于1000,则加入任务队列  
  58.             taskQueue.add(socket);  
  59.         }  
  60.         else{  
  61.             try {  
  62.                 socket.close();  
  63.             } catch (IOException e) {  
  64.                 log.error("关闭客户端socket失败!");  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69. }  
  70.  
  71. /*  
  72.  *开启定时器,处理任务队列,每隔 500 毫秒查看有没有空闲连接  
  73.  */ 
  74. class TaskHandle extends Thread{  
  75.     static LinkedList<Socket> taskQueue =  Server.taskQueue;  
  76.     public TaskHandle(){  
  77.         System.out.println("队列任务处理器开启..");  
  78.     }  
  79.     public void run() {  
  80.         try {  
  81.             while(true){  
  82.                 Thread.sleep(500);  
  83.                 if(taskQueue.size()>0 && ServerThreadPool.threadPool.size()>0){//如果池中还有服务线程,则处理任务队列  
  84.                     ServerThreadPool.threadPool.removeFirst().startWork(Server.taskQueue.removeFirst());  
  85.                 }  
  86.             }  
  87.         } catch (InterruptedException e) {  
  88.             e.printStackTrace();  
  89.         }  
  90.     }  
  91.       

可能有些东西读者不是很明白,这需要对socket编程熟悉点,当然我的设计也有些问题,希望读者能提改进意见,有时间就修改

服务端启动如下:

四、下面说说客户端

客户端就是登陆界面和管理网盘界面

1.登陆界面:cn.mike.client.ClientLogin

不是很漂亮,勉强用着吧,当然你可以设计得漂亮点

登陆成功后是这样的:

大概做到这里了,数据的上传下载等没时间实现了,感兴趣的朋友可以继续我的工作

源码下载

原文链接:http://www.cnblogs.com/mikevictor07/archive/2012/08/21/2648716.html

【编辑推荐】

  1. Java项目经验——程序员成长的关键
  2. Java 8 Lambda:模拟Mixin实现类多重继承
  3. 为什么Java程序占用的内存比实际分配的多
  4. 11项针对轻量级高效同行代码评审
责任编辑:张伟 来源: mikevictor的博客
相关推荐

2017-01-04 09:47:38

联想企业网盘

2016-10-19 11:00:26

2013-07-18 14:36:21

2015-07-14 10:46:20

网盘

2011-12-08 17:53:56

DBank网盘华为云存储

2015-07-31 16:14:24

联想

2015-07-31 16:33:32

联想

2012-02-29 12:33:14

新浪微盘网盘

2019-12-20 15:13:57

网络硬盘软件磁盘

2022-03-18 08:46:08

vivo官网APP首页改版

2012-08-13 10:22:30

网盘战争

2023-03-15 16:30:19

2022-01-21 09:25:33

5G千兆光网网盘限速

2009-06-14 22:09:24

Java界面布局DSL

2011-07-25 13:16:23

无线局域网扩频通信

2023-12-01 08:09:08

2016-05-04 11:00:07

DBank华为

2011-09-15 19:27:41

windows7分区

2015-11-04 10:12:40

导航设计任务腾讯

2017-09-04 08:59:14

点赞
收藏

51CTO技术栈公众号