您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页计算机网络课设实验报告FTP

计算机网络课设实验报告FTP

来源:宝玛科技网
下载可编辑

.专业.整理.

软件

简易的FTP下载可编辑

目 录

一、需求分析 ........................................................................................ 3 1.1用户需求 ................................................................................... 3 1.2功能需求 ................................................................................... 3 1.3 性能需求 .................................................................................. 3 二、概要设计 ........................................................................................ 3 2.1主要模块功能描述 ................................................................. 3 2.2整体结构模块图 ..................................................................... 4 2.3流程图 ........................................................................................ 5 三、详细设计 ........................................................................................ 5 3.1 界面模块实现 ......................................................................... 6 3.2模块功能调用 ........................................................................ 13 3.3功能函数编写及实现效果 ................................................. 14 四、调试分析 ...................................................................................... 17 五、测试结果 ...................................................................................... 18

.专业.整理.

下载可编辑

六、课程设计总结 ............................................................................. 23 七、参考文献 ...................................................................................... 24 八、附录 ............................................................................................... 25

一、需求分析

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\");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTabbedPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jLabel2.setText(\"用户名:\");

jLabel3.setText(\"密码:\");

jButton1.setText(\"登陆\");

jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

.专业.整理.

下载可编辑

jButton2.setText(\"连接服务器\");

jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } });

jLabel6.setText(\"远处服务器地址:\"); jTextField5.setText(\"localhost\");

jTextField5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField5ActionPerformed(evt); } });

jLabel7.setText(\"端口号:\"); jTextField6.setText(\"21\");

jLabel8.setFont(new java.awt.Font(\"宋体\ jLabel8.setForeground(new java.awt.Color(255, 0, 51)); jLabel8.setText(\"注意:请先连接服务器再登陆\"); jButton6.setText(\"断开连接\");

jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {

.专业.整理.

下载可编辑

jButton6ActionPerformed(evt); } });

javax.swing.GroupLayout javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(117, 117, 117)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel2) .addComponent(jLabel3))

.专业.整理.

jPanel1Layout = new

下载可编辑

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(123, 123, 123) .addComponent(jLabel7) .addGap(18, 18, 18) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(55, 55, 55)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel8)

.专业.整理.

88,

58,

下载可编辑

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jLabel6) .addGap(37, 37, 37) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))

.addGap(94, 94, 94))

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(193, 193, 193) .addComponent(jButton1))

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(184, 184, 184) .addComponent(jButton2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)

.addComponent(jButton6) .addGap(5, 5, 5))) .addGap(119, 119, 119)) );

jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.s

.专业.整理.

112,

下载可编辑

wing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

.addContainerGap() .addComponent(jLabel8)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel6) .addComponent(jTextField5,

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(28, 28, 28)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel7) .addComponent(jTextField6,

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(13, 13, 13)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.Gro

.专业.整理.

下载可编辑

upLayout.Alignment.BASELINE)

.addComponent(jButton2) .addComponent(jButton6)) .addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2) .addComponent(jTextField1,

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3) .addComponent(jTextField2,

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18) .addComponent(jButton1) .addGap(98, 98, 98)) );

jTabbedPane1.addTab(\"登陆\

.专业.整理.

下载可编辑

用户登录界面显示如下图:

3.2模块功能调用

在mainframe类中定义的各按钮通过对ftpfunction类中的函数进行调用实现各自功能,以下是对各模块按钮在运行过程中调用函数的编写。由于按钮太多并且源代码已在结尾附上,在此以用户登录过程中的函数调用为例:

在用户登录界面中通过调用FtpFunction类中的setUser、setPW、login三个函数实现用户的登录。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

String user = jTextField1.getText().toString(); String pw = jTextField2.getText().toString(); FtpFunction.setUser(user); FtpFunction.setPW(pw); FtpFunction.login();

.专业.整理.

下载可编辑

}

3.3功能函数编写及实现效果

