自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于C/S的網(wǎng)盤設計(Java)

開發(fā) 后端 項目管理
我希望有時間的其他朋友可以繼續(xù)工作,雖然網(wǎng)絡上有很多現(xiàn)成的網(wǎng)盤代碼,不過還是希望自己能做一個,并借鑒一些優(yōu)秀的思想來實現(xiàn),下面說下實現(xiàn)過程,有些部分需要改進

由于有其他的工作,網(wǎng)盤做了一部分不得不放手了,我希望有時間的其他朋友可以繼續(xù)工作,雖然網(wǎng)絡上有很多現(xiàn)成的網(wǎng)盤代碼,不過還是希望自己能做一個,并借鑒一些優(yōu)秀的思想來實現(xiàn),下面說下實現(xiàn)過程,有些部分需要改進

一、數(shù)據(jù)庫的設計,目前只涉及到用戶表,當然還有其他的,你可以根據(jù)需要來增加

用戶表

  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. );  

二、數(shù)據(jù)源的設置,我這里使用c3p0數(shù)據(jù)源,當然你可以使用dbcp或者其他的

配置c3p0-config.xml文件就可以了,網(wǎng)絡上有詳細的配置項,或者在我源碼里面下載,在最后公布下載地址

在這里我寫一個簡單的JdbcUtil,當然還可以編寫一些復雜的操作,工作不允許我繼續(xù)往下寫了,你可以集成一些操作,就像hibernate那樣

  1. public class JdbcUtil {  
  2.     /**  
  3.      * 數(shù)據(jù)庫連接管理器  
  4.      */ 
  5. //    private static Logger log = Logger.getLogger(JdbcUtil.class);  
  6.     /*初始化數(shù)據(jù)庫連接池*/ 
  7.     private static DataSource dataSource = new ComboPooledDataSource();  
  8.       
  9.     /*獲取數(shù)據(jù)源*/ 
  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線程池

池的作用想必大家都知道,循環(huán)利用資源,我這里的這個池只是簡單的池,沒有時間再完成一個復雜的工作了

cn.mike.server.ServerThread是一個負責處理用戶請求的線程,我們要創(chuàng)建一批這樣的線程,并由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;//最大連接數(shù)  
  9.     private static int minPoolSize;//最小連接數(shù)  
  10.     private static int initialPoolSize;//初始化連接數(shù)  
  11.     private static int maxIdleTime;//連接的最大空閑時間,單位:秒  
  12.     private static int acquireIncrement;//在當前連接數(shù)耗盡的時候,一次獲取的新的連接數(shù)  
  13.     static int maxWaitUserTime;//線程等待用戶操作的最大時間,到達最大時間未傳送數(shù)據(jù),則進行線程釋放  
  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("最大連接數(shù):"+ServerThreadPool.maxPoolSize);  
  78.         System.out.println("最小連接數(shù):"+ServerThreadPool.minPoolSize);  
  79.         System.out.println("初始化連接數(shù):"+ServerThreadPool.initialPoolSize);  
  80.         System.out.println("連接的最大空閑時間:"+ServerThreadPool.maxIdleTime+" 秒");  
  81.         System.out.println("在當前連接數(shù)耗盡的時候,一次獲取的新的連接數(shù):"+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.      * 線程池動態(tài)調(diào)整器  
  98.      */ 
  99.     public void poolAdjust(){  
  100.           
  101.     }  

一些配置規(guī)范我把它放在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.     //最大連接數(shù)  
  9.     public final static String MAX_POOL_SIZE = "maxPoolSize";  
  10.     //最小連接數(shù)  
  11.     public final static String MIN_POOL_SIZE = "minPoolSize";  
  12.     //初始化連接數(shù)  
  13.     public final static String INITIAL_POOL_SIZE= "initialPoolSize";  
  14.     //連接的最大空閑時間,單位:秒  
  15.     public final static String MAX_IDLE_TIME = "maxIdleTime";  
  16.     //在當前連接數(shù)耗盡的時候,一次獲取的新的連接數(shù)  
  17.     public final static String ACQUIRE_INCREMENT = "acquireIncrement";  
  18.     //線程等待用戶操作的最大時間,到達最大時間未傳送數(shù)據(jù),則進行線程釋放  
  19.     public final static String MAX_WAIT_USER_TIME = "maxWaitUserTime";  
  20.       

threadPool.properties文件用于配置線程池的一些選項,我這里的設置可能不夠完整,你可以根據(jù)需要增加

還有一個重要問題,這里需要開啟一個線程來管理線程池里線程的數(shù)量,實現(xiàn)動態(tài)調(diào)整,這工作我并沒完成,希望你能把它完成

cn.mike.server.Server是一個用于接收用戶請求的分配器,處理一些初始化工作

  1. public class Server {  
  2.     /**  
  3.      * 25米 網(wǎng)盤 服務端  
  4.      */ 
  5.     private final static Logger log = Logger.getLogger(Server.class);  
  6.     private int listenPort = 8594;//監(jiān)聽端口  
  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.     /*數(shù)據(jù)庫連接工具*/ 
  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. //        初始化數(shù)據(jù)庫連接池  
  24.         System.out.println("正在初始化數(shù)據(jù)庫連接池...");  
  25.         try {  
  26.             System.out.println("#################################");  
  27.             JdbcUtil.getConnection().close();  
  28.             System.out.println("#################################");  
  29.             System.out.println("數(shù)據(jù)庫連接池創(chuàng)建成功!");  
  30.         } catch (SQLException e1) {  
  31.             log.error("數(shù)據(jù)庫連接池創(chuàng)建失??!");  
  32.             System.exit(1);  
  33.         }  
  34.           
  35.         /*開啟監(jiān)聽服務*/ 
  36.         try {  
  37.             ss = new ServerSocket(listenPort);  
  38.             System.out.println("服務器啟動成功,正在監(jiān)聽端口:"+listenPort);  
  39.             while(true){  
  40.                 Socket socket = ss.accept();  
  41.                 socket.setSoTimeout(maxWaitUserTime);//設置最大連接時長  
  42.                 System.out.println("發(fā)現(xiàn)客戶端連接,IP:"+socket.getRemoteSocketAddress());  
  43.                 process(socket);//轉(zhuǎn)入線程池處理  
  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("關(guān)閉客戶端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編程熟悉點,當然我的設計也有些問題,希望讀者能提改進意見,有時間就修改

服務端啟動如下:

四、下面說說客戶端

客戶端就是登陸界面和管理網(wǎng)盤界面

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

不是很漂亮,勉強用著吧,當然你可以設計得漂亮點

登陸成功后是這樣的:

大概做到這里了,數(shù)據(jù)的上傳下載等沒時間實現(xiàn)了,感興趣的朋友可以繼續(xù)我的工作

源碼下載

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

【編輯推薦】

  1. Java項目經(jīng)驗——程序員成長的關(guān)鍵
  2. Java 8 Lambda:模擬Mixin實現(xiàn)類多重繼承
  3. 為什么Java程序占用的內(nèi)存比實際分配的多
  4. 11項針對輕量級高效同行代碼評審
責任編輯:張偉 來源: mikevictor的博客
相關(guān)推薦

2017-01-04 09:47:38

聯(lián)想企業(yè)網(wǎng)盤

2016-10-19 11:00:26

2013-07-18 14:36:21

2015-07-14 10:46:20

網(wǎng)盤

2011-12-08 17:53:56

DBank網(wǎng)盤華為云存儲

2015-07-31 16:33:32

聯(lián)想

2015-07-31 16:14:24

聯(lián)想

2012-02-29 12:33:14

新浪微盤網(wǎng)盤

2019-12-20 15:13:57

網(wǎng)絡硬盤軟件磁盤

2022-03-18 08:46:08

vivo官網(wǎng)APP首頁改版

2022-01-21 09:25:33

5G千兆光網(wǎng)網(wǎng)盤限速

2023-03-15 16:30:19

2012-08-13 10:22:30

網(wǎng)盤戰(zhàn)爭

2011-07-25 13:16:23

無線局域網(wǎng)擴頻通信

2015-11-04 10:12:40

導航設計任務騰訊

2009-06-14 22:09:24

Java界面布局DSL

2017-09-04 08:59:14

2013-10-28 15:07:30

Windows 8C 盤

2023-12-01 08:09:08

2011-09-15 19:27:41

windows7分區(qū)
點贊
收藏

51CTO技術(shù)棧公眾號