计算机网络课设实验报告材料FTP_第1页
计算机网络课设实验报告材料FTP_第2页
计算机网络课设实验报告材料FTP_第3页
计算机网络课设实验报告材料FTP_第4页
计算机网络课设实验报告材料FTP_第5页
已阅读5页,还剩51页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

文档简易的FTP软件文档目 录一、需求分析 31.1 用户需求 31.2 功能需求 31.3 性能需求 3二、概要设计 32.1 主要模块功能描述 32.2 整体结构模块图 42.3 流程图 5三、详细设计 53.1 界面模块实现 53.2 模块功能调用 113.3 功能函数编写及实现效果 11四、调试分析 14五、测试结果 15六、课程设计总结 20七、参考文献 21八、附录 21文档一、需求分析1.1用户需求实现一个简易的 FTP软件,要现FTP常用的功能,包括列文件目录,下载文件,上传文件,用户登录 /注销等。1.2功能需求该系统主要包括: 1.可视化的交互界面模块; 2.连接服务器用户登录模块; 3.浏览服务器/本地目录和文件模块; 4.上传/下载文件模块; 5.目录操作模块。1.3性能需求整个系统操作应当简便,界面友好,运行稳定,执行速度快。系统本身运行对计算机硬件平台和操作系统平台要求适中。二、概要设计2.1主要模块功能描述本次课程设计的简易FTP软件需要具备一些基本功能模块,如连接、上传、下载等,其中最主要的功能模块如下:1、下载功能模块:连接用户指定的FTP服务器,获取服务器目录下的文件列表,当用户选择项目为文件时,点击下载可以下载用户所需要的文件。选定文件后提示用户将文件存储在何处,用户选择存储区后开始下载任务。依靠FTP协议的支持,用户下载的文件没有类型和大小限制, 即用户可已选择任何自己想要下载的文件,通过 FTP客户端进行下载。2、上传功能模块:用户可以自行选择本机上存储的文件,上传至 FTP服务器。在用户选择存储在服务器的位置后,点击上传,选择所需要上传的文件,即可将文件上传至服务器。3、连接功能模块:将用户本机与指定的IP地址连接,验证用户命和密码,在FTP服务器上进行注册。4、目录操作模块:连接用户指定的 FTP服务器,可以创建或删除文件夹,文档也可以通过更改远程工作目录改变该用户的根目录。2.2整体结构模块图客户在启动客户端程序后输入相应的登陆信息就能连接到服务器上,对服务器上的文件进行浏览、新建、删除、重命名、上传、下载文件等操作,通过断开结束与服务器的连接。文档2.3流程图三、详细设计本次课程设计的界面显示通过 mainframe类实现,所有界面相关定义、布局等以及各功能模块的调用都在 mainframe类中编写。各功能定义及实现则在 ftpfunction类中编写。3.1界面模块实现由于各界面编写过程中代码太多并且源代码已在结尾附上,在此以登陆界面为例,以下是对登录界面按钮、输入框等的布局。登陆界面源代码:文档jLabel1.setText("jLabel1");jLabel2.setText("用户名:");jLabel3.setText("密码:");jButton1.setText("登陆");jButton1ActionPerformed(evt);}});jButton2.setText("连接服务器}});jLabel6.setText("远处服务器地址: ");jTextField5.setText("localhost");}});jLabel7.setText("端口号:");文档jTextField6.setText("21");宋体",1,12));jLabel8.setText("注意:请先连接服务器再登陆 ");jButton6.setText("断开连接");jButton6ActionPerformed(evt);}}); jPanel1Layout = newjPanel1.setLayout(jPanel1Layout);.addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swing.addGroup(jPanel1Layout.createSequentialGroup().addGap(117,117,117).addGroup(jPanel1Layout.createParallelGroup(ja.addComponent(jLabel2).addComponent(jLabel3)).addGap(18,18,18).addGroup(jPanel1Layout.createParallelGroup(ja文档.addComponent(jTextField2,.addComponent(jTextField1,.addGroup(jPanel1Layout.createSequentialGroup().addGap(123,123,123).addComponent(jLabel7).addGap(18,18,18).addComponent(jTextField6,.addGroup(jPanel1Layout.createSequentialGroup().addGap(55,55,55).addGroup(jPanel1Layout.createParallelGroup(ja.addComponent(jLabel8).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel6).addGap(37,37,37).addComponent(jTextField5,.addGap(94,94,94)).addGroup(jPanel1Layout.createSequentialGroup().addGap(193,193,193).addComponent(jButton1)).addGroup(jPanel1Layout.createSequentialGroup().addGap(184,184,184).addComponent(jButton2)文档cement.RELATED,27,Short.MAX_VALUE).addComponent(jButton6).addGap(5,5,5))).addGap(119,119,119)));jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel8).addComponent(jLabel6).addComponent(jTextField5, .addGap(28,28,28).addComponent(jLabel7).addComponent(jTextField6, .addGap(13,13,13)文档.addComponent(jButton2).addComponent(jButton6)).addGap(18,18,18).addComponent(jLabel2).addComponent(jTextField1, .addGap(18,18,18).addComponent(jLabel3).addComponent(jTextField2,.addGap(18,18,18).addComponent(jButton1).addGap(98,98,98)));jTabbedPane1.addTab("登陆",jPanel1);用户登录界面显示如下图:文档3.2模块功能调用在mainframe类中定义的各按钮通过对ftpfunction类中的函数进行调用实现各自功能,以下是对各模块按钮在运行过程中调用函数的编写。由于按钮太多并且源代码已在结尾附上,在此以用户登录过程中的函数调用为例:在用户登录界面过调用FtpFunction类中的setUser、setPW、login三个函数实现用户的登录。{Stringuser=jTextField1.getText().toString();Stringpw=jTextField2.getText().toString();FtpFunction.setUser(user);FtpFunction.setPW(pw);FtpFunction.login();}3.3功能函数编写及实现效果在ftpfunction 类中创建并定义了各个模块的功能函数, 由于定义函数太多并且源代码已在结尾附上,在此以登陆时所需用到的 login函数为例,以下为login函数的源代码:publicvoidlogin(){try{文档if(connectSocket==null){JOptionPane.showConfirmDialog(null,服务器尚未连接,请先连接!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);return;}sendCommand("USER"+user);response=readLine();if(!response.startsWith("331")){cleanup();JOptionPane.showConfirmDialog(null,用户名或密码错误!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);用户名或密码错误! "+response);return;}sendCommand("PASS"+passWord);response=readLine();if(!response.startsWith("230")){cleanup();JOptionPane.showConfirmDialog(null,用户名或密码错误!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);用户名或密码错误! "+response);return;文档}logined=true;JOptionPane.showConfirmDialog(null,登陆成功!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);cwd(remotePath);}catch(Exceptione){JOptionPane.showConfirmDialog(null,登陆失败!",登陆信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);}}如果在服务器尚未连接的情况下登陆,则会显示如下界面:如果用户名或密码输入错误,则会显示如下界面:文档在连接服务器后,且用户名和密码都输入成功的情况下,会显示如下界面:四、调试分析此建议

FTP软件在

U-FTPsrv运行情况下作出测试, 结果基本能够达到预期功能,

满足用户的基本的登录服务器,上传下载数据的需求,并且在

Windows

平台上经测试能够稳定的运行。不足之处在于上传下载过程中无法得知过程进度、剩余时间等信息,对用户上传较大文件时会造成一定的不方便,能加入断点传输的话会更好。总的来说,测试容较少,可能会存在一些 BUG,与现实生活家使用的那些 FTP下载软件有相当的距离。对此,还更应该的深入的学习 Java语言中的那多重要的类库,更全面的了解他们的功能,文档争取完善其功能。五、测试结果输入服务器的 IP地址,默认端口号为 21,连接服务器:服务器连接成功后,输入用户名和登录密码:所登陆用户的详细信息:输入要上传的文件路径,点击上传进行上传:文档上传前该用户的文件夹:上传成功后该用户的文件夹:浏览服务器地址中该用户的工作目录:文档显示当前目录,显示在了框中,如下图:在当前目录中创建文件夹 123,该用户工作目录的文件夹中出现名为 123的文件夹,可见于下面截图的左下角:文档删除名为123的文件夹,该文件夹在用户的工作目录的文件夹中删除,下面截图的左下角:

同样可见于改变该用户的工作目录,截图中为改为A为当前用户的工作目录,通过显示当前工作目录可以知道更改工作目录成功,也可以通过显示远程文件列表验证是否成功:文档输入所需下载的文件路径和下载后存放的路径进行下载:下图可见2.doc下载成功:通过断开连接可同时进行断开服务器连接和用户注销:文档六、课程设计总结通过翻阅书籍,在网上查阅资料,实践编程等一系列进程,一个简易的 FTP客户端软件的设计与开发终于完成了,虽然功能上还有许多不足,但通过这些时间的钻研,从拟定方案,设计模块,解决开发过程中遇到的各种困难,最终实现了该文件传输系统的基本功能。实现了远程登录服务器、浏览本地与服务器目录文件和数据上传下载等功能需求,从而实现了一个简易的FTP客户端软件。通过自己实际设计并开发一个FTP客户端软件,使我了解了C/S开发模式和FTP的发展历史及工作原理,对FTP协议在网络中的结构和本身协议在传输的作用有了更深的认识。通过系统测试,我又了解了软件测试的理论知识,理解了测试的概念,了解了测试方法以及过程,虽然只是很有限的尝试和使用,但我尽量通过所学的知识从中寻找错误和不足,也对我今后做类似项目起到了积极的指导作用,总的说来此次课题我受益颇丰。遇到的问题是刚开始输入文件进行上传下载的格式总是不正确,导致一段时间的焦虑和止步不前,但通过查阅资料,对输入的格式有了更加清晰的认知,终于解决了这个问题。我们在遇到问题时,要积极查阅资料或者和老师同学进行探讨,对基础知识一定要掌握的非常牢靠,才能不出现一些小问题。随着计算机网络的迅猛发展,计算机网络的应用日益广泛,并且已经渗透到生活的方方面面,对人们的生活起着不可忽视的作用。在这个信息化的社会中,了解网络是当代大学生必不可少的一门课程。尤其是对我们信息专业的学生,认识计算机网络的基本理论,以及其在生活中发挥的重大作用,为今后我们进一步深入学习专业课程,奠定了良好的基础。 科学技术日新月异蓬勃发展,从20世纪90年代初迅速发展起来的文档internet,已经飞速改变了人们的生活和工作。人们被其丰富无穷的信息资源、方便快捷的交流方式深深吸引。如今计算机网络的教育更是早已深入大学校园,尤其是对于我们信息管理与信息系统这个专业,网络是信息传播、资源共享的重要媒介,这门课程也是我们必不可少的一课。随着计算机技术的迅猛发展,计算机的应用逐渐渗透到各个技术领域和整个社会的各个方面。社会的信息化、数据的分布处理、各种计算机资源的共享等各种应用要求都推动计算机技术朝着群体化方向发展,促使计算机技术与通信技术紧密结合。网络是计算机的一个群体,是由多台计算机组成的,这些计算机是通过一定的通信介质互连在一起的, 计算机之间的互连是指它们彼此之间能够交换信息。 计算机网络属于多机系统的畴, 是计算机和通信这两大现代技术相结合的产物, 它代表着当前计算机体系结构发展的一个重要方向。计算机网络技术的发展和普及日益改变着我们的学习和生活,各种各样的网络应用让我们眼花缭乱,因特网让我们真正体会到信息爆炸的威力。七、参考文献HerbertSchildt.Java实用教程[M].:清华大学,2005。月,叶曙光,徐饪.Jbuilder9入门与提高[M].:科学,2004。耿祥义,跃平.JAVA2实用教程(第二版)[M].:清华大学,2004。[4]树林,洁萍.JAVA语言最新实用案例教程 [M].:科学,2006。W.RichardStevens.TCP/IP详解卷1:协议[M].:机械工业,2000。晓明.计算机网络编程技术[M].:中国铁道,2009。八、附录Mainframe 类:文档ftpFunctionFtpFunction=newftpFunction();/**CreatesnewformmainFrame*/publicmainFrame(){setTitle("ftp 客户端");setLocation(250,150);initComponents();}privatevoidinitComponents(){文档jLabel1.setText("jLabel1");jLabel2.setText(" 用户名:");jLabel3.setText(" 密码:");jButton1.setText(" 登陆");文档jButton1ActionPerformed(evt);}});jButton2.setText(" 连接服务器");jButton2ActionPerformed(evt);}});jLabel6.setText(" 远处服务器地址: ");jTextField5.setText("localhost");jTextField5ActionPerformed(evt);}});jLabel7.setText(" 端口号:");jTextField6.setText("21"); 宋体",1,12));jLabel8.setText(" 注意:请先连接服务器再登陆 ");jButton6.setText(" 断开连接");jButton6ActionPerformed(evt);}});文档