在ftpfunction类中创建并定义了各个模块的功能函数,由于定义函数太多并且源代码已在结尾附上,在此以登陆时所需用到的login函数为例,以下为login函数的源代码:

public void login() { try {

if (connectSocket == null) {

JOptionPane.showConfirmDialog(null, \" 服务器尚未连接,请先连接!\

\" 连接信息\

JOptionPane.INFORMATION_MESSAGE); return; }

sendCommand(\"USER \" + user); response = readLine();

if (!response.startsWith(\"331\")) { cleanup();

JOptionPane.showConfirmDialog(null, \" 用户名或密码错误!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE);

.专业.整理.

下载可编辑

System.out.println(\"Error:用户名或密码错误!\" + response); System.out.println(response); return; }

sendCommand(\"PASS \" + passWord); .专业.整理.

response = readLine();

if (!response.startsWith(\"230\")) { cleanup();

JOptionPane.showConfirmDialog(null, \" 用户名或密码错误!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE);

System.out.println(\"Error:用户名或密码错误!\" + response); System.out.println(response); return; }

logined = true;

JOptionPane.showConfirmDialog(null, \" 登陆成功!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE); cwd(remotePath);

下载可编辑

} catch (Exception e) {

JOptionPane.showConfirmDialog(null, \" 登陆失败!\

\" 登陆信息\ 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,已经飞速改变了人们的生活和工作。人们被其丰富无穷的信息资源、方便快捷的交流方式深深吸引。如今计算机网络的教育更是早已深入大学校园,尤其是对于我们信息管理与信息系统这个专业,网络是信息传播、资源共享的重要媒介,这门课程也是我们必不可少的一课。 随着计算机技术的迅猛发展,计算机的应用逐渐渗透到各个技术领域和整个社会的各个方面。社会的信息化、数据的分布处理、各种计算机资源的共享等各种应用要求都推动计算机技术朝着群体化方向发展,促使计算机技术与通信技术紧密结合。网络是计算机的一个群体,是由多台计算机组成的,这些计算机是通过一定的通信介质互连在一起的,计算机之间的互连是指它们彼此之间能够交换信息。计算机网络属于多机系统的范畴,是计算机和通信这两大现代技术相结合的产物,它代表着当前计算机体系结构发展的一个重要方向。 计算机网络技术的发展和普及日益改变着我们的学习和生活,各种各样的网络应用让我们眼花缭乱,因特网让我们真正体会到信息爆炸的威力。

七、参考文献

[1] Herbert Schildt.Java实用教程[M].北京:清华大学出版社,2005。 [2] 胡月,叶曙光,徐饪.Jbuilder9入门与提高[M].北京:科学出版社,2004。

.专业.整理.

下载可编辑

[3] 耿祥义,张跃平.JAVA2实用教程(第二版)[M].北京:清华大学出版社,2004。 [4] 杨树林,胡洁萍.JAVA语言最新实用案例教程[M].北京:科学出版社,2006。 [5] W.Richard Stevens.TCP/IP详解 卷1:协议[M].北京:机械工业出版社,2000。 [6] 张晓明.计算机网络编程技术[M].北京:中国铁道出版社,2009。

八、附录

Mainframe类:

import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane;

public class mainFrame extends javax.swing.JFrame { ftpFunction FtpFunction = new ftpFunction(); /** Creates new form mainFrame */ public mainFrame() { setTitle(\"ftp客户端\"); setLocation(250, 150); initComponents(); }

private void initComponents() {

.专业.整理.

下载可编辑

jLabel1 = new javax.swing.JLabel();

jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); jTextField5 = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jTextField6 = new javax.swing.JTextField(); jLabel8 = new javax.swing.JLabel(); jButton6 = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jLabel4 = new javax.swing.JLabel(); jTextField3 = new javax.swing.JTextField(); jButton3 = new javax.swing.JButton(); jPanel3 = new javax.swing.JPanel();

jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea();

.专业.整理.

下载可编辑

jButton5 = new javax.swing.JButton(); jLabel11 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel(); jTextField9 = new javax.swing.JTextField(); jTextField10 = new javax.swing.JTextField(); jButton9 = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jTextField4 = new javax.swing.JTextField(); jButton4 = new javax.swing.JButton(); jLabel9 = new javax.swing.JLabel(); jTextField7 = new javax.swing.JTextField(); jButton7 = new javax.swing.JButton(); jLabel10 = new javax.swing.JLabel(); jTextField8 = new javax.swing.JTextField(); jButton8 = new javax.swing.JButton(); jLabel13 = new javax.swing.JLabel(); jTextField11 = new javax.swing.JTextField(); jButton10 = new javax.swing.JButton(); jLabel1.setText(\"jLabel1\");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTabbedPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

.专业.整理.

下载可编辑

jLabel2.setText(\"用户名:\");

jLabel3.setText(\"密码:\");

jButton1.setText(\"登陆\");

jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

jButton2.setText(\"连接服务器\");

jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } });

jLabel6.setText(\"远处服务器地址:\"); jTextField5.setText(\"localhost\");

jTextField5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField5ActionPerformed(evt);

.专业.整理.

下载可编辑

} });

jLabel7.setText(\"端口号:\"); jTextField6.setText(\"21\");

jLabel8.setFont(new java.awt.Font(\"宋体\ jLabel8.setForeground(new java.awt.Color(255, 0, 51)); jLabel8.setText(\"注意:请先连接服务器再登陆\");

jButton6.setText(\"断开连接\");

jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } });

javax.swing.GroupLayout jPanel1Layout

=

javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.专业.整理.

new

下载可编辑

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(117, 117, 117)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel2) .addComponent(jLabel3)) .addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jTextField2,

javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)))

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(123, 123, 123) .addComponent(jLabel7) .addGap(18, 18, 18)

.专业.整理.

下载可编辑

.addComponent(jTextField6,

javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(55, 55, 55)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel8)

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jLabel6) .addGap(37, 37, 37) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))

.addGap(94, 94, 94))

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(193, 193, 193) .addComponent(jButton1))

.addGroup(jPanel1Layout.createSequentialGroup() .addGap(184, 184, 184) .addComponent(jButton2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacem

112,

.专业.整理.

下载可编辑

ent.RELATED, 27, Short.MAX_VALUE)

.addComponent(jButton6) .addGap(5, 5, 5))) .addGap(119, 119, 119)) );

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

.addContainerGap() .addComponent(jLabel8)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel6) .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(28, 28, 28)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayo

javax.swing.GroupLayout.DEFAULT_SIZE,

.专业.整理.

下载可编辑

ut.Alignment.BASELINE)

.addComponent(jLabel7) .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(13, 13, 13)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton2) .addComponent(jButton6)) .addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3) .addComponent(jTextField2,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

.专业.整理.

下载可编辑

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18) .addComponent(jButton1) .addGap(98, 98, 98)) );

