交作业了!哈哈-·
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:// 发送//mm=2,进入发送确认端口 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; //服务器接受发送端 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[]分别读取发送过来的前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; } } // 发送端 确认信息 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;// 到发送端去确认已经成功的信息 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; //客户端发送 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; } } // 上传客户发送端 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; } }
简单说下写的程序,实现过程是这样的:服务接收端随时监控Socket请求(每次socket结束后会关闭掉,这里是直接填写的双方地址和端口号,主要不知道怎么断了重连,获取到的客户端端口是动态的) ,连接到上后 客户端会发给 服务端一个长度为3的数组,其中包涵用户名,密码,表示客户请求目的的变量。 服务端收到后 拿去分析第三个数字 再决定是否注册 和登录,到服务发送端返回结果。如果接到上传的请求,就有个专门的上传接收方法接收文件 (再传文件之前,调用接收端,接收一下文件的名字),然后io写入一个自定文件夹,insert 文件的信息(这里不知道怎么实现把文件直接存到数据库,可以直接预览?)。 调试bug真tm多,修bug都花了一天-·终于搞定了阿西坝,欢迎指点···