jPanel1Layout

=

newjPanel1.setLayout(jPanel1Layout);jPanel1Layout.setHorizontalGroup(.addGroup(jPanel1Layout.createSequentialGroup().Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createSequentialGroup().addGap(117,117,117).addGroup(jPanel1Layout.createParallelGroup(javax.s.addComponent(jLabel2).addComponent(jLabel3)).addGap(18,18,18).addGroup(jPanel1Layout.createParallelGroup(javax.s.addComponent(jTextField2,.addComponent(jTextField1,.addGroup(jPanel1Layout.createSequentialGroup().addGap(123,123,123).addComponent(jLabel7).addGap(18,18,18).addComponent(jTextField6,文档.addGroup(jPanel1Layout.createSequentialGroup().addGap(55,55,55).addGroup(jPanel1Layout.createParallelGroup(javax.s.addComponent(jLabel8).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel6).addGap(37,37,37).addComponent(jTextField5, 112,.addGap(94,94,94)).addGroup(jPanel1Layout.createSequentialGroup().addGap(193,193,193).addComponent(jButton1)).addGroup(jPanel1Layout.createSequentialGroup().addGap(184,184,184).addComponent(jButton2)ent.RELATED,27,Short.MAX_VALUE).addComponent(jButton6).addGap(5,5,5))).addGap(119,119,119)));jPanel1Layout.setVerticalGroup(文档jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel8)TED,11,Short.MAX_VALUE).Alignment.BASELINE).addComponent(jLabel6).addComponent(jTextField5, .addGap(28,28,28).Alignment.BASELINE).addComponent(jLabel7).addComponent(jTextField6, .addGap(13,13,13).Alignment.BASELINE).addComponent(jButton2).addComponent(jButton6)).addGap(18,18,18).Alignment.BASELINE).addComponent(jLabel2).addComponent(jTextField1, 文档.addGap(18,18,18).Alignment.BASELINE).addComponent(jLabel3).addComponent(jTextField2, .addGap(18,18,18).addComponent(jButton1).addGap(98,98,98)));jTabbedPane1.addTab(" 登陆",jPanel1);jLabel4.setText(" 文件路径:");jButton3.setText(" 上传");jButton3ActionPerformed(evt);}});

