java实现FTP源代码12页_第1页
java实现FTP源代码12页_第2页
java实现FTP源代码12页_第3页
java实现FTP源代码12页_第4页
java实现FTP源代码12页_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

1、package ftpclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.beans.*;import java.io.*;import .TelnetInputStream;import .ftp.*;import java.lang.Object;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeMod

2、el;import javax.swing.JTextField;import javax.swing.JCheckBox;import javax.swing.tree.TreePath;import .TelnetOutputStream;import java.util.Properties;/* * Title: an example * Description:an no * Copyright: Copyright (c) 2002 * Company: home * author liujun * version 1.0 */public class Frame1 extends

3、 JFrame private JPanel contentPane; private JTextField jTextField1 = new JTextField(); private JTextField jTextField2 = new JTextField(); private JTextField jTextField3 = new JTextField(); private JCheckBox jCheckbox1 = new JCheckBox(); FtpClient ftp=null; JTabbedPane jTabbedPane1 = new JTabbedPane(

4、); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); PaneLayout paneLayout1 = new PaneLayout(); XYLayout xYLayout1 = new XYLayout(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JTextField jTextField4 = new JTextField(); JLabel jLabel4 = ne

5、w JLabel(); BorderLayout borderLayout1 = new BorderLayout(); Box box1; JPanel jPanel3 = new JPanel(); JPanel jPanel4 = new JPanel(); XYLayout xYLayout2 = new XYLayout(); BorderLayout borderLayout2 = new BorderLayout(); JScrollPane jScrollPane1 = new JScrollPane(); JButton jButton3 = new JButton(); J

6、Button jButton4 = new JButton(); JScrollPane jScrollPane2 = new JScrollPane(); /定义树节点,模型和树视图 DefaultMutableTreeNode root2 = new DefaultMutableTreeNode(目录中没有文件); DefaultTreeModel model2 = new DefaultTreeModel(root2); JTree jTree2 = new JTree(model2); JButton jButton5 = new JButton(); JLabel statusLab

7、el = new JLabel(); List list1 = new List(); JScrollPane jScrollPane3 = new JScrollPane(); JTextArea jTextArea1 = new JTextArea(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); /Construct the frame public Frame1() ena

8、bleEvents(AWTEvent.WINDOW_EVENT_MASK); try jbInit(); catch(Exception e) e.printStackTrace(); /Component initialization private void jbInit() throws Exception /setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource(Your Icon); contentPane = (JPanel) this.getContentPane(); box1

9、= Box.createVerticalBox(); jTextField1.setText(); contentPane.setLayout(paneLayout1); this.setSize(new Dimension(487, 462); this.setTitle(文件传输系统); jTextField2.setText(anonymous); jTextField3.setText(a); jCheckbox1.setText(使用匿名); jCheckbox1.setSelected(true); jPanel1.setLayout(xYLayout1)

10、; jLabel1.setText(服务器地址:); jLabel2.setText(用户名:); jLabel3.setText(密 码:); jTextField4.setText(21); jLabel4.setText(端口号:); jPanel2.setLayout(borderLayout1); jPanel3.setLayout(xYLayout2); jPanel4.setDebugGraphicsOptions(0); jPanel4.setMinimumSize(new Dimension(160, 18); jPanel4.setPreferredSize(new Dim

11、ension(160, 18); jPanel4.setLayout(borderLayout2); jButton3.setText(文件上载); jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) jButton3_actionPerformed(e); ); jButton4.setText(文件下载); jButton4.addActionListener(new java.awt.event.ActionListener()

12、public void actionPerformed(ActionEvent e) jButton4_actionPerformed(e); ); box1.setEnabled(true); jPanel3.setDebugGraphicsOptions(0); jPanel3.setMinimumSize(new Dimension(400, 240); jPanel3.setPreferredSize(new Dimension(400, 240); jTree2.setToggleClickCount(2); jButton5.setText(选择目录); jButton5.addA

13、ctionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) jButton5_actionPerformed(e); ); statusLabel.setText(没有连接ftp服务器); list1.addMouseListener(new java.awt.event.MouseAdapter() public void mouseClicked(MouseEvent e) list1_mouseClicked(e); ); jTextArea1.setText(

14、); jButton1.setText(连接到服务器); jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) jButton1_actionPerformed(e); ); jButton2.setText(断开服务器连接); jButton2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e)

