java实现FTP源代码.docx
- 文档编号:9243115
- 上传时间:2023-05-17
- 格式:DOCX
- 页数:19
- 大小:19.28KB
java实现FTP源代码.docx
《java实现FTP源代码.docx》由会员分享,可在线阅读,更多相关《java实现FTP源代码.docx(19页珍藏版)》请在冰点文库上搜索。
java实现FTP源代码
packageftpclient;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importcom.borland.jbcl.layout.*;
importjava.beans.*;
importjava.io.*;
import.TelnetInputStream;
import.ftp.*;
importjava.lang.Object;
importjavax.swing.tree.DefaultMutableTreeNode;
importjavax.swing.tree.DefaultTreeModel;
importjavax.swing.JTextField;
importjavax.swing.JCheckBox;
importjavax.swing.tree.TreePath;
import.TelnetOutputStream;
importjava.util.Properties;
/**
*
Title:
anexample
*
Description:
anno
*
Copyright:
Copyright(c)2002
*
Company:
home
*@authorliujun
*@version1.0
*/
publicclassFrame1extendsJFrame{
privateJPanelcontentPane;
privateJTextFieldjTextField1=newJTextField();
privateJTextFieldjTextField2=newJTextField();
privateJTextFieldjTextField3=newJTextField();
privateJCheckBoxjCheckbox1=newJCheckBox();
FtpClientftp=null;
JTabbedPanejTabbedPane1=newJTabbedPane();
JPaneljPanel1=newJPanel();
JPaneljPanel2=newJPanel();
PaneLayoutpaneLayout1=newPaneLayout();
XYLayoutxYLayout1=newXYLayout();
JLabeljLabel1=newJLabel();
JLabeljLabel2=newJLabel();
JLabeljLabel3=newJLabel();
JTextFieldjTextField4=newJTextField();
JLabeljLabel4=newJLabel();
BorderLayoutborderLayout1=newBorderLayout();
Boxbox1;
JPaneljPanel3=newJPanel();
JPaneljPanel4=newJPanel();
XYLayoutxYLayout2=newXYLayout();
BorderLayoutborderLayout2=newBorderLayout();
JScrollPanejScrollPane1=newJScrollPane();
JButtonjButton3=newJButton();
JButtonjButton4=newJButton();
JScrollPanejScrollPane2=newJScrollPane();
//定义树节点,模型和树视图
DefaultMutableTreeNoderoot2=newDefaultMutableTreeNode("目录中没有文件");
DefaultTreeModelmodel2=newDefaultTreeModel(root2);
JTreejTree2=newJTree(model2);
JButtonjButton5=newJButton();
JLabelstatusLabel=newJLabel();
Listlist1=newList();
JScrollPanejScrollPane3=newJScrollPane();
JTextAreajTextArea1=newJTextArea();
JButtonjButton1=newJButton();
JButtonjButton2=newJButton();
JLabeljLabel5=newJLabel();
JLabeljLabel6=newJLabel();
//Constructtheframe
publicFrame1(){
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try{
jbInit();
}
catch(Exceptione){
e.printStackTrace();
}
}
//Componentinitialization
privatevoidjbInit()throwsException{
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[YourIcon]")));
contentPane=(JPanel)this.getContentPane();
box1=Box.createVerticalBox();
jTextField1.setText("192.168.101.2");
contentPane.setLayout(paneLayout1);
this.setSize(newDimension(487,462));
this.setTitle("文件传输系统");
jTextField2.setText("anonymous");
jTextField3.setText("a");
jCheckbox1.setText("使用匿名");
jCheckbox1.setSelected(true);
jPanel1.setLayout(xYLayout1);
jLabel1.setText("服务器地址:
");
jLabel2.setText("用户名:
");
jLabel3.setText("密码:
");
jTextField4.setText("21");
jLabel4.setText("端口号:
");
jPanel2.setLayout(borderLayout1);
jPanel3.setLayout(xYLayout2);
jPanel4.setDebugGraphicsOptions(0);
jPanel4.setMinimumSize(newDimension(160,18));
jPanel4.setPreferredSize(newDimension(160,18));
jPanel4.setLayout(borderLayout2);
jButton3.setText("文件上载");
jButton3.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
jButton3_actionPerformed(e);
}
});
jButton4.setText("文件下载");
jButton4.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
jButton4_actionPerformed(e);
}
});
box1.setEnabled(true);
jPanel3.setDebugGraphicsOptions(0);
jPanel3.setMinimumSize(newDimension(400,240));
jPanel3.setPreferredSize(newDimension(400,240));
jTree2.setToggleClickCount
(2);
jButton5.setText("选择目录");
jButton5.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
jButton5_actionPerformed(e);
}
});
statusLabel.setText("没有连接ftp服务器");
list1.addMouseListener(newjava.awt.event.MouseAdapter(){
publicvoidmouseClicked(MouseEvente){
list1_mouseClicked(e);
}
});
jTextArea1.setText("");
jButton1.setText("连接到服务器");
jButton1.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
jButton1_actionPerformed(e);
}
});
jButton2.setText("断开服务器连接");
jButton2.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(ActionEvente){
jButton2_actionPerformed(e);
}
});
list1.setMultipleMode(true);
jLabel5.setText("^^FTP服务器目录列表^^");
jLabel6.setText("^^本地硬盘目录列表^^");
jPanel1.add(jTextField1,newXYConstraints(81,11,297,-1));
jPanel1.add(jLabel1,newXYConstraints(21,11,-1,-1));
jPanel1.add(statusLabel,newXYConstraints(-2,370,484,29));
jPanel1.add(jButton2,newXYConstraints(34,304,122,-1));
jPanel1.add(jButton1,newXYConstraints(34,268,106,23));
jPanel1.add(jLabel4,newXYConstraints(22,50,-1,-1));
jPanel1.add(jTextField4,newXYConstraints(82,49,88,-1));
jPanel1.add(jCheckbox1,newXYConstraints(22,84,-1,-1));
jPanel1.add(jTextField2,newXYConstraints(80,124,86,-1));
jPanel1.add(jLabel2,newXYConstraints(20,124,58,21));
jPanel1.add(jTextField3,newXYConstraints(80,165,87,-1));
jPanel1.add(jLabel3,newXYConstraints(20,166,-1,-1));
jPanel2.add(box1,BorderLayout.CENTER);
jPanel3.add(jScrollPane2,newXYConstraints(295,6,173,280));
jPanel3.add(jScrollPane1,newXYConstraints(7,6,194,281));
jPanel3.add(jButton4,newXYConstraints(205,93,84,19));
jPanel3.add(jButton3,newXYConstraints(205,53,86,19));
jPanel3.add(jButton5,newXYConstraints(205,136,85,19));
jPanel3.add(jLabel5,newXYConstraints(16,293,144,22));
jPanel3.add(jLabel6,newXYConstraints(314,293,144,22));
jScrollPane1.getViewport().add(list1,null);
jScrollPane2.getViewport().add(jTree2,null);
box1.add(jPanel3,null);
box1.add(jPanel4,null);
jPanel4.add(jScrollPane3,BorderLayout.CENTER);
jScrollPane3.getViewport().add(jTextArea1,null);
myInit();
jTabbedPane1.add(jPanel2,"文件处理");
jTabbedPane1.add(jPanel1,"连接FTP服务器");
contentPane.add(jTabbedPane1,newPaneConstraints("jTabbedPane1","jTabbedPane1",PaneConstraints.ROOT,0.5f));
}
privatevoidmyInit(){
Filerootfile=newFile("c:
\\");//得到根目录文件
if(rootfile.isFile())rootfile=rootfile.getParentFile();//如果得到的不是目录,则使用他的目录
DefaultMutableTreeNoderootTree2=newDefaultMutableTreeNode(rootfile.getPath());
setTree(rootfile.getPath(),rootTree2);//遍历目录树
model2.setRoot(rootTree2);//设置模型的根节点
model2.reload();//重新构造树视图
if(ftp!
=null){
ReloadList();
jTextArea1.append(ftp.welcomeMsg);
}
}
//---------------------------------------------------------------------------
//浏览程序:
privatevoidReloadList()
//作用:
清空目录列表,调用List()方法获取文件列表。
privatevoidReloadList(){
StringBufferbuf=newStringBuffer();
intch;
//清空目录列表
list1.removeAll();
try{
//调用List()方法得到目录表
TelnetInputStreamt=ftp.list();
t.setStickyCRLF(true);
//分解TelnetInputStream数据流
while((ch=t.read())>=0){
if(ch=='\n'){
//向列表框添加分解得到的目录和文件
list1.add(getDIR(buf.toString()));
buf.setLength(0);
}
else{
buf.append((char)ch);
}
}
//完成后关闭TelnetInputStream数据流
t.close();
}
catch(IOExceptionex){
ex.printStackTrace();
}
//刷新列表框的内容
list1.validate();
}
//Overriddensowecanexitwhenwindowisclosed
protectedvoidprocessWindowEvent(WindowEvente){
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
publicStringgetDIR(Stringpath){
StringDIRName;
intch;
//分解字符串得到目录和文件名
intbegin=55;
DIRName=path.substring(begin).trim();
returnDIRName;
}
//---------------------------------------------------------------------------
//浏览程序:
publicvoidsetTree(StringPath,DefaultMutableTreeNodeTreePath)
//作用:
构造jTree对象的目录树
publicvoidsetTree(StringPath,DefaultMutableTreeNodeTreePath){
//递归遍历目录树
try{
Filesource=newFile(Path);//得到源文件路径
String[]fileName=source.list();//的到该目录下文件列表
for(inti=0;i FilefileMem=newFile(source.getPath(),fileName[i]); if(fileMem.isDirectory()){//是目录则添加目录 DefaultMutableTreeNodeTreeMem=newDefaultMutableTreeNode(fileMem.getName()); TreePath.add(TreeMem);//在树中添加目录 setTree(fileMem.getPath(),TreeMem);//递归遍历目录树 }//endif else{//是文件则添加节点 DefaultMutableTreeNodeTreeMem=newDefaultMutableTreeNode(fileMem.getName()); TreePath.add(TreeMem);//添加节点 } }//endfor }//Endtry catch(Exceptionei) { JOptionPane.showMessageDialog(this,"系统出错: "+ei);//显示提示信息。 } } voidJCheckbox1_propertyChange(PropertyChangeEvente){ if(jCheckbox1.isSelected()){ jTextField1.setText("anonymous"); jTextField2.setText("a"); } } //--------------------------------------------------------------------------- //浏览程序: voidjButton5_actionPerformed(ActionEvente) //作用: 选择需要的本地文件目录 voidjButton5_actionPerformed(ActionEvente){ JFileChooserJFileCh=newJFileChooser();//创建文件对话框 JFileCh.setFileSelectionMode(JFileCh.DIRECTORIES_ONLY);//只选择目录 intreturnVal=JFileCh.showOpenDialog(this);//显示文件对话框 if(returnVal==JFileChooser.APPROVE_OPTION){ Fileroot2=JFileCh.getSelectedFile();//得到根目录文件 if(root2.isFile())root2=root2.getParentFile();//如果得到的不是目录,则使用他的目录 DefaultMutableTreeNoderootTree2=newDefaultMutableTreeNode(root2.getPath()); setTree(root2.getPath(),rootTree2);//遍历目录树 model2.setRoot(rootTree2);//设置模型的根节点 model2.reload();//重新构造树视图 }//endif else JOptionPane.showMessageDialog(this,"没有选择文件");//显示提示信息。 } //--------------------------------------------------------------------------- //浏览程序: voidlist1_mouseClicked(MouseEvente) //作用:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实现 FTP 源代码