jPanel2Layout

=

newjPanel2.setLayout(jPanel2Layout);jPanel2Layout.setHorizontalGroup(.addGroup(jPanel2Layout.createSequentialGroup().addGap(48,48,48).addComponent(jLabel4).addGap(42,42,42)文档.addComponent(jTextField3,.addGap(36,36,36).addComponent(jButton3).addContainerGap(82,Short.MAX_VALUE)));jPanel2Layout.setVerticalGroup(.addGroup(jPanel2Layout.createSequentialGroup().addGap(37,37,37).Alignment.BASELINE).addComponent(jLabel4).addComponent(jTextField3, .addComponent(jButton3)).addContainerGap(301,Short.MAX_VALUE)));jTabbedPane1.addTab(" 上传文件",jPanel2);jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jButton5.setText(" 显示远程文件列表 ");jButton5ActionPerformed(evt);}});文档jLabel11.setText(" 所需下载的文件路径: ");jLabel12.setText(" 下载后存放路径: ");jButton9.setText(" 下载");jButton9ActionPerformed(evt);}});

jPanel3Layout

=

newjPanel3.setLayout(jPanel3Layout);jPanel3Layout.setHorizontalGroup(.addGroup(jPanel3Layout.createSequentialGroup().Alignment.LEADING,false).addGroup(jPanel3Layout.createSequentialGroup().addGap(35,35,35).addComponent(jButton5).addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout.createParallelGroup(javax.s.addComponent(jLabel11).addComponent(jLabel12)).addGap(18,18,18).addGroup(jPanel3Layout.createParallelGroup(javax.s文档.addComponent(jTextField10).addComponent(jTextField9,.addComponent(jButton9)))).addGroup(jPanel3Layout.createSequentialGroup().addGap(50,50,50).addComponent(jScrollPane1, 379,.addContainerGap(84,Short.MAX_VALUE)));jPanel3Layout.setVerticalGroup(.addGroup(jPanel3Layout.createSequentialGroup().addGap(25,25,25).addComponent(jButton5)LATED).addComponent(jScrollPane1, 184,.addGap(18,18,18).Alignment.BASELINE).addComponent(jLabel11).addComponent(jTextField9,文档 .addGap(18,18,18).Alignment.BASELINE).addComponent(jLabel12).addComponent(jTextField10, .addComponent(jButton9)).addContainerGap(37,Short.MAX_VALUE)));jTabbedPane1.addTab(" 下载文件",jPanel3);jLabel5.setText(" 创建目录:");jButton4.setText(" 创建");jButton4ActionPerformed(evt);}});jLabel9.setText(" 当前目录:");jButton7.setText(" 显示");jButton7ActionPerformed(evt);}});jLabel10.setText(" 删除目录:");jButton8.setText(" 删除");文档jButton8ActionPerformed(evt);}});jLabel13.setText(" 改变远程工作目录: ");jButton10.setText(" 改变目录");jButton10ActionPerformed(evt);}});

