mainframe.java

来自「java网络高级编程的配套源码,java网络高级编程为清华出版社出版.」· Java 代码 · 共 566 行 · 第 1/2 页

JAVA
566
字号
/*源程序清单12-22*/

package ftp;

import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import borland.jbcl.model.*;
import sun.net.ftp.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class MainFrame extends DecoratedFrame
{
  TextField textField1 = new TextField();
  Button button1 = new Button();
  TextField textField2 = new TextField();
  Label label2 = new Label();
  TextField textField3 = new TextField();
  Label label3 = new Label();
  TextField textField4 = new TextField();
  Button button2 = new Button();
  GroupBox groupBox1 = new GroupBox();
  GridControl gridControl1 = new GridControl();
  Button button3 = new Button();
  Button button4 = new Button();
  Button button5 = new Button();
  Button button6 = new Button();
  PaneLayout paneLayout1 = new PaneLayout();
  Panel panel1 = new Panel();
  PaneLayout paneLayout2 = new PaneLayout();
  Label label1 = new Label();
  GroupBox groupBox2 = new GroupBox();
  GroupBox groupBox3 = new GroupBox();
  PaneLayout paneLayout3 = new PaneLayout();
  TextArea textArea1 = new TextArea();
  PaneLayout paneLayout4 = new PaneLayout();
  PaneLayout paneLayout5 = new PaneLayout();
  List list1 = new List();
  Filer filer1 = new Filer(this,"文件下载到...",Filer.SAVE);
  Filer filer2 = new Filer(this,"上载文件...",Filer.LOAD);
  FtpClient ftpClient=null;
  String ip="";
  String username="";
  String password="";
  String path="";
  Vector file=new Vector();
  Vector size=new Vector();
  Vector date=new Vector();
  Vector type=new Vector();//-rw-r--r--  或是 <DIR>
  String blank[][]=new String[1][3];
  int row=0;
  int column=0;
  Vector downFile=new Vector();

  public MainFrame()
  {
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    this.setTitle("FTP上下载实例");
    button1.setLabel("连接");
    button1.addActionListener(new MainFrame_button1_actionAdapter(this));
    label2.setText("密码:");
    label3.setText("当前目录:");
    textField4.setEditable(false);
    button2.setLabel("上一级目录");
    button2.addActionListener(new MainFrame_button2_actionAdapter(this));
    groupBox1.setLayout(paneLayout1);
    groupBox1.setLabel("文件");
    button3.setLabel("下载");
    button3.addActionListener(new MainFrame_button3_actionAdapter(this));
    button4.setLabel("上载");
    button4.addActionListener(new MainFrame_button4_actionAdapter(this));
    button5.setLabel("断开");
    button5.addActionListener(new MainFrame_button5_actionAdapter(this));
    button6.setLabel("关闭");
    button6.addActionListener(new MainFrame_button6_actionAdapter(this));
    label1.setText("用户名:");
    groupBox2.setLayout(paneLayout5);
    groupBox2.setLabel("上下载队列");
    groupBox3.setLayout(paneLayout4);
    groupBox3.setLabel("上下载信息");
    panel1.setLayout(paneLayout3);
    this.setLayout(paneLayout2);
    gridControl1.setColumnCaptions(new String[] {"目录/文件", "大小", "日期"});
    gridControl1.addSubfocusListener(new MainFrame_gridControl1_subfocusAdapter(this));    
    groupBox1.add(button3, new PaneConstraints("button3", "button3", PaneConstraints.ROOT, 0.5f));
    groupBox1.add(gridControl1, new PaneConstraints("gridControl1", "button3", PaneConstraints.TOP, 0.8208955f));
    groupBox1.add(button4, new PaneConstraints("button4", "button3", PaneConstraints.RIGHT, 0.76666665f));
    groupBox1.add(button5, new PaneConstraints("button5", "button4", PaneConstraints.RIGHT, 0.6847826f));
    groupBox1.add(button6, new PaneConstraints("button6", "button5", PaneConstraints.RIGHT, 0.4948454f));
    this.add(groupBox1, new PaneConstraints("groupBox1", "groupBox1", PaneConstraints.ROOT, 0.5f));
    this.add(label3, new PaneConstraints("label3", "groupBox1", PaneConstraints.TOP, 0.1541096f));
    this.add(panel1, new PaneConstraints("panel1", "groupBox1", PaneConstraints.BOTTOM, 0.34661353f));
    panel1.add(groupBox2, new PaneConstraints("groupBox2", "groupBox2", PaneConstraints.ROOT, 0.5f));
    groupBox2.add(list1, new PaneConstraints("list1", "list1", PaneConstraints.ROOT, 1.0f));
    panel1.add(groupBox3, new PaneConstraints("groupBox3", "groupBox2", PaneConstraints.RIGHT, 0.50344825f));
    groupBox3.add(textArea1, new PaneConstraints("textArea1", "textArea1", PaneConstraints.ROOT, 1.0f));
    this.add(button1, new PaneConstraints("button1", "label3", PaneConstraints.TOP, 0.4666667f));
    this.add(button2, new PaneConstraints("button2", "label3", PaneConstraints.RIGHT, 0.4306569f));
    this.add(textField4, new PaneConstraints("textField4", "label3", PaneConstraints.RIGHT, 0.6680162f));
    this.add(textField2, new PaneConstraints("textField2", "button1", PaneConstraints.RIGHT, 0.80459774f));
    this.add(textField1, new PaneConstraints("textField1", "textField2", PaneConstraints.LEFT, 0.39999998f));
    this.add(label2, new PaneConstraints("label2", "textField2", PaneConstraints.RIGHT, 0.5165877f));
    this.add(textField3, new PaneConstraints("textField3", "label2", PaneConstraints.RIGHT, 0.6513761f));
    this.add(label1, new PaneConstraints("label1", "textField1", PaneConstraints.RIGHT, 0.2877698f));
    textField1.setText("10.15.43.216");
    gridControl1.setReadOnly(true);
    gridControl1.addMouseListener(new MainFrame_gridControl1_mouseAdapter(this));
    textArea1.setEditable(false);
  }

  public static void main(String[] args)
  {
    MainFrame frame=new MainFrame();
    frame.setLocation(100,100);
    frame.setSize(500,400);
    frame.show();
  }

  void button1_actionPerformed(ActionEvent e)
  {
    ip=textField1.getText();
    username=textField2.getText();
    if(username.equals("")) username="anonymous";
    password=textField3.getText();
    if(password.equals("")) password="anonymous";
    if(ip.equals("")) return;
    try
    {
      ConnListen conn=new ConnListen(this,ip,username,password);
      conn.start();
    }
    catch(Exception ee)
    {}
  }

  void button3_actionPerformed(ActionEvent e)
  {
    try
    {
     String str=(String)file.elementAt(row);
     String t=(String)type.elementAt(row);
     if(t.startsWith("-")) //该行为一文件
       download(str,"");
     }catch(Exception ee)
     {
       textArea1.append("文件下载出错!\n");
     }
  }

  void button4_actionPerformed(ActionEvent e)
  {
    upload();
  }
  
  void upload()
  {//上载文件
    try
    {
     filer2.show();
     String directory=filer2.getDirectory();
     if(directory==null||directory.equals("")) return;
     String file=filer2.getFile();
     if(file==null||file.equals("")) return;
     String filename=directory+file;
     UploadFile up=new UploadFile(this,ip,username,password,path,file,filename);
     up.start();
     downFile.addElement(up);
    }catch(Exception e)
    {
      textArea1.append("文件上载出错!\n");
      return;
    }
  }

  void button5_actionPerformed(ActionEvent e)
  {
    disConnection();
  }

  public void disConnection()
  {
    try
    {
      if(downFile.size()!=0)
      {
        MessageDialog mdlg=new MessageDialog(this,"警告对话框","在队列中还有文件正在下载!确定要断开吗?",6);
        mdlg.show();
        if(mdlg.getResult()==MessageDialog.NO) return;
        try
        {
          for(int i=0;i<downFile.size();i++)
          {
            DownloadFile down=(DownloadFile)downFile.elementAt(i);
            down.stop();
          }
        }
        catch(Exception ee)
        {
          System.out.println(ee.toString());
        }
        list1.removeAll();
      }
      gridControl1.setItems(blank);
      file.removeAllElements();
      date.removeAllElements();
      size.removeAllElements();
      type.removeAllElements();
      ftpClient.closeServer();
      textField1.setText("");
      textField2.setText("");
      textField3.setText("");
      textField4.setText("");
      textArea1.append("服务器"+ip+"断开!\n");
      ip="";
      username="";
      password="";
      path="";
    }
    catch(Exception ee)
    {
      textArea1.append("服务器"+ip+"断开连接出错!\n");
    }
  }

  void button6_actionPerformed(ActionEvent e)
  {
    System.exit(0);
  }
  
  void setGridData()
  {     //设置当前路径下的文件
    try
    {
      if(file.size()!=0)
      {
        file.removeAllElements();
        date.removeAllElements();
        size.removeAllElements();
        type.removeAllElements();
      }
      String list="";
      ftpClient.cd("/");
      if(!path.equals(""))
        ftpClient.cd(path);
      textField4.setText(path);
      InputStream is=ftpClient.list();
      int c;
      while ((c=is.read())!=-1)
      {
        String s=(new Character((char) c)).toString();
        list+=s;
        }//end of while
      is.close();
      if(list.equals(""))
        gridControl1.setItems(blank);
      else{
        StringTokenizer st=new StringTokenizer(list,"\n");
        int count=st.countTokens();
        for(int i=0;i<count;i++)
        {
          String s=st.nextToken();
          StringTokenizer sst=new StringTokenizer(s," ");
          c=sst.countTokens();
          String datestr="";
          String filestr="";
          for(int j=0;j<c;j++)
          {
            String ss=sst.nextToken();
            if(j==0) type.addElement(ss);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?