交作業(yè)了!哈哈-·
package?uploaddemo; import?java.io.IOException; public?class?Main?{?//本程序有注冊,登錄上傳功能,按提示操作 ???//服務器main public?static?int?mm; public?static?void?main(String[]?args)?throws?IOException?{ //?TODO?Auto-generated?method?stub String[]?m?=?null; int?w?=?0; while?(true)?{ switch?(mm)?{ case?0: m?=?new?ReceiveServer().receive(); break; case?1: w?=?new?Control().controls(m);//?mm=1,進入分析控制端 break; case?2://?發(fā)送//mm=2,進入發(fā)送確認端口 case?3: new?ReceiveServer().send(w); break; case?4: //?調用上傳 w?=?new?ReceiveServer().updat(); break; } } } }
package?uploaddemo; import?java.io.BufferedReader; import?java.io.FileOutputStream; import?java.io.IOException; import?java.io.InputStreamReader; import?java.io.PrintWriter; import?java.net.ServerSocket; import?java.net.Socket; import?java.io.InputStream; //服務器接受發(fā)送端 public?class?ReceiveServer?{ //?receive()服務器接受端 public?String[]?receive()?{ String[]?w?=?new?String[3]; while?(true)?{ ServerSocket?net0?=?null; try?{ net0?=?new?ServerSocket(9999); Socket?net?=?net0.accept(); BufferedReader?n?=?new?BufferedReader(new?InputStreamReader( net.getInputStream())); String?bb?=?null; int?m?=?0; while?((bb?=?n.readLine())?!=?null)?{ w[m]?=?bb; m++;//?m[]分別讀取發(fā)送過來的前3位 } }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); Main.mm=0; }?finally?{ if?(net0?!=?null) try?{ net0.close(); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } if?(w[0]?==null)?{ Main.mm?=?0; }else{ Main.mm=1;} return?w; } } //?發(fā)送端?確認信息? public?void?send(int?a)?{ PrintWriter?pnet?=?null; try?{ Socket?net?=?new?Socket("localhost",?8888); pnet?=?new?PrintWriter(net.getOutputStream()); pnet.println(a); pnet.flush(); }?catch?(IOException?e)?{ Main.mm=0; //?TODO?Auto-generated?catch?block }?finally?{ if?(pnet?!=?null) pnet.close(); if?(Main.mm?==?1)?{ Main.mm?=?0; } if?(Main.mm?==?3)?{ if(a==1){ Main.mm?=?4;????//失敗了就重新開始 }else{ Main.mm?=?0; } } } } //?上傳接收端 public?int?updat()?throws?IOException?{ ReceiveServer?rec?=?new?ReceiveServer();//?先接受文件名字且a[1]方的是文件名字 String[]?a?=?rec.receive(); String?cc?=?a[2]; String?bb?=?"C:\\Documents?and?Settings\\Administrator\\桌面\\sjk\\"?+?cc; //?TODO?Auto-generated?method?stub ServerSocket?net?=?null; FileOutputStream?ft?=?null; int?b?=?-1; try?{ net?=?new?ServerSocket(7777); Socket?nett?=?net.accept(); InputStream?nettt?=?nett.getInputStream(); ft?=?new?FileOutputStream(bb); while?((b?=?nettt.read())?!=?-1)?{ ft.write(b); ft.flush(); } }?catch?(Exception?e)?{ //?TODO?Auto-generated?catch?block Main.mm=0; e.printStackTrace(); net.close(); }?finally?{ if?(net?!=?null) net.close(); if?(ft?!=?null) ft.close(); if?(b!=-1){ new?model().shangC(a[1],?bb); return?1; } } return?0; } }
package?uploaddemo; import?java.io.IOException; //分析?控制?返回0失敗 public?class?Control?{ public?int?controls(String[]?m)?throws?IOException?{ int?n?=?Integer.valueOf(m[0]); int?w?=?0; switch?(n)?{ case?1://?m[0]為1時注冊返回確認w w?=?new?model().zhuC(m[1],?m[2]); Main.mm?=?2; break; case?2://?m[0]為2時登錄返回確認w?,且進入上傳接受端 w?=?new?model().dengL(m[1],?m[2]); Main.mm?=?3;//?到發(fā)送端去確認已經成功的信息 break; } return?w; } }
package?uploaddemo; import?java.sql.Connection; import?java.sql.PreparedStatement; import?java.sql.ResultSet; import?java.sql.SQLException; /注冊?登錄?上傳?和數據庫直接連接 /* ?*?zhuC(a,b)注冊?返回0失敗1成功 ?*?dengL(a,b)登錄?返回0失敗1成功 ?*/ public?class?model?{ //?注冊方法?id,password即可 public?int?zhuC(String?id,?String?psd)?{ int?mm?=?0; Connection?net?=?mysqlnet.getnet(); String?str?=?"insert?into?admin"?+?"(id,psd)values(?,?);"; try?{ PreparedStatement?n?=?net.prepareStatement(str); n.setString(1,?id); n.setString(2,?psd); n.execute(); mm?=?1; }?catch?(Exception?e)?{ mm?=?0; } return?mm; } //?登錄方法?id,password即可?dl返回0失敗1成功 public?int?dengL(String?id,?String?psd)?{ Connection?net?=?mysqlnet.getnet(); int?dl?=?0; String?str?=?"select?psd?from?admin?"?+?"where?id=?;"; try?{ PreparedStatement?n?=?net.prepareStatement(str); n.setString(1,?id); ResultSet?re?=?n.executeQuery(); if?(re.next())?{ if?(psd.equals(re.getString("psd")))?{ dl?=?1; }?else?{ dl?=?0; } } }?catch?(SQLException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); dl?=?0; } return?dl; } //?上傳 public?int?shangC(String?id,?String?psd)?{ int?mm?=?0; Connection?net?=?mysqlnet.getnet(); String?str?=?"insert?into?file"?+?"(fName,fdir)values(?,?);"; try?{ PreparedStatement?n?=?net.prepareStatement(str); n.setString(1,?id); n.setString(2,?psd); n.execute(); mm?=?1; }?catch?(Exception?e)?{ mm?=?0; } return?mm; } }
package?uploaddemo; import?java.util.Scanner; ???????? public?class?MainClient?{ ????//客戶端main public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub Scanner?input=new?Scanner(System.in); while(true){ System.out.println("請選擇操作類型"+"\n"+"1,注冊"+"\n"+"2,登錄"); System.out.println("*********************"); String?in=input.nextLine(); int?mm=0; try{ mm=Integer.valueOf(in); }catch(Exception?e){ System.out.println("輸入有誤,請重新輸入"); } if(mm==1){//注冊 while(true){ System.out.println("請輸出用戶名"); String?in11=input.nextLine(); System.out.println("請輸出密碼"); String?in12=input.nextLine(); System.out.println("請確認密碼"); String?in13=input.nextLine(); if(in12.equals(in13)){ new?sendClient().send(in11,?in12,1); ??break; }else{ System.out.println("兩次輸入密碼不一直,請重新輸入"); System.out.println("*********************"); } } int?aa=?new?sendClient().receive(); if(aa==1){ ??System.out.println("恭喜你注冊成功"); ?}else{ ??System.out.println("注冊失敗啦都比"); ??} System.out.println("*********************"); } //登錄&上傳 if(mm==2){ System.out.println("請輸出用戶名"); String?in11=input.nextLine(); System.out.println("請輸出密碼"); String?in12=input.nextLine(); new?sendClient().send(in11,?in12,2); int?aa=?new?sendClient().receive(); if(aa==1){ ??System.out.println("恭喜你登錄成功"); ?new?sendClient().sChuan(in11); ?}else{ ??System.out.println("登錄失敗啦都比"); ?? ??} System.out.println("*********************"); } } } }
package?uploaddemo; import?java.io.BufferedReader; import?java.io.File; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.IOException; import?java.io.InputStreamReader; import?java.io.OutputStream; import?java.io.PrintWriter; import?java.net.ServerSocket; import?java.net.Socket; import?java.net.UnknownHostException; import?java.util.Scanner; //客戶端發(fā)送 public?class?sendClient?{ public?void?send(String?a,?String?b,?int?c)?{ Socket?net?=?null; try?{ net?=?new?Socket("localhost",?9999); PrintWriter?pnet?=?new?PrintWriter(net.getOutputStream()); pnet.println(c); pnet.flush(); pnet.println(a); pnet.flush(); pnet.println(b); pnet.flush(); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); }?finally?{ if?(net?!=?null) try?{ net.close(); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } } //?客戶端接受 public?int?receive()?{ while?(true)?{ int?i?=?0;?//?返回i?0失敗?1成功 ServerSocket?net?=?null; try?{ net?=?new?ServerSocket(8888); Socket?nett?=?net.accept(); BufferedReader?bnet?=?new?BufferedReader(new?InputStreamReader( nett.getInputStream())); String?b?=?null; while?((b?=?bnet.readLine())?!=?null)?{ i?=?Integer.valueOf(b); } }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); System.out.println("登錄失敗"); }?finally?{ if?(net?!=?null) try?{ net.close(); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } return?i; } } //?上傳客戶發(fā)送端 public?void?sChuan(String?id)?{ Scanner?input?=?new?Scanner(System.in); System.out.println("請輸入文件絕對路徑"); String?str?=?input.nextLine(); File?ss?=?new?File(str); new?sendClient().send(id,?ss.getName(),?3); FileInputStream?inS?=?null; Socket?nett?=?null; try?{ nett?=?new?Socket("localhost",?7777); inS?=?new?FileInputStream(str); OutputStream?nettt?=?nett.getOutputStream(); int?a?=?0; while?((a?=?inS.read())?!=?-1)?{ nettt.write(a); nettt.flush(); } inS.close(); nett.close(); }?catch?(FileNotFoundException?e)?{ System.out.println("輸入文件名有誤!上傳失敗"); }?catch?(UnknownHostException?e)?{ System.out.println("未知本地地址,連接服務器失敗"); }?catch?(IOException?e)?{ System.out.println("輸入有誤"); }?finally?{ try?{ if?(inS?!=?null) inS.close(); if?(nett?!=?null) nett.close(); }?catch?(IOException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } } }
package?uploaddemo; import?java.sql.Connection; import?java.sql.DriverManager; import?java.sql.SQLException; /*數據庫連接到dd數據庫 ?*?getnet方法獲得連接 ?*/ public?class?mysqlnet?{ private?static?final?String?URL="jdbc:mysql://localhost:3306/dd"; private?static?Connection?net=null; static?{ try?{ Class.forName("com.mysql.jdbc.Driver"); }?catch?(ClassNotFoundException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } public?static?Connection?getnet(){ try?{ net=DriverManager.getConnection(URL,"root","12345"); }?catch?(SQLException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } return?net; } }
簡單說下寫的程序,實現過程是這樣的:服務接收端隨時監(jiān)控Socket請求(每次socket結束后會關閉掉,這里是直接填寫的雙方地址和端口號,主要不知道怎么斷了重連,獲取到的客戶端端口是動態(tài)的) ,連接到上后 ?客戶端會發(fā)給 服務端一個長度為3的數組,其中包涵用戶名,密碼,表示客戶請求目的的變量。 服務端收到后 拿去分析第三個數字 再決定是否注冊 和登錄,到服務發(fā)送端返回結果。如果接到上傳的請求,就有個專門的上傳接收方法接收文件 (再傳文件之前,調用接收端,接收一下文件的名字),然后io寫入一個自定文件夾,insert 文件的信息(這里不知道怎么實現把文件直接存到數據庫,可以直接預覽?)。 ? ? 調試bug真tm多,修bug都花了一天-·終于搞定了阿西壩,歡迎指點···
2015-12-22
加油
加油
2015-08-18
好贊!同學你的學習態(tài)度是我們的榜樣!