jPanel4Layout

=

newjPanel4.setLayout(jPanel4Layout);jPanel4Layout.setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel4Layout.createSequentialGroup().addGap(78,78,78).addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel4Layout.createSequentialGroup().addComponent(jLabel13).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jTextField11,javax.swing.GroupLayout.PREFERRED_SIZE,137,文档.addGap(18,18,18).addComponent(jButton10)).addGroup(jPanel4Layout.createSequentialGroup().addGroup(jPanel4Layout.createSequentialGroup().addComponent(jLabel5)ntPlacement.UNRELATED).addComponent(jTextField4, 125,.addGroup(jPanel4Layout.createParallelGroup(javax.swinING,jPanel4Layout.createSequentialGroup().addComponent(jLabel10).addGap(18,18,18).addComponent(jTextField8))ING,jPanel4Layout.createSequentialGroup().addComponent(jLabel9).addGap(18,18,18).addComponent(jTextField7, 116,.addGap(35,35,35)文档.addComponent(jButton7).addComponent(jButton4).addComponent(jButton8)))).addContainerGap(85,Short.MAX_VALUE)));jPanel4Layout.setVerticalGroup(.addGroup(jPanel4Layout.createSequentialGroup().addGap(34,34,34).Alignment.BASELINE).addComponent(jLabel5).addComponent(jTextField4, .addComponent(jButton4)).addGap(27,27,27).Alignment.BASELINE).addComponent(jLabel9).addComponent(jTextField7, .addComponent(jButton7)).addGap(38,38,38).Alignment.BASELINE).addComponent(jLabel10).addComponent(jTextField8,文档 .addComponent(jButton8)).addGap(37,37,37).Alignment.BASELINE).addComponent(jLabel13).addComponent(jTextField11, .addComponent(jButton10)).addContainerGap(127,Short.MAX_VALUE)));jTabbedPane1.addTab(" 目录操作",jPanel4); layout = newgetContentPane().setLayout(layout);layout.setHorizontalGroup(.addComponent(jTabbedPane1, 522,Short.MAX_VALUE));layout.setVerticalGroup(.addComponent(jTabbedPane1, 393,Short.MAX_VALUE));pack();文档}//</editor-fold>//GEN-END:initComponents用户登陆按钮private void evt){//GEN-FIRST:event_jButton1ActionPerformedStringuser=jTextField1.getText().toString();Stringpw=jTextField2.getText().toString();FtpFunction.setUser(user);FtpFunction.setPW(pw);FtpFunction.login();}//GEN-LAST:event_jButton1ActionPerformed连接服务器private void