jTabbedPane1.addTab(\"登陆\ jLabel4.setText(\"文件路径:\"); jButton3.setText(\"上传\");

jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } });

javax.swing.GroupLayout jPanel2Layout

=

javax.swing.GroupLayout(jPanel2);

jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(

jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel2Layout.createSequentialGroup()

.专业.整理.

new

下载可编辑

.addGap(48, 48, 48) .addComponent(jLabel4) .addGap(42, 42, 42) .addComponent(jTextField3,

javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(36, 36, 36) .addComponent(jButton3)

.addContainerGap(82, Short.MAX_VALUE)) );

jPanel2Layout.setVerticalGroup(

jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel2Layout.createSequentialGroup() .addGap(37, 37, 37)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton3))

.addContainerGap(301, Short.MAX_VALUE))

javax.swing.GroupLayout.DEFAULT_SIZE,

.专业.整理.

下载可编辑

);

jTabbedPane1.addTab(\"上传文件\ jTextArea1.setColumns(20); jTextArea1.setRows(5);

jScrollPane1.setViewportView(jTextArea1); jButton5.setText(\"显示远程文件列表\");

jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } });

jLabel11.setText(\"所需下载的文件路径:\"); jLabel12.setText(\"下载后存放路径:\"); jButton9.setText(\"下载\");

jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton9ActionPerformed(evt); } });

