版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机网络课程设计题目:java实现服务器端的编程姓名:王.漫漫 学号:321209010413—指导教师:王蠢一评阅教师: TOC\o"1-5"\h\z\o"CurrentDocument"绪论 3\o"CurrentDocument"2相关知识 4FTP工作原理 4\o"CurrentDocument"ftp工作模式 6ftp传输方式 7ASCII传输方式 8\o"CurrentDocument"二进制传输方式 8\o"CurrentDocument"3需求分析 9\o"CurrentDocument"数据需求 9\o"CurrentDocument"功能需求 9\o"CurrentDocument"4详细设计 9\o"CurrentDocument"5代码分析 10\o"CurrentDocument"实现服务器端功能的代码: 10\o"CurrentDocument"用户登录时对用户的用户,密码,目录处理。 12\o"CurrentDocument"主函数 15\o"CurrentDocument"16\o"CurrentDocument"49\o"CurrentDocument"6运行结果 63绪论1.1绪论随着计算机的普及,网络进入大众的家庭,通过网络共享数据和资源,已不再是什么新鲜的事,尽管Telnet提供了访问远程文件的极好方法,但怎么也比不上使用自己计算机中的文件方便。如果用户想使用其它计算机上的文件,最理想的方法就是把它COPY到自己的计算机中,以便在本地计算机上操作。那么用什么方法去将远程计算机中的文件获取过来呢?FTP正是完成这项工作的工具。FTP以它所使用的协议:文件传输协议(FileTransferProtocol)来命名的。正如其名所示:协议的任务是从一台计算机将文件传送到另一台计算机,它与这两台计算机所处的位置、联系的方式、以及使用的操作系统无关。假设两台计算机能与FTP协议对话,并且能访HINTERNET,就可以用FTP软件的命令来传输文件。对于不同的操作系统具体操作上可能会有些细微差别,但是其基本的命令结构是相同的。FTP是个非常有用的工具,你可以在任意个可经FTP访问的公共有效的联机数据库或文档中找到你想要的任何东西。全世界现在已有1000多(96年的数据)个FTP文件服务器对所有INTERNET用户开使用,用户可以通过与INTERNET相连的计算机,把自己需要的文件传输过来。FTP采用“客户机/服务器”方式,用户端要在自己的本地计算机上安装FTP客户程序。FTP客户程序有字符界和图形界面(如CuteFTP)两种。2相关知识FTP工作原理FTP(FileTransferProtocol),是文件传输协议的简称。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序。用户可以通过它把自己机器与世界各地所有运FTP协议的服务器相连,访问服务器上的资源和信息。FTP协议在TCP/IP协议栈中的位置如表I:HTTPFTPTELNSMTPDNSTFTPNMP应用层TCPUDP传输层IP互联网络层X25ISDNLANWLANFDDIATM网络接口层当启动FTP从远程计算机拷贝文件时,事实上启动了两个程序:一个本地机器上的FTP客户端程序,它向FTP服务器提出拷贝文件的请求。另一个是启动在远程计算机的上的FTP服务器程序,它响应请求把你指定的文件传送到你的计算机中。FTP采用“客户端/服务器”方式,用户要在自己的本地计算机上安装FTP客户端程序。从根本上说,FTP协议就是在网络中各种不同的计算机之间按照TCP/IP协议来传输文件。FTP协议采用客户端/服务器(Client/Sever)模式,由FTP客户端程序和FTP服务器端程序组成。使用时,先启动FTP客户端程序与远程主机建立连接,然后向远程主机发出传输命令,远程主机在收到命令后就给予响应,并执行正确的命令。但是FTP有一个根本的限制,那就是,如果用户在某个主机上没有注册获得授权,即没有用户名和口令,就不能与该主机进行文件传输。但匿名FTP服务器除外,它能够使用户与远程主机建立连接并以匿名身份从远程主机上拷贝文件,而不必是该远程主机的注册用户。用户使用特殊的用户名“Anonymous”或“guest”就可有限制地访问远程主机上公开的文件。现在许多系统要求用户将Email地址作为口令,以便更好地对访问者进行跟综。出于安全的目的,大部分匿名FTP主机一般只允许远程用户下载文件,而不允许上载文件。也就是说,用户只能从匿名FTP主机拷贝其需要的文件而不能把文件拷贝到匿名FTP主机。另外,匿名FTP主机还采用了其他一些保护措施以保护自己的文件不至于被用户修改和删除,并防止计算机病毒的侵入。匿名FTP一直是Internet上获取信息资源的最主要方式,在Internet成千上万的匿名FTP主机中存储着无以计数的文件,这些文件包含了各种各样的信息、数据和软件。人们只要知道特定信息资源的主机地址,就可以用匿名FTP登陆。ftp工作模式FTP采用C/S(客户端/服务器)模式进行通信,但与其他的C/S模式网络通信协议又有一个很大的区别。通常在进行HTTP通信或是TELNET通信的时候,只需要一个端口进行通信。即客户端只需要连接一个端口进行数据通信。如TELNET的默认端口是23,用户从头到尾都只需使用这个端口。但是FTP通信除了有一个默认端口21之外,还需要其他的端口。其中默认端口(21)主要进行控制连接,进行命令协议和服务器端的响应码的传输;另外一个非标准端口主要进行数据传递,如文件的上载、下载等。至于非标准端口的产生则要根据用户选择的连接模式而定:如果客户选择的是主动模式(PORT),则需要用户端提供给服务器一个IP地址和一个非标准端口;而如果用户采用被动模式(PASV),则服务器端需要提供给客户端一个IP地址和一个非标准端口。下表是几个著名的TCP端口号:表2TCP端口号TCP端口号关键字描述20FTP-DATA文件传输协议数据21FTP文件传输协议控制23TELENET远程登录协议25SMTP简单邮件传输协议53DOMAIN域名服务器80HTTP超文本传输协议110POP3邮局协议119NNTP新闻传送协议FTP中字节大小有两个:逻辑字节大小和用于传输的字节大小。后者通常是8位,而前者可不一定是多少了。传输字节不必等于逻辑字节大小,也不必对数据结构进行解释。控制连接是建立在USER-PIT和SERVER-PI之间用于交换命令与应答的通信链路。数据连接是传输数据的全双工连接,传输数据可以发生在服务器DTP和用户DTP之间也可以发生在两个服务器DTP之间,FTP可以传输非连续的文件,这些文件的一部分称为页。服务器DTP代表一种传输过程,它通常处于“主动”状态,它和侦听端口建立数据连接,它还可以为传输和存储设置参数,并根据PI的指令传输数据。当然,DTP也可以转入“被动”状态。服务器FTP进程,它是和用户FTP进程一起工作的,它由PI和DTP组成。在开始阶段,标准FTP命令由用户PI产生并通过控制连接传送到服务器进程。服务器PI向用户PI返回标准应答。FTP命令指定数据连接参数和文件系统操作。用户DTP在特定数据端口侦听,服务器开始数据连接并以指定的参数开始数据传输。数据端口不必在开始FTP命令的机器上,但用户或用户FTP进程必须确定它在指定的数据端口上侦听.这个数据连接是全双工的。ftp传输方式FTP协议的任务是从一台计算机将文件传送到另一台计算机,它与这两台计算机所处的位置、联接的方式、甚至是是否使用相同的操作系统无关。假设两台计算机通过ftp协议对话,并且能访问Internet,你可以用ftp命令来传输文件。每种操作系统使用上有某一些细微差别,但是每种协议基本的命令结构是相同的。FTP的传输有两种方式:ASCII传输模式和二进制数据传输模式。ASCII传输方式假定用户正在拷贝的文件包含的简单ASCII码文本,如果在远程机器上运行的不是UNIX,当文件传输时ftp通常会自动地调整文件的内容以便于把文件解释成另外那台计算机存储文本文件的格式。但是常常有这样的情况,用户正在传输的文件包含的不是文本文件,它们可能是程序,数据库,字处理文件或者压缩文件(尽管字处理文件包含的大部分是文本,其中也包含有指示页尺寸,字库等信息的非打印字符)。在拷贝任何非文本文件之前,用binary命令告诉ftp逐字拷贝,不要对这些文件进行处理。二进制传输方式在二进制传输中,保存文件的位序,以便原始和拷贝的是逐位一一对应的。即使目的地机器上包含位序列的文件是没意义的。例如,macintosh以二进制方式传送可执行文件到Windows系统,在对方系统上,此文件不能执行。如果你在ASCII方式下传输二进制文件,即使不需要也仍会转译。这会使传输稍微变慢,也会损坏数据,使文件变得不能用。3需求分析设计和实现FTP服务器,主要是遵从FTP协议标准实现FTP服务器;允许现有标准的FTP客户端与它互连互通;支持上传和下载文件。数据需求由于匿名FTP的功能比非匿名FTP有很大的限制,所以为了完成上传下载文件功能,需要设计非匿名FTP,即需要通过账号和密码才能访问此服务器,则服务器需要有创建账号功能,并保存账号信息,这需要建立对应的数据结构表。功能需求FTP服务器要有可供管理的主界面,并可以通过这个界面来添加删除账号,设置路径,查询信息,并根据需要启动和关闭服务。在起内部需要支持上传下载文件功能。4详细设计
图3-1客户端连接流程图。5代码分析实现服务器端功能的代码:publicclassFtpServerprivateSocketsocketclient;privateintcounter;publicstaticStringinitDir;publicstaticArrayListusers=newArrayList();publicstaticArrayListuserslnfo=newArrayList();publicFtpServer()(FtpConsolefc=newFtpConsole();fc.start();loadUsersInfo();intcounter=1;inti=0;try(〃监听21号端口ServerSockets=newServerSocket(21);创建服务器端的socket并设置监听端口21for(;;){〃接受客户端请求Socketincoming=s.accept();BufferedReaderin=newBufferedReader(newInputStreamReader(incoming.getInputStream()));PrintWriterout=newPrintWriter(incoming.getOutputStream(),true);out.println("220Servicereadyfornewuser"+counter);〃创建服务线程FtpHandlerh=newFtpHandler(incoming,i);h.start();users.add(h);counter++;i++;})catch(Exceptione)(e.printStackTrace();}}//FtpServer()end用户登录时对用户的用户,密码,目录处理。publicvoidloadUsersInfo()Strings=getClass().getResource("user.cfg").toString();s=s.substring(6,s.length());intpl=0;intp2=0;if(newFile(s).exists())(try{BufferedReaderfin=newBufferedReader(newInputStreamReader(newFilelnputStream(s)));Stringline;Stringfield;inti=0;while((line=fin.readLine())!=null)(UserinfotempUserlnfo=newUserlnfo();pl=0;p2=0;if(line.startsWith("#"))continue;while((p2=line.indexOf('T',p1))!=-1)(field=line.substring(p1,p2);p2=p2+1;pl=p2;switch(i)(case0:tempUserlnfo.user=field;//System.out.println(tempUserlnfo.user);break;tempUserlnfo.password=field;//System.out.println(tempUserInfo.password);break;tempUserlnfo.workDir=field;//System.out.println(tempUserlnfo.workDir);break;i++;}//while((p2=line.indexOf('T',p1))!=-1)endusersInfo.add(tempUserlnfo);}//while((line=fin.readLine())!=null)endfin.close();Icatch(Exceptione)(e.printStackTrace();)}//if(newFile(s).exists())end}//loadUsersInfo()end主函数publicstaticvoidmain(String[]args){if(args.length!=0)initDir=args[0];elseinitDir="c:/";)FtpServerftpServer=newFtpServer();}//mainendclassFtpHandlerextendsThread(Socketcsocket;用于控制的套接字Socketdsocket;用于传输的套接字intid;Stringcmd=吗存放指令空格前Stringparam=存放当前指令后的参数Stringuser;StringremoteHost="客户IPintremotePort=0;客户Tcp端口号Stringdir=FtpServer.initDir;当前目录Stringrootdir="c:/";//默认根目录,在checkPASS中设置intstate=0;Stringreply;PrintWriterout;inttype=0;Stringrequestfile=booleanisrest=false;intparseInput(Strings)(intp=0;inti=-1;p=s.indexOf("");if(p==-1)cmd=s;elsecmd=s.substring(0,p);if(p>=s.length()IIp==-l)param=elseparam=s.substring(p+1,s.length());cmd=cmd.toUpperCase();if(cmd.equals("USER"))i=1;if(cmd.equals("PASS"))i=2;if(cmd.equals("ACCT"))i=3;if(cmd.equals("CDUP"))i=4;if(cmd.equals("SMNT"))i=5;if(cmd.equals("CWD"))i=6;if(cmd.equals("QUIT"))i=7;if(cmd.equals("REIN"))i=8;if(cmd.equals("PORT"))if(cmd.equals("PASV"))i=10;if(cmd.equals("TYPE"))i=11;if(cmd.equals("STRU"))i=12;if(cmd.equals("MODE"))i=13;if(cmd.equals("RETR"))i=14;if(cmd.equals("STOR"))i=15;if(cmd.equals("STOU"))i=16;if(cmd.equals("APPE"))i=17;if(cmd.equals("ALLO"))i=18;if(cmd.equals("REST"))i=19;if(cmd.equals("RNFR"))if(cmd.equals("RNTO"))i=21;if(cmd.equals("ABOR"))i=22;if(cmd.equals("DELE"))i=23;if(cmd.equals("RMD"))i=24;if(cmd.equals("XMKD"))i=25;if(cmd.equals("MKD"))i=25;if(cmd.equals("PWD"))i=26;if(cmd.equals("LIST"))i=27;if(cmd.equals("NLST"))i=28;if(cmd.equals("SITE"))i=29;if(cmd.equals("SYST"))if(cmd.equals("HELP"))=31;if(cmd.equals("NOOP"))i=32;if(cmd.equals("XPWD"))i=33;returni;}//parseInput()endintvalidatePath(Strings)(Filef=newFile(s);if(f.exists()&&!f.isDirectory())(Stringsi=s.toLowerCase();Strings2=rootdir.toLowerCase();if(sl.startsWith(s2))return1;elsereturn0;f=newFile(addTail(dir)+s);if(f.exists()&&!f.isDirectory())Stringsi=(addTail(dir)+s).toLowerCase();Strings2=rootdir.toLowerCase();if(sl.startsWith(s2))return2;elsereturn0;}return0;}//validatePath()endbooleancheckPASS(Strings)(for(inti=0;i<FtpServer.usersInfo.size();i++)(if(((UserInfo)FtpServer.usersInfo.get(i)).user.equals(user)&&((UserInfo)FtpServer.usersInfo.get(i)).password.equals(s)){rootdir=((UserInfo)FtpServer.usersInfo.get(i)).workDir;dir=((UserInfo)FtpServer.usersInfo.get(i)).workDir;returntrue;returnfalse;}//checkPASS()endbooleancommandUSER(){if(cmd.equals("USER")){reply="331Usernameokay,needpassword";user=param;state=FtpState.FS_WAIT_PASS;returnfalse;)else(reply="501Syntaxerrorinparametersorarguments";returntrue;}//commandUser()endbooleancommandPASS()if(cmd.equals("PASS"))(if(checkPASS(param))(reply="230Userloggedin,proceed";state=FtpState.FS_LOGIN;System.out.println("Message:user"+user+"Form"+remoteHost+"Login");System.out.print("->");returnfalse;)else{reply="530Notloggedin";returntrue;))elsereply="501Syntaxerrorinparametersorarguments";returntrue;}//commandPass()endvoiderrCMD(){reply="500Syntaxerror,commandunrecognized";)booleancommandCDUP(){dir=FtpServer.initDir;Filef=newFile(dir);if(f.getParent()!=null&&(!dir.equals(rootdir)))(dir=f.getParent();reply="200Commandokay";)elsereply="550Currentdirectoryhasnoparent";returnfalse;}//commandCDUP()endbooleancommandCWD(){Filef=newFile(param);Strings=Stringsi=if(dir.endsWith("/"))s=dir;elses=dir+V";Filefl=newFile(s+param);if(f.isDirectory()&&f.exists())(if(param.equals("..")IIparam.equals("..\\"))if(pareToIgnoreCase(rootdir)==0)reply="550Thedirectorydoesnotexists";//returnfalse;)else{si=newFile(dir).getParent();if(sl!=null){dir=si;reply="250Requestedfileactionokay,directorychangeto"+dir;}elsereply="550Thedirectorydoesnotexists";)Ielseif(param.equals(".")IIparam.equals(".\\"))(}elsedir=param;reply="250Requestedfileactionokay,directorychangeto"+dir;elseif(f1.isDirectory()&&fl.exists())(dir=s+param;reply="250Requestedfileactionokay,directorychangeto"+dir;)elsereply="501Syntaxerrorinparametersorarguments";returnfalse;}//commandCDW()endbooleancommandQUIT()(reply="221Serviceclosingcontrolconnection";returntrue;}//commandQuit()endbooleancommandPORT()intpl=0;intp2=0;int[]a=newint[6];inti=0;try(while((p2=param.indexOf(",",p1))!=-1)(a[i]=Integer.parselnt(param.substring(p1,p2));p2=p2+1;pl=p2;i++;)a[i]=Integer.parselnt(param.substring(pl,param.length()));}catch(NumberFormatExceptione)(reply="501Syntaxerrorinparametersorarguments";returnfalse;remoteHost=a[0]+"."+a[l]4-"."+a[2]+"."4-a[3];remotePort=a[4]*256+a[5];reply="200Commandokay";returnfalse;}//commandPort()endbooleancommandLIST()(try(dsocket=newSocket(remoteHost,remotePort,InetAddress.getLocalHost(),20);PrintWriterdout=newPrintWriter(dsocket.getOutputStream(),true);if(param.equals("")IIparam.equals("LIST")){out.println("150OpeningASCIImodedataconnectionfor/bin/ls.");Filef=newFile(dir);String[]dirStructure=f.list();StringfileType;for(inti=0;i<dirStructure.length;i++)if(dirStructure[i].indexOf(".")!=-1)(fileType=)else(fileType="d)dout.println(fileType+dirStructure[i]);))dout.close();dsocket.close();reply="226Transfercomplete!";}catch(Exceptione)(e.printStackTrace();reply="451Requestedactionaborted:localerrorin , H.processing;returnfalse;returnfalse;}//commandLIST()endbooleancommandTYPE()(if(param.equals("a"))(type=FtpState.FTYPE_ASCII;reply="200CommandokayChangetoASCIImode";)elseif(param.equals("i"))(type=FtpState.FTYPE_IMAGE;reply="200CommandokayChangetoBINARYmode";)elsereply="504Commandnotimplementedforthatparameter";returnfalse;}//commandTYPE()endbooleancommandRETR()(requestfile=param;Filef=newFile(requestfile);if(!f.exists())(f=newFile(addTail(dir)+param);if(!f.exists()){reply="550Filenotfound";returnfalse;)requestfile=addTail(dir)+param;)if(isrest)elseif(type==FtpState.FTYPE_IMAGE)try{out.println("150OpeningBinarymodedataconnectionfor"+requestfile);dsocket=newSocket(remoteHost,remotePort,InetAddress.getLocalHost(),20);BufferedlnputStreamfin=newBufferedInputStream(newFilelnputStream(requestfile));PrintStreamdout=newPrintStream(dsocket.getOutputStream(),true);byte[]buf=newbyte[1024];int1=0;while((l=fin.read(buf,0,1024))!=-l){dout.write(buf,0,1);)fin.close();dout.close();dsocket.close();reply="226Transfercomplete!";catch(Exceptione){e.printStackTrace();reply="451Requestedactionaborted:localerrorinprocessing";returnfalse;))if(type==FtpState.FTYPE_ASCII)out.println("150OpeningASCIImodedataconnectionfor"+requestfile);dsocket=newSocket(remoteHost,remotePort,InetAddress.getLocalHost(),20);BufferedReaderfin=newBufferedReader(newFileReader(requestfile));PrintWriterdout=newPrintWriter(dsocket.getOutputStream(),true);Strings;while((s=fin.readLine())!=null)(dout.println(s);)fin.close();dout.close();dsocket.close();reply="226Transfercomplete!";catch(Exceptione)(e.printstackTrace();reply="451Requestedactionaborted:localerrorinprocessing;returnfalse;returnfalse;}//commandRETR()endbooleancommandSTOR()(if(param.equals(""))(reply="501Syntaxerrorinparametersorarguments";returnfalse;)requestfile=addTail(dir)+param;if(type==FtpState.FTYPEJMAGE)(try{out.println("150OpeningBinarymodedataconnectionfor"+requestfile);dsocket=newSocket(remoteHost,remotePort,InetAddress.getLocalHost(),20);BufferedOutputStreamfout=newBufferedOutputStream(newFileOutputStream(requestfile));BufferedlnputStreamdin=newBufferedInputStream(dsocket.getInputStream());byte[]buf=newbyte[1024];int1=0;while((l=din.read(buf,0,1024))!=-l){fout.write(buf,0,l);}//while()din.close();fout.close();dsocket.close();reply="226Transfercomplete!";)catch(Exceptione)(e.printStackTrace();reply="451Requestedactionaborted:localerrorinprocessing";returnfalse;if(type==FtpState.FTYPE_ASCII)tryout.println("150OpeningASCIImodedataconnectionfor"+requestfile);dsocket=newSocket(remoteHost,remotePort,InetAddress.getLocalHost(),20);PrintWriterfout=newPrintWriter(newFileOutputStream(requestfile));BufferedReaderdin=newBufferedReader(newInputStreamReader(dsocket.getInputStream()));Stringline;while((line=din.readLine())!=null){fout.println(line);)din.close();fout.close();dsocket.close();reply="226Transfercomplete!";catch(Exceptione)e.printStackTrace();reply="451Requestedactionaborted:localerrorin, Itprocessing;returnfalse;))returnfalse;}//commandSTOR()endbooleancommandPWD()(reply="257"+dir+"iscurrentdirectory.";returnfalse;}//commandPWD()endbooleancommandNOOP()(reply="200OK.";returnfalse;}//commandNOOP()endbooleancommandABOR()trydsocket.close();catch(Exceptione)(e.printStackTrace();reply="451Requestedactionaborted:localerrorinprocessing";returnfalse;)reply="421Servicenotavailable,closingcontrolconnection";returnfalse;}//commandABOR()endbooleancommandDELE()(inti=validatePath(param);if(i==0)reply="550Requestactionnottaken";returnfalse;if(i==1)(Filef=newFile(param);f.delete();)if(i==2){Filef=newFile(addTail(dir)+param);f.delete();)reply="250Requestfileactionok,complete";returnfalse;}//commandDELE()endbooleancommandMKD()(Stringsi=param.toLowerCase();Strings2=rootdir.toLowerCase();if(s1.startsWith(s2))Filef=newFile(param);if(f.exists()){reply="550Requestactionnottaken";returnfalse;)else{f.mkdirs();reply="250Requestfileactionok,complete";))else(Filef=newFile(addTail(dir)+param);if(f.exists()){reply="550Requestactionnottaken";returnfalse;elsef.mkdirs();reply="250Requestfileactionok,complete";returnfalse;}//commandMKD()endStringaddTail(Strings)(if(!s.endsWith('7"))s=s+V";returns;)publicFtpHandler(Sockets,inti)(csocket=s;id=i;publicvoidrun()Stringstr=intparseResult;BufferedReaderin=newBufferedReader(newInputStreamReader(csocket.getInputStream()));out=newPrintWriter(csocket.getOutputStream(),true);state=FtpState.FS_WAIT_LOGIN;booleanfinished=false;while(Jfinished){str=in.readLine();if(str==null)finished=true;elseparseResult=parselnput(str);System.out.println("Command:"+cmd+"Parameter:"+param);System.out.print("->");switch(state)(caseFtpState.FS_WAIT_LOGIN:finished=commandUSER();break;caseFtpState.FS_WAIT_PASS:finished=commandPASS();break;caseFtpState.FS_LOGIN:(switch(parseResult){case-1:errCMD();break;case4:finished=commandCDUP();break;finished=commandCWD();break;finished=commandQUIT();break;case9:finished=commandPORT();break;case27:finished=commandLIST();break;case11:finished=commandTYPE();break;finished=commandRETR();break;finished=commandSTOR();break;case26:finished=commandPWD();break;case32:finished=commandNOOP();break;finished=commandABOR();break;finished=commandDELE();break;case25:finished=commandMKD();break;}//switch(parseResult)end}//caseFtpState.FS_LOGIN:endbreak;}//switch(state)end}//elseout.println(reply);}//whilecsocket.close();}//trycatch(Exceptione)(e.printStackTrace();))5.5classFtpConsoleextendsThread(BufferedReadercin;StringconCmd;StringconParam;intconsoleQUIT(){System.exit(O);return0;}//consoleQUIT()endbooleanconsoleLISTUSER()(System.out.println("username\t\tworkdirectory");for(inti=0;i<FtpServer.usersInfo.size();i++){System.out.println(((UserInfo)FtpServer.usersInfo.get(i)).user+"\t\t\t"+((UserInfo)FtpServer.usersInfo.get(i)).workDir);)returnfalse;}Z/consoleLISTUSER()endbooleanconsoleLIST()(inti=0;for(i=0;i<FtpServer.users.size();i++){System.out.println((i+1)+":"+((FtpHandler)(FtpServer.users.get(iFrom))).userFrom+((FtpHandler)(FtpServer.users.get(i))).csocket.getInetAddress().toString());returnfalse;}//consoleLIST()endbooleanvalidateUserName(Strings){for(inti=0;i<FtpServer.usersInfo.size();i++){if(((UserInfo)FtpServer.usersInfo.get(i)).user.equals(s))returnfalse;)returntrue;}//validateUserName()endbooleanconsoleADDUSER()(System.out.print("pleaseenterusername:");try
cmnewBufferedReader(newcmnewBufferedReader(newInputStreamReader(System.in));UserinfotempUserlnfo=newUserlnfo();Stringline=cin.readLine();if(line!="")(if(!validateUserName(line))(System.out.println("user"+line+"alreadyexists!");returnfalse;elseSystem.out.println("usernamecannotbenull!");returnfalse;)tempUserlnfo.user=line;System.out.print("enterpasswordline=cin.readLine();if(line!="")tempUserlnfo.password=line;else(System.out.println("passwordcannotbenull!");returnfalse;)System.out.print("entertheinitialdirectory:");line=cin.readLine();if(line!="")(Filef=newFile(line);if(!f.exists())f.mkdirs();tempUserlnfo.workDir=line;)else(System.out.println("thedirectorycannotbenull!");returnfalse;)FtpServer.usersInfo.add(tempUserlnfo);saveUserInfo();catch(Exceptione){e.printStackTrace();returnfalse;)returnfalse;}//consoleADDUSER()endvoidsaveUserInfo()(Strings=try(BufferedWriterfout=newBufferedWriter(newOutputStreamWriter(newFileOutputStream("user.cfg")));for(inti=0;i<FtpServer.usersInfo.size();i++)(s =((UserInfo)FtpServer.usersInfo.get(i)).user4-'T'+((UserInfo)FtpServer.usersInfo.get(i)).password4-'T'+((UserInfo)FtpServer.usersInfo.get(i)).workDir+'T';fout.write(s);fout.newLine();fout.close();)catch(Exceptione)(e.printStackTrace();)}//saveUserInfo()endbooleanconsoleDELUSER()(Strings=//System.out.println(conParam);if(conParam.equals(""))(System.out.println("usage:deluserusername");returnfalse;for(inti=0;i<FtpServer.usersInfo.size();i++)s=((UserInfo)FtpServer.usersInfo.get(i)).user;if(s.equals(conParam))(System.out.println("User"+conParam+"deleted");FtpServer.usersInfo.remove(i);saveUserInfo();returnfalse;))System.out.println("User"+conParam+"notexists");returnfalse;}//consoleDELUSER()endbooleanconsoleHELP()(if(conParam.equals(""))(System.out.println("adduser:addnewuser");System.out.println("deluser<username>:deleteauser");System.out.println("quit:quit");System.out.println("list:listalluserconnecttoserver");System.out.println("listuser:listallaccountofthisserver");System.out.println("help:showthishelp");)elseif(conParam.equals("adduser"))System.out.println("adduser:addnewuser");elseif(conParam.equals("deluser"))System.out.println("deluser<username>:deleteauser");elseif(conParam.equals("quit"))System.out.println("quit:quit");elseif(conParam.equals("list"))System.out.println("list:listalluserconnecttoserver");elseif(conParam.equals("listuser"))System.out.println("listuser:listallaccountofthisserver");elseif(conParam.equals("help"))System.out.println("help:showthishelp");elsereturnfalse;returnfalse;}//consoleHELP()endbooleanconsoleERR(){System.out.println("badcommand!");returnfalse;}//consoleERR()endpublicFtpConsole()(System.out.println("ftpserverstarted!");cin = new BufferedReader(newInputStreamReader(System.in));)publicvoidrun()booleanok=false;Stringinput=while(!ok){System.out.print("->");try(input=cin.readLine();)catch(Exceptione)(e.printStackTrace();)switch(parselnput(input))(case1:consoleQUIT();break;case8:ok=consoleLISTUSER();break;case0:ok=consoleLIST();break;ok=consoleADDUSER();break;ok=consoleDELUSER();break;case7:ok=consoleHELP();break;case-1:ok=consoleERR();break;)}//whileend//}//run()endintparseInput(Strings)StringupperCmd;intp=0;conCmd=conParam=p=s.indexOf("");if(p==-1)conCmd=s;elseconCmd=s.substring(O,p);if(p>=s.length()IIp==
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年交通加盟AI 解决方案协议
- 2026甘肃财贸职业学院招聘事业编制人员20人备考题库完整参考答案详解
- 2026四川攀枝花市东区返乡创业指导员遴选1人备考题库含答案详解(巩固)
- 2026上半年广东肇庆宣卿中学面向高校招聘中学教师8人备考题库(第二批编制)及参考答案详解
- 2026黄淮学院招聘高层次人才38人备考题库及一套参考答案详解
- 2026广东医科大学附属医院遂溪医院(遂溪县人民医院)招聘监管场所固定医师1人备考题库及1套参考答案详解
- 2026非洲移动支付市场普及率与金融包容性研究报告
- 2026非洲本土化生产费托蜡的劳动力成本优势实证分析报告
- 2026宁夏水务集团有限公司社会化招聘5人备考题库附答案详解(精练)
- 广元市特种设备监督检验所2026年第二批检验检测人员招聘备考题库(6人)及答案详解(易错题)
- 2026年一级注册建筑师之建筑材料与构造模考模拟试题一套附答案详解
- 2026年北京市昌平区高三二模英语试卷(含答案)
- 2026年大学生志愿服务西部计划题库
- 2025年三支一扶教师招聘面试题及答案
- 2026年禁毒人员笔试试题及答案
- 人教版七年级数学下册93一元一次不等式组应用题课件(25张)
- 湖北省鄂州市2025-2026学年九年级下学期4月份中考模拟练习语文试题(含答案)
- 南湖杯监理汇报材料
- 清末广西书院改制:历史进程、驱动因素与时代影响
- 2026年54西部计划笔试试题库及答案
- 2026八年级劳动国家质量监测考试卷含答案
评论
0/150
提交评论