evt){//GEN-FIRST:event_jButton2ActionPerformedStringremoteHost=jTextField5.getText().toString();intremotePort=Integer .parseInt(jTextField6.getText().toString());FtpFunction.setRemoteHost(remoteHost);FtpFunction.setRemotePort(remotePort);FtpFunction.connect();}//GEN-LAST:event_jButton2ActionPerformedprivate void

evt){//GEN-FIRST:event_jTextField5ActionPerformed//TODOaddyourhandlingcodehere:}//GEN-LAST:event_jTextField5ActionPerformed文件上传private void evt){//GEN-FIRST:event_jButton3ActionPerformedStringlocalFileName=jTextField3.getText().toString();文档try{FtpFunction.upload(localFileName);}catch(IOExceptionex){}}//GEN-LAST:event_jButton3ActionPerformed下载文件按钮private void evt){//GEN-FIRST:event_jButton9ActionPerformedStringremoteFile=jTextField9.getText().toString();StringlocalFile=jTextField10.getText().toString();try{FtpFunction.download(remoteFile,localFile);}catch(IOExceptionex){}}//GEN-LAST:event_jButton9ActionPerformed显示远程文件列表private void evt){//GEN-FIRST:event_jButton5ActionPerformedtry{ArrayList<String>fileList=FtpFunction.list("");for(inti=0;i<fileList.size();i++){jTextArea1.setText(jTextArea1.getText().toString()+fileList.get(i)+"\n");}}catch(IOExceptionex){}文档}//GEN-LAST:event_jButton5ActionPerformed创建目录private void evt){//GEN-FIRST:event_jButton4ActionPerformedStringdirName=jTextField4.getText().toString();try{FtpFunction.mkdir(dirName);}catch(IOExceptionex){}}//GEN-LAST:event_jButton4ActionPerformed断开连接private void evt){//GEN-FIRST:event_jButton6ActionPerformedtry{FtpFunction.close();}catch(IOExceptionex){}JOptionPane.showConfirmDialog(null,成功断开连接!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);}//GEN-LAST:event_jButton6ActionPerformed显示当前目录private void evt){//GEN-FIRST:event_jButton7ActionPerformedtry{Stringdir=FtpFunction.pwd();文档if(dir!=null){jTextField7.setText(dir);}else{jTextField7.setText(" 当前目录为根目录 ");}}catch(IOExceptionex){}}//GEN-LAST:event_jButton7ActionPerformed更改远程工作目录private void evt){//GEN-FIRST:event_jButton10ActionPerformedtry{Stringdir=jTextField11.getText().toString();FtpFunction.cwd(dir);}catch(IOExceptionex){}}//GEN-LAST:event_jButton10ActionPerformed删除目录private void evt){//GEN-FIRST:event_jButton8ActionPerformedtry{FtpFunction.rmdir(jTextField8.getText().toString());//GEN-LAST:event_jButton8ActionPerformed}catch(IOExceptionex){}文档}显示当前远程目录/**paramargsthecommandlinearguments*/publicstaticvoidmain(Stringargs[]){publicvoidrun(){newmainFrame().setVisible(true);}});}文档//Endofvariablesdeclaration//GEN-END:variables}Ftpfunction 类:文档publicclassftpFunction{privateSocketconnectSocket;// 控制连接,用于传送和响应命令privateSocketdataSocket;// 数据连接,用于数据传输privateBufferedReaderinData;// 控制连接中用于读取返回信息的数据流privateBufferedWriteroutData;//控制连接中用于传送用户命令的数据流privateStringresponse=null;//将返回信息封装成字符串privateStringremoteHost;//远程主机名privateintremotePort;//通信端口号privateStringremotePath;//远程路径privateStringuser;//用户名privateStringpassWord;//用户口令FilerootPath=newFile("/");//根路径FilecurrentPath=rootPath;//当前路径privatebooleanlogined;//判断是否登录服务器的标志privatebooleandebug;publicftpFunction(){remoteHost="localhost";remotePort=21;remotePath="/";user="user";passWord="123";logined=false;debug=false;}设置服务器域名(IP地址)publicvoidsetRemoteHost(StringremoteHost){this.remoteHost=remoteHost;文档}返回服务器域名(IP地址)publicStringgetRemoteHost(){returnremoteHost;}设置端口publicvoidsetRemotePort(intremotePort){this.remotePort=remotePort;}返回端口publicintgetRemotePort(){returnremotePort;}//TheremotedirectorypathpublicvoidsetRemotePath(StringremotePath){this.remotePath=remotePath;}Thecurrentremotedirectorypath.publicStringgetRemotePath(){returnremotePath;}//用户名publicvoidsetUser(Stringuser){this.user=user;}密码publicvoidsetPW(Stringpassword){this.passWord=password;}publicvoidsetDebug(booleandebug){文档this.debug=debug;}publicSocketconnect(){try{if(connectSocket==null){connectSocket=newSocket(remoteHost,remotePort);inData = new BufferedReader(newInputStreamReader(connectSocket.getInputStream()));// 输入信息(字符输入流)outData = new BufferedWriter(newOutputStreamWriter(connectSocket.getOutputStream()));// 输出信息(字符输出流)}response=readLine();JOptionPane.showConfirmDialog(null,服务器已经成功连接",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);}catch(Exceptione){JOptionPane.showConfirmDialog(null,连接失败",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);}returnconnectSocket;}publicvoidlogin(){try{if(connectSocket==null){文档JOptionPane.showConfirmDialog(null,"服务器尚未连接,请先连接! ",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);return;}sendCommand("USER"+user);response=readLine();if(!response.startsWith("331")){cleanup();JOptionPane.showConfirmDialog(null,用户名或密码错误!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE); 用户名或密码错误! "+response);return;}sendCommand("PASS"+passWord);response=readLine();if(!response.startsWith("230")){cleanup();JOptionPane.showConfirmDialog(null,用户名或密码错误!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE); 用户名或密码错误! "+response);return;}文档logined=true;JOptionPane.showConfirmDialog(null,登陆成功!",连接信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);cwd(remotePath);}catch(Exceptione){JOptionPane.showConfirmDialog(null,登陆失败!",登陆信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);}}获取远程服务器的目录列表publicArrayList<String>list(Stringmask)throwsIOException{if(!logined){ 服务器尚未连接。 ");//login();}ArrayList<String>fileList=newArrayList<String>();try{dataSocket=createDataSocket();if(mask==null||mask.equals("")||mask.equals("")){sendCommand("LIST");}else{sendCommand("LIST"+mask);}response=readLine();if(!response.startsWith("1")){文档}BufferedReader dataIn = new BufferedReader(newInputStreamReader(dataSocket.getInputStream()));Stringline;while((line=dataIn.readLine())!=null){fileList.add(line);}dataIn.close();// 关闭数据流dataSocket.close();// 关闭数据连接response=readLine();}catch(IOExceptionioex){ioex.printStackTrace();}catch(Exceptionex){ex.printStackTrace();}returnfileList;}///ClosetheFTPconnection.退出登录并终止连接QUITpublicsynchronizedvoidclose()throwsIOException{try{sendCommand("QUIT");}finally{cleanup();正在关闭......");}}文档privatevoidcleanup(){try{inData.close();outData.close();connectSocket.close();//connectSocket=null;logined=false;}catch(Exceptionex){ex.printStackTrace();}}///Ifthevalueofmodeistrue,setbinarymodefordownloads.Else,setAsciimode.//////publicvoidsetBinaryMode(Booleanmode)throwsIOException{if(mode){sendCommand("TYPEI");}else{sendCommand("TYPEA");}response=readLine();if(!response.startsWith("200")){thrownewIOException("CaughtError"+response);}}//显示当前远程工作目录 PWD文档publicsynchronizedStringpwd()throwsIOException{sendCommand("XPWD");Stringdir=null;response=readLine();if(response.startsWith("257")){ //服务器响应信息如: 257"/C:/TEMP/"iscurrentdirectory. 截取两引号之间的容intfristQuote=response.indexOf('\"');intsecondQuote=response.indexOf('\"',fristQuote+1);if(secondQuote>0){dir=response.substring(fristQuote+1,secondQuote);}}returndir;}//CWD 改变远程系统的工作目录publicsynchronizedbooleancwd(Stringdir)throwsIOException{if(dir.equals("/")){// 根路径 当前路径是根目录! ");}if(!logined){login();}sendCommand("CWD"+dir);response=readLine();if(response.startsWith("250")){returntrue;}else{returnfalse;}文档}上传文件publicsynchronizedbooleanupload(StringlocalFileName)throwsIOException{dataSocket=createDataSocket();inti=localFileName.lastIndexOf("/");if(i==-1){i=localFileName.lastIndexOf("\\");}Stringelement_1="";if(i!=-1){element_1=localFileName.substring(i+1);}sendCommand("STOR"+element_1);response=readLine();if(!response.startsWith("1")){}FileInputStreamdataIn=newFileInputStream(localFileName);BufferedOutputStream dataOut = newBufferedOutputStream(dataSocket.getOutputStream());byte[]buffer=newbyte[4096];intbytesRead=0;do{bytesRead=dataIn.read(buffer);if(bytesRead!=-1){dataOut.write(buffer ,0,bytesRead);}}while(bytesRead!=-1);dataOut.flush();dataOut.close();文档dataIn.close();dataSocket.close();// 关闭此数据连接response=readLine();if(response.startsWith("226")){JOptionPane.showConfirmDialog(null,文件上传成功!",上传信息",JOptionPane.CLOSED_OPTION,JOptionPane.INFORMATION_MESSAGE);}return(response.startsWith("226"));}//下载文件 RETRpublic synchronized boolean download(String remoteFile, String localFile) throwsIOException{dataSocket=createDataSocket();sendCommand("RETR"+remoteFile);response=readLine();if(!response.startsWith("1")){}BufferedInputStream dataIn = newBuffere

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论