javax.swing.GroupLayout jPanel3Layout

=

javax.swing.GroupLayout(jPanel3);

.专业.整理.

new

下载可编辑

jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(

jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel3Layout.createSequentialGroup()

.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(jPanel3Layout.createSequentialGroup() .addGap(35, 35, 35)

.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jButton5)

.addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel11) .addComponent(jLabel12)) .addGap(18, 18, 18)

.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(jTextField10) .addComponent(jTextField9,

.专业.整理.

下载可编辑

javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jButton9)))) .addGroup(jPanel3Layout.createSequentialGroup() .addGap(50, 50, 50)

.addComponent(jScrollPane1,

javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(84, Short.MAX_VALUE)) );

jPanel3Layout.setVerticalGroup(

jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel3Layout.createSequentialGroup() .addGap(25, 25, 25) .addComponent(jButton5)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jScrollPane1,

javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayo

.专业.整理.

下载可编辑

ut.Alignment.BASELINE)

.addComponent(jLabel11) .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel12) .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton9))

.addContainerGap(37, Short.MAX_VALUE)) );

jTabbedPane1.addTab(\"下载文件\ jLabel5.setText(\"创建目录:\"); jButton4.setText(\"创建\");

jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); }

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

.专业.整理.

下载可编辑

});

jLabel9.setText(\"当前目录:\");

jButton7.setText(\"显示\");

jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton7ActionPerformed(evt); } });

jLabel10.setText(\"删除目录:\"); jButton8.setText(\"删除\");

jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton8ActionPerformed(evt); } });

jLabel13.setText(\"改变远程工作目录:\");

jButton10.setText(\"改变目录\");

jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton10ActionPerformed(evt);

.专业.整理.

下载可编辑

} });

javax.swing.GroupLayout javax.swing.GroupLayout(jPanel4);

jPanel4.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, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18) .addComponent(jButton10))

.addGroup(jPanel4Layout.createSequentialGroup()

jPanel4Layout

=

new

.专业.整理.

下载可编辑

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jLabel5)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jTextField4,

javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()

.addComponent(jLabel10) .addGap(18, 18, 18)

.addComponent(jTextField8))

.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()

.addComponent(jLabel9) .addGap(18, 18, 18) .addComponent(jTextField7,

javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGap(35, 35, 35)

.专业.整理.

下载可编辑

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jButton7) .addComponent(jButton4) .addComponent(jButton8)))) .addContainerGap(85, Short.MAX_VALUE)) );

jPanel4Layout.setVerticalGroup(

jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel4Layout.createSequentialGroup() .addGap(34, 34, 34)

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel5) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton4)) .addGap(27, 27, 27)

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

javax.swing.GroupLayout.DEFAULT_SIZE,

.专业.整理.

下载可编辑

.addComponent(jLabel9) .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton7)) .addGap(38, 38, 38)

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel10) .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton8)) .addGap(37, 37, 37)

.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel13) .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton10))

.addContainerGap(127, Short.MAX_VALUE))

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,

.专业.整理.

下载可编辑

);

jTabbedPane1.addTab(\"目录操作\

javax.swing.GroupLayout javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout); layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTabbedPane1, 522, Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTabbedPane1, 393, Short.MAX_VALUE)

); pack();

}// //GEN-END:initComponents //用户登陆按钮 private

void

jButton1ActionPerformed(java.awt.event.ActionEvent

evt)

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, layout

=

new