15、jButton2_actionPerformed(e); ); list1.setMultipleMode(true); jLabel5.setText(FTP服务器目录列表); jLabel6.setText(本地硬盘目录列表); jPanel1.add(jTextField1, new XYConstraints(81, 11, 297, -1); jPanel1.add(jLabel1, new XYConstraints(21, 11, -1, -1); jPanel1.add(statusLabel, new XYConstraints(-2, 370, 484, 29); jPan

16、el1.add(jButton2, new XYConstraints(34, 304, 122, -1); jPanel1.add(jButton1, new XYConstraints(34, 268, 106, 23); jPanel1.add(jLabel4, new XYConstraints(22, 50, -1, -1); jPanel1.add(jTextField4, new XYConstraints(82, 49, 88, -1); jPanel1.add(jCheckbox1, new XYConstraints(22, 84, -1, -1); jPanel1.add

17、(jTextField2, new XYConstraints(80, 124, 86, -1); jPanel1.add(jLabel2, new XYConstraints(20, 124, 58, 21); jPanel1.add(jTextField3, new XYConstraints(80, 165, 87, -1); jPanel1.add(jLabel3, new XYConstraints(20, 166, -1, -1); jPanel2.add(box1, BorderLayout.CENTER); jPanel3.add(jScrollPane2, new XYCon

18、straints(295, 6, 173, 280); jPanel3.add(jScrollPane1, new XYConstraints(7, 6, 194, 281); jPanel3.add(jButton4, new XYConstraints(205, 93, 84, 19); jPanel3.add(jButton3, new XYConstraints(205, 53, 86, 19); jPanel3.add(jButton5, new XYConstraints(205, 136, 85, 19); jPanel3.add(jLabel5, new XYConstrain

19、ts(16, 293, 144, 22); jPanel3.add(jLabel6, new XYConstraints(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

20、(jTextArea1, null); myInit(); jTabbedPane1.add(jPanel2, 文件处理); jTabbedPane1.add(jPanel1, 连接FTP服务器); contentPane.add(jTabbedPane1, new PaneConstraints(jTabbedPane1, jTabbedPane1, PaneConstraints.ROOT, 0.5f); private void myInit() File rootfile= new File(c:);/得到根目录文件 if(rootfile.isFile() rootfile=root

21、file.getParentFile();/如果得到的不是目录,则使用他的目录 DefaultMutableTreeNode rootTree2 =new DefaultMutableTreeNode(rootfile.getPath(); setTree(rootfile.getPath(),rootTree2);/遍历目录树 model2.setRoot(rootTree2);/设置模型的根节点 model2.reload();/重新构造树视图 if(ftp!=null) ReloadList(); jTextArea1.append(ftp.welcomeMsg); /-/ 浏览程序:

22、private void ReloadList()/ 作用:清空目录列表,调用List()方法获取文件列表。 private void ReloadList() StringBuffer buf=new StringBuffer(); int ch; /清空目录列表 list1.removeAll(); try /调用List()方法得到目录表 TelnetInputStream t = ftp.list(); t.setStickyCRLF(true); /分解TelnetInputStream数据流 while ( (ch = t.read() = 0) if (ch = n) /向列表框

23、添加分解得到的目录和文件 list1.add(getDIR(buf.toString(); buf.setLength(0); else buf.append( (char) ch); /完成后关闭TelnetInputStream数据流 t.close(); catch (IOException ex) ex.printStackTrace(); /刷新列表框的内容 list1.validate(); /Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e

24、) cessWindowEvent(e); if (e.getID() = WindowEvent.WINDOW_CLOSING) System.exit(0); public String getDIR(String path) String DIRName; int ch; /分解字符串得到目录和文件名 int begin=55; DIRName=path.substring(begin).trim(); return DIRName; /-/ 浏览程序: public void setTree(String Path,DefaultMutableTreeNode Tre

25、ePath)/ 作用:构造jTree对象的目录树 public void setTree(String Path,DefaultMutableTreeNode TreePath) /递归遍历目录树 try File source = new File(Path);/得到源文件路径 String fileName=source.list();/的到该目录下文件列表 for(int i=0;i0) /选择列表框中单击选中的内容 String dir=list1.getSelectedItem().trim(); try /如果是目录,则进入目录 ftp.cd(dir); /列表框需要重新刷新 Re

26、loadList(); jTextArea1.append(进入目录: ); jTextArea1.append(dir); jTextArea1.append(n); catch (IOException ex) /否则,表明点击的内容是ftp上的一个文件 jTextArea1.append(dir); jTextArea1.append(:是一个文件n); /-/ 登录程序: void jButton1_actionPerformed(ActionEvent e)/ 作用:登录连接到FTP服务器。 void jButton1_actionPerformed(ActionEvent e) i

27、nt ch; String hostname=jTextField1.getText(); /如果已经打开了FTP服务器,则先关闭FTP文件服务器 try if (ftp!=null) ftp.closeServer(); catch (IOException ex) ex.printStackTrace(); /连接到服务器 try statusLabel.setText(正在连接,请等待.); ftp= new FtpClient(hostname); /登录Ftp服务器 ftp.login(jTextField2.getText(),jTextField3.getText(); /使用二

28、进制协议 ftp.binary(); catch(FtpLoginException ex) /没有主机的登录权限 statusLabel.setText(无权限与主机:+hostname+连接!); catch (IOException ex) /连接主机失败 statusLabel.setText(连接主机:+hostname+失败!); catch(SecurityException ex) /用户或者密码可能不对 statusLabel.setText(用户或者密码可能不对,无权限与主机:+hostname+连接!); /连接成功后的显示 statusLabel.setText(连接主

29、机:+hostname+成功!); /列表框需要重新刷新 ReloadList(); void jButton2_actionPerformed(ActionEvent e) try /关闭FTP服务器连接 if (ftp!=null) ftp.closeServer(); list1.removeAll(); catch (IOException ex) ex.printStackTrace(); statusLabel.setText(断开主机连接成功!); void jButton3_actionPerformed(ActionEvent e) /上载本地文件到ftp服务器中 if(jT

30、ree2.isSelectionEmpty() ) jTextArea1.append(没有选择本地文件上载n); else File file1=new File(conPath(); /判断是否是文件,如果是目录则不与操作,以后的程序可能会考虑添加目录的操作 if(file1.isFile() String filename=jTree2.getSelectionPath().getLastPathComponent().toString(); int ch; try File localFile = new File(conPath() ); RandomAccessFile sendF

31、ile = new RandomAccessFile( conPath() , r); /上载文件到ftp服务器中 sendFile.seek(0); TelnetOutputStream outs = ftp.put(filename); DataOutputStream outputs = new DataOutputStream(outs); /上载处理中. while (sendFile.getFilePointer() sendFile.length() ch = sendFile.read(); outputs.write(ch); /上载完成,关闭输入输出流 outs.close(); sendFile.close(); catch (IOExcepti

温馨提示

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

评论

0/150

提交评论