package ftpClient;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import sun.net.*;
import sun.net.ftp.*;
import javax.swing.*;
 
public class FtpClientFrame extends JFrame{
JPanel contentPane;
Label LabelHost=new Label(),
      LabelPrompt=new Label(),
      LabelUser=new Label(),
      LabelPassword=new Label(),
      LabelDir=new Label(),
      LabelFile=new Label();
TextField TextHost=new TextField(),
          TextUser=new TextField(),
          TextPassword=new TextField(),
          TextFile=new TextField(),
          TextDir=new TextField();
Button ButtonLink=new Button("连接"),
       ButtonDisconnect=new Button("断开"),
       ButtonDownLoad=new Button("下载");
TextArea TextAreafile=new TextArea();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
 
FtpClient MyFtp=null;
TelnetInputStream inStream=null;
public FtpClientFrame()
{
try{
JInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void JInit() throws Exception
{
contentPane=(JPanel)this.getContentPane();
contentPane.setLayout(null);
LabelPrompt.setBounds(new Rectangle(25,6,180,22));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
LabelHost.setText("主机名");
LabelHost.setBounds(new Rectangle(25, 38,50,22));
TextHost.setBounds(new Rectangle(78,38,280,22)); 
LabelPrompt.setText("连接");
LabelUser.setText("用户名");
LabelUser.setBounds(new Rectangle(25,70,50,22)); 
TextUser.setBounds(new Rectangle(78,70,114,22));
LabelPassword.setText("密码");
LabelPassword.setBounds(new Rectangle(205,70,37,22)); 
TextPassword.setBounds(new Rectangle(244,70,114,22));
TextPassword.setEchoChar('*');
ButtonLink.setBounds(new Rectangle(375,38,70,22));
ButtonLink.setEnabled(true);
ButtonDisconnect.setBounds(new Rectangle(375,70,70,22));
ButtonDisconnect.setEnabled(false);
TextAreafile.setBounds(new Rectangle(25,135,420,235));
TextAreafile.setEditable(false);
LabelDir.setText("下载文件"); 
LabelDir.setBounds(new Rectangle(25,380,100,22));
TextDir.setBounds(new Rectangle(128,380,230,22));
LabelFile.setText("存放路径");
LabelFile.setBounds(new Rectangle(25,412,100,22));
TextFile.setBounds(new Rectangle(128,412,230,22));
ButtonDownLoad.setBounds(new Rectangle(375,412,70,22));
ButtonLink.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ButtonLink_actionPerformed(e);

}
});
ButtonDisconnect.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ButtonDisconnect_actionPerFormed(e);
}
}
);
ButtonDownLoad.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
buttonDownLoad_actionPerformed(e);
}
}
);
contentPane.add(LabelPrompt);
contentPane.add(LabelHost);
contentPane.add(TextHost);
contentPane.add(ButtonLink);
contentPane.add(LabelUser);
contentPane.add(TextUser);
contentPane.add(LabelPassword);
contentPane.add(TextPassword);
contentPane.add(ButtonDisconnect);
contentPane.add(TextAreafile);

contentPane.add(LabelFile);
contentPane.add(TextFile);
contentPane.add(LabelDir);
contentPane.add(TextDir);
contentPane.add(ButtonDownLoad);
  
this.setSize(new Dimension(480,485));
this.setResizable(false);
this.setTitle("FTP客户端");
this.setVisible(true);

}
void ButtonLink_actionPerformed(ActionEvent e)
{
String hostname=TextHost.getText();
LabelPrompt.setText("正在连接,请等待.......");
try{
MyFtp=new FtpClient(hostname);
MyFtp.login(TextUser.getText(),TextPassword.getText());
MyFtp.binary();
showFileConntents();
}
catch(FtpLoginException e1)
{
String strPrompt="用户名密码错误";
LabelPrompt.setText(strPrompt);
}
catch(IOException e1)
{
String strPrompt="连接主机"+hostname+"失败";
LabelPrompt.setText(strPrompt);
}
catch(SecurityException e1)
{
String strPrompt="无权限与主机:"+hostname+"连接";
     LabelPrompt.setText(strPrompt);
}
LabelPrompt.setText("连接主机"+TextHost.getText()+"成功!");
ButtonDisconnect.setEnabled(true);
ButtonDownLoad.setEnabled(true);
ButtonLink.setEnabled(false);

}
    void ButtonDisconnect_actionPerFormed(ActionEvent e)
    {
     try{
     MyFtp.closeServer();
     TextAreafile.setText("");
     LabelPrompt.setText("与主机断开连接!");
     }
     catch(Exception e2)
     {
     e2.getStackTrace();
     }
     ButtonDisconnect.setEnabled(false);
     ButtonDownLoad.setEnabled(false);
     ButtonLink.setEnabled(true);
    }
    public void showFileConntents()
    {
     int ch;
     StringBuffer buf=new StringBuffer();
     try{
     inStream=MyFtp.list();
     while((ch=inStream.read())>=0)
     {
     buf.append((char)ch);
     }
     TextAreafile.append(buf.toString());
     inStream.close();
     }
     catch(Exception e2)
     {
     e2.getStackTrace();
     }
    
    }
    void buttonDownLoad_actionPerformed(ActionEvent e)
    {
     int ch;
     StringBuffer buf=new StringBuffer();
     buf.setLength(0);
     try{
     File dir=new File(TextDir.getText());
     File f=new File(dir,TextFile.getText());
     RandomAccessFile file=new RandomAccessFile(f,"rw");
     inStream=MyFtp.get(TextFile.getText());
     while((ch=inStream.read())>=0)
     {
     buf.append((char)ch);
     }
     file.writeBytes(buf.toString());
     file.close();
     JOptionPane msg=new JOptionPane();
     JOptionPane.showMessageDialog(FtpClientFrame.this,"下载成功","下载成功!",1);
     }
     catch(Exception e2)
     {
     e2.getStackTrace();
     }
    }
    protected void processWindowEvent(WindowEvent e)
    {
     super.processWindowEvent(e);
     if(e.getID()==WindowEvent.WINDOW_CLOSING){
     try{
     if(inStream!=null)
     inStream.close();
     }
     catch(IOException e1)
     {
     System.out.println("Error:"+e1);
     }
     System.exit(0);
    
     }
    
    }
    public static void main(String[] args)
    {
     new FtpClientFrame();
    }
}
为什么不能显示出ftp的目录???????

解决方案 »

  1.   

    public void showFileConntents()
        {
         int ch;
         StringBuffer buf=new StringBuffer();
         try{
         inStream=MyFtp.list();
         while((ch=inStream.read())>=0)
         {
         buf.append((char)ch);
         }
         TextAreafile.append(buf.toString());
         inStream.close();
         }
         catch(Exception e2)
         {
         e2.getStackTrace();
         }
        
        }
    主要是这个方法有什么错误吗?
    知道的能告诉我一下吗?