{//GEN-FIRST:event_jButton1ActionPerformed

.专业.整理.

下载可编辑

String user = jTextField1.getText().toString(); String pw = jTextField2.getText().toString(); FtpFunction.setUser(user); FtpFunction.setPW(pw); FtpFunction.login();

}//GEN-LAST:event_jButton1ActionPerformed //连接服务器 private

void

jButton2ActionPerformed(java.awt.event.ActionEvent

{//GEN-FIRST:event_jButton2ActionPerformed

String remoteHost=jTextField5.getText().toString();

int remotePort=Integer.parseInt(jTextField6.getText().toString()); FtpFunction.setRemoteHost(remoteHost); FtpFunction.setRemotePort(remotePort); FtpFunction.connect();

}//GEN-LAST:event_jButton2ActionPerformed private

void

jTextField5ActionPerformed(java.awt.event.ActionEvent

{//GEN-FIRST:event_jTextField5ActionPerformed

// TODO add your handling code here: }//GEN-LAST:event_jTextField5ActionPerformed

.专业.整理.

evt)

evt)

下载可编辑

//文件上传 private

void

jButton3ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton3ActionPerformed

String localFileName=jTextField3.getText().toString(); try {

FtpFunction.upload(localFileName); } catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

}//GEN-LAST:event_jButton3ActionPerformed //下载文件按钮 private

void

jButton9ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton9ActionPerformed

String remoteFile=jTextField9.getText().toString(); String localFile=jTextField10.getText().toString(); try {

FtpFunction.download(remoteFile, localFile); } catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

}//GEN-LAST:event_jButton9ActionPerformed //显示远程文件列表

.专业.整理.

下载可编辑

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)

{//GEN-FIRST:event_jButton5ActionPerformed

try {

ArrayList fileList = FtpFunction.list(\"\"); for (int i = 0; i < fileList.size(); i++) {

jTextArea1.setText(jTextArea1.getText().toString()+ fileList.get(i)+\"\\n\"); }

} catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

}//GEN-LAST:event_jButton5ActionPerformed //创建目录 private

void

jButton4ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton4ActionPerformed

String dirName=jTextField4.getText().toString(); try {

FtpFunction.mkdir(dirName); } catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

}//GEN-LAST:event_jButton4ActionPerformed

.专业.整理.

下载可编辑

//断开连接 private

void

jButton6ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton6ActionPerformed

try {

FtpFunction.close(); } catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

JOptionPane.showConfirmDialog(null, \" 成功断开连接!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_jButton6ActionPerformed //显示当前目录 private

void

jButton7ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton7ActionPerformed

try {

String dir = FtpFunction.pwd(); if (dir != null) {

jTextField7.setText(dir); } else {

.专业.整理.

下载可编辑

jTextField7.setText(\"当前目录为根目录\"); }

} catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

}//GEN-LAST:event_jButton7ActionPerformed

//更改远程工作目录 private

void

jButton10ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton10ActionPerformed

try {

String dir = jTextField11.getText().toString(); FtpFunction.cwd(dir); } catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); }

}//GEN-LAST:event_jButton10ActionPerformed //删除目录 private

void

jButton8ActionPerformed(java.awt.event.ActionEvent

evt)

{//GEN-FIRST:event_jButton8ActionPerformed

try {

.专业.整理.

下载可编辑

FtpFunction.rmdir(jTextField8.getText().toString());//GEN-LAST:event_jButton8ActionPerformed

} catch (IOException ex) {

Logger.getLogger(mainFrame.class.getName()).log(Level.SEVERE, null, ex); } }

//显示当前远程目录 /**

* @param args the command line arguments */

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new mainFrame().setVisible(true); } }); }

// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton10; private javax.swing.JButton jButton2;

.专业.整理.

下载可编辑

private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JButton jButton6; private javax.swing.JButton jButton7; private javax.swing.JButton jButton8; private javax.swing.JButton jButton9; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2;

.专业.整理.

下载可编辑

private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4;

private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField10; private javax.swing.JTextField jTextField11; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField6; private javax.swing.JTextField jTextField7; private javax.swing.JTextField jTextField8; private javax.swing.JTextField jTextField9;

// End of variables declaration//GEN-END:variables }

Ftpfunction类:

import java.net.Socket; import java.io.*;

.专业.整理.

下载可编辑

import java.util.*;

import javax.swing.JOptionPane; public class ftpFunction {

private Socket connectSocket;//控制连接,用于传送和响应命令 private Socket dataSocket;//数据连接,用于数据传输

private BufferedReader inData;//控制连接中用于读取返回信息的数据流 private BufferedWriter outData;//控制连接中用于传送用户命令的数据流 private String response = null;//将返回信息封装成字符串 private String remoteHost;//远程主机名 private int remotePort;//通信端口号 private String remotePath;//远程路径 private String user;//用户名 private String passWord;//用户口令 File rootPath = new File(\"/\");//根路径 File currentPath = rootPath;//当前路径

private boolean logined;//判断是否登录服务器的标志 private boolean debug; public ftpFunction() {

remoteHost = \"localhost\"; remotePort = 21; remotePath = \"/\"; user = \"user\";

.专业.整理.

下载可编辑

passWord = \"123\"; logined = false; debug = false; }

//设置服务器域名(IP地址)

public void setRemoteHost(String remoteHost) { this.remoteHost = remoteHost; }

//返回服务器域名(IP地址) public String getRemoteHost() { return remoteHost; }

//设置端口

public void setRemotePort(int remotePort) { this.remotePort = remotePort; }

//返回端口

public int getRemotePort() { return remotePort; }

//The remote directory path

public void setRemotePath(String remotePath) {

.专业.整理.

下载可编辑

this.remotePath = remotePath; }

/// The current remote directory path. public String getRemotePath() { return remotePath; } //用户名

public void setUser(String user) { this.user = user; } //密码

public void setPW(String password) { this.passWord = password; }

public void setDebug(boolean debug) { this.debug = debug; }

public Socket connect() { try {

if (connectSocket == null) {

connectSocket = new Socket(remoteHost, remotePort);

.专业.整理.

下载可编辑

inData = new BufferedReader(new

InputStreamReader(connectSocket.getInputStream()));//输入信息(字符输入流)

outData

=

new

BufferedWriter(new

OutputStreamWriter(connectSocket.getOutputStream()));//输出信息(字符输出流)

}

response = readLine();

JOptionPane.showConfirmDialog(null, \"服务器已经成功连接\

\"连接信息\ JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) {

JOptionPane.showConfirmDialog(null, \" 连接失败\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE); }

return connectSocket; }

public void login() { try {

.专业.整理.

下载可编辑

if (connectSocket == null) {

JOptionPane.showConfirmDialog(null, \" 服务器尚未连接,请先连接!\

\" 连接信息\

JOptionPane.INFORMATION_MESSAGE); .专业.整理.

return; }

sendCommand(\"USER \" + user); response = readLine();

if (!response.startsWith(\"331\")) { cleanup();

JOptionPane.showConfirmDialog(null, \" 用户名或密码错误!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE);

System.out.println(\"Error:用户名或密码错误!\" + response); System.out.println(response); return; }

sendCommand(\"PASS \" + passWord); response = readLine();

if (!response.startsWith(\"230\")) {

下载可编辑

cleanup();

JOptionPane.showConfirmDialog(null, \" 用户名或密码错误!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE);

System.out.println(\"Error:用户名或密码错误!\" + response); System.out.println(response); return; }

logined = true;

JOptionPane.showConfirmDialog(null, \" 登陆成功!\

\" 连接信息\ JOptionPane.INFORMATION_MESSAGE); cwd(remotePath); } catch (Exception e) {

JOptionPane.showConfirmDialog(null, \" 登陆失败!\

\" 登陆信息\ JOptionPane.INFORMATION_MESSAGE); } }

.专业.整理.

下载可编辑

//获取远程服务器的目录列表

public ArrayList list(String mask) throws IOException { if (!logined) {

System.out.println(\"服务器尚未连接。\"); //login(); }

ArrayList fileList = new ArrayList(); try {

dataSocket = createDataSocket();

if (mask == null || mask.equals(\"\") || mask.equals(\" \")) { sendCommand(\"LIST\"); } else {

sendCommand(\"LIST \" + mask); }

response = readLine(); if (!response.startsWith(\"1\")) { System.out.println(response); }

BufferedReader

dataIn

=

new

BufferedReader(new

InputStreamReader(dataSocket.getInputStream()));

String line;

while ((line = dataIn.readLine()) != null) {

.专业.整理.

下载可编辑

fileList.add(line); }

dataIn.close();//关闭数据流 dataSocket.close();//关闭数据连接 response = readLine();

System.out.println(\"List Complete.\"); } catch (IOException ioex) { ioex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); }

return fileList; } ///

/// Close the FTP connection. /// 退出登录并终止连接QUIT

public synchronized void close() throws IOException { try {

sendCommand(\"QUIT \"); } finally { cleanup();

.专业.整理.

下载可编辑

System.out.println(\"正在关闭......\"); } }

private void cleanup() { try {

inData.close(); outData.close(); connectSocket.close(); //connectSocket = null; logined = false; } catch (Exception ex) { ex.printStackTrace(); } } ///

/// If the value of mode is true, set binary mode for downloads. /// Else, set Ascii mode. /// ///

public void setBinaryMode(Boolean mode) throws IOException {

.专业.整理.

下载可编辑

if (mode) {

sendCommand(\"TYPE I \"); } else {

sendCommand(\"TYPE A \"); }

response = readLine();

if (!response.startsWith(\"200\")) {

throw new IOException(\"Caught Error \" + response); } }

//显示当前远程工作目录PWD

public synchronized String pwd() throws IOException { sendCommand(\"XPWD \"); String dir = null; response = readLine();

if (response.startsWith(\"257\")) { //服务器响应信息如:257 \"/C:/TEMP/\" is current directory.截取两引号之间的内容

int fristQuote = response.indexOf('\\\"');

int secondQuote = response.indexOf('\\\"', fristQuote + 1); if (secondQuote > 0) {

dir = response.substring(fristQuote + 1, secondQuote); }

.专业.整理.

下载可编辑

}

System.out.println(\"\"+dir); return dir; }

//CWD 改变远程系统的工作目录

public synchronized boolean cwd(String dir) throws IOException { if (dir.equals(\"/\")) {//根路径

System.out.println(\"当前路径是根目录!\"); }

if (!logined) { login(); }

sendCommand(\"CWD \" + dir); response = readLine();

if (response.startsWith(\"250 \")) { return true; } else {

return false; } }

//上传文件

public synchronized boolean upload(String localFileName) throws IOException {

.专业.整理.

下载可编辑

dataSocket = createDataSocket(); int i = localFileName.lastIndexOf(\"/\"); if (i == -1) {

i = localFileName.lastIndexOf(\"\\\\\"); }

String element_1 = \"\"; if (i != -1) {

element_1 = localFileName.substring(i + 1); }

sendCommand(\"STOR \" + element_1); response = readLine(); if (!response.startsWith(\"1\")) { System.out.println(response); }

FileInputStream dataIn = new FileInputStream(localFileName); BufferedOutputStream

dataOut

=

BufferedOutputStream(dataSocket.getOutputStream());

byte[] buffer = new byte[4096]; int bytesRead = 0; do {

bytesRead = dataIn.read(buffer); if (bytesRead != -1) {

.专业.整理.

new

下载可编辑

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

return (response.startsWith(\"226\")); }

//下载文件 RETR

public synchronized boolean download(String remoteFile, String localFile) throws IOException {

dataSocket = createDataSocket(); sendCommand(\"RETR \" + remoteFile);

.专业.整理.

下载可编辑

response = readLine(); if (!response.startsWith(\"1\")) { System.out.println(response); }

System.out.println(localFile); BufferedInputStream

dataIn

=

BufferedInputStream(dataSocket.getInputStream());

new File(localFile).createNewFile();

FileOutputStream fileOut = new FileOutputStream(localFile); byte[] buffer = new byte[4096]; int bytesRead = 0; do {

bytesRead = dataIn.read(buffer); if (bytesRead != -1) {

fileOut.write(buffer, 0, bytesRead); }

} while (bytesRead != -1); fileOut.flush(); fileOut.close(); dataSocket.close(); response = readLine(); if (response.startsWith(\"226\")) {

.专业.整理.

new

下载可编辑

JOptionPane.showConfirmDialog(null, \"下载成功\

\" 下载信息\ JOptionPane.INFORMATION_MESSAGE); }

return (response.startsWith(\"226\")); }

//在远程服务器上创建一个目录

public void mkdir(String dirName) throws IOException {

if (!logined) { login(); }

sendCommand(\"XMKD \" + dirName); // 创建目录 response = readLine();

if (!response.startsWith(\"257\")) { //FTP命令发送过程发生异常 System.out.println( response); } else {

JOptionPane.showConfirmDialog(null, \"创建目录\"+dirName+\" 成功!!\ \" 创建目录\

JOptionPane.INFORMATION_MESSAGE); //成功创建

.专业.整理.

下载可编辑

目录

} }

//删除远程个服务器上的一个目录

public void rmdir(String dirName) throws IOException {

if (!logined) { //如果尚未与服务器连接,则连接服务器 login(); }

sendCommand(\"XRMD \" + dirName); response = readLine();

if (!response.startsWith(\"250\")) { //FTP命令发送过程发生异常 System.out.println(response); } else {

JOptionPane.showConfirmDialog(null, \"删除目录\"+dirName+\" 成功!!\ \" 删除目录\

JOptionPane.INFORMATION_MESSAGE); //成功删除目录

} }

//建立数据连接

.专业.整理.

下载可编辑

private Socket createDataSocket() throws IOException {

sendCommand(\"PASV \"); //采用Pasv模式(被动模式),由服务器返回数据传输的临时端口号,使用该端口进行数据传输

response = readLine();

if (!response.startsWith(\"227\")) { //FTP命令传输过程发生异常 System.out.println(response); }

String clientIp = \"\"; int port = -1;

int opening = response.indexOf('('); //采用Pasv模式服务器返回的信息如“227 Entering Passive Mode (127,0,0,1,,2)”

int closing = response.indexOf(')', opening + 1); //取\"()\"之间的内容:127,0,0,1,,2 ,前4个数字为本机IP地址,转换成127.0.0.1格式

if (closing > 0) { //端口号由后2个数字计算得出:*256+2=16386

String dataLink = response.substring(opening + 1, closing);

StringTokenizer arg = new StringTokenizer(dataLink, \ clientIp = arg.nextToken();

for (int i = 0; i < 3; i++) {

.专业.整理.

下载可编辑

String hIp = arg.nextToken(); clientIp = clientIp + \".\" + hIp; } port

=

Integer.parseInt(arg.nextToken())

*

256

+

Integer.parseInt(arg.nextToken());

}

return new Socket(clientIp, port); }

//用于读取服务器返回的响应信息

private String readLine() throws IOException { String line = inData.readLine(); if (debug) {

System.out.println(\"< \" + line); }

return line; }

//用于发送命令

private void sendCommand(String line) { if (connectSocket == null) {

System.out.println(\"FTP尚未连接\"); 警告

.专业.整理.

//未建立通信链接,抛出异常 下载可编辑

} try {

outData.write(line + \"\\r\\n\"); //发送命令 outData.flush(); //刷新输出流 if (debug) {

System.out.println(\"> \" + line);

}

} catch (Exception e) { connectSocket = null; System.out.println(e); return; } } }

.专业.整理.

//同时控制台输出相应命令信息, 以便分析

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务