/*
 * Main.java
 *
 * Created on 2007年12月19日, 下午8:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import sun.net.ftp.*;
import sun.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class UseFTP extends JFrame implements   ActionListener{FtpClient aftp;
DataOutputStream outputs;                   //数据输出流
TelnetInputStream ins;                     //输入流
TelnetOutputStream outs;                   //输出流
JTextArea lsArea=new JTextArea(40,100);
JLabel lblPrompt =new JLabel("没有连接主机",Label.LEFT);
JLabel lblHost =new JLabel("主机名:");
JLabel lblUID =new JLabel("用户名:");
JLabel lblPWD =new JLabel("密码:");
JLabel lblFile =new JLabel("要下载的文件名:");
JLabel lblDir =new JLabel("存放文件的路径:");
JButton btnConn=new JButton("连接");
JButton btnClose=new JButton("断开");
JButton btnDown=new JButton("下载");
JButton btnUp=new JButton("上传");
JTextField txtHost=new JTextField(34);
JTextField txtUID=new JTextField(14);
JPasswordField txtPWD=new JPasswordField(14);
JTextField txtDir=new JTextField(29);
JTextField txtFile=new JTextField(29);
int ch;
String a="没有连接主机";
String hostname="";public  UseFTP(){
    JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel p3=new JPanel(new FlowLayout(FlowLayout.LEFT));
    btnClose.enable(false);
    p1.add(lblPrompt);
    p2.add(lblHost);p2.add(txtHost);p2.add(btnConn);
    p3.add(lblUID);p3.add(txtUID);p3.add(lblPWD);
    p3.add(txtPWD);p3.add(btnClose);
    
    JPanel top =new JPanel(new GridLayout(3,1));
    top.add(p1);top.add(p2);top.add(p3);
    lsArea.setEditable(false);
    JScrollPane jsp=new JScrollPane(lsArea);
    JPanel bottom=new JPanel(new GridLayout(2,1));
    JPanel p4=new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel p5=new JPanel(new FlowLayout(FlowLayout.LEFT));
    p4.add(lblFile);p4.add(txtFile);p4.add(btnUp);
    p5.add(lblDir);p5.add(txtDir);p5.add(btnDown);
    bottom.add(p4);bottom.add(p5);
    this.getContentPane().add(top,"North");
    this.getContentPane().add(jsp,"Center");
    this.getContentPane().add(bottom,"South");
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    try{aftp.closeServer();
    }catch(Exception ee){}
    dispose();
    System.exit(0);
    }
    });
    setSize(520,400);
    setTitle("FTP客户端");
    setVisible(true);
}
public boolean connect(String hostname,String uid,String pwd){                     //连接函数
this.hostname =hostname;
lblPrompt.setText("正在连接,请等待……");
try{
    aftp=new FtpClient(hostname);
    aftp.login(uid,pwd);
    aftp.binary();
    showFileContents();
}
catch(FtpLoginException e){
lblPrompt.setText("无权限与主机:"+hostname+"连接!");
return false;
}
catch(IOException e){
lblPrompt.setText("连接主机:"+hostname+"失败!");
return false;
}
catch(SecurityException e){
lblPrompt.setText("无权限与主机:"+hostname+"连接!");
return false;
}
lblPrompt.setText("连接主机:"+hostname+"成功!");
return true;
}
public void actionPerformed(ActionEvent e){                                    //点击按钮相应的响应
if(e.getSource()==btnConn){                                                    //连接按钮
lblPrompt.setText("正在连接,请等待……");
  if(connect(txtHost.getText(),txtUID.getText(),txtPWD.getText())){
  btnConn.setEnabled(false);
  btnClose.setEnabled(true);
  }
}
else if(e.getSource()==btnClose){                                              //断开按钮
try{
  aftp.closeServer();
  }
catch(IOException ee) {}
btnConn.enable(true);
btnClose.enable(false);
lblPrompt.setText("与主机"+hostname+" 连接已断开");
}
else if(e.getSource()==btnDown){                                              //下载按钮
downLoad(txtDir.getText(),txtFile.getText());
}
else if(e.getSource()==btnUp){                                                //上传按钮
  sendFile(txtDir.getText()+"//"+txtFile.getText());
}
}
public void sendFile(String filepathname){                                    //上传文件;
    if (aftp!=null){
    lblPrompt.setText("正在粘贴文件,请耐心等待……");
    String contentperline;
    try{
      String fg=new String("\\");
      int index=filepathname.lastIndexOf(fg);
      String filename=filepathname.substring(index+1);
      File localFile=new File(filepathname);
      RandomAccessFile file =new RandomAccessFile(filepathname,"r");
      file.seek(0);
      outs =aftp.put(filename);
      outputs=new DataOutputStream(outs);
      while(file.getFilePointer()<file.length()){
      ch=file.read();
      outputs.write(ch);
     }
     outs.close();
     file.close();
      lblPrompt.setText("粘贴成功!");
      showFileContents();
      return;      
    }
    catch(IOException e){}
    }
    lblPrompt.setText("粘贴失败!");
  }
  public void showFileContents() {                                   //显示目录和文件名
      StringBuffer buf =new StringBuffer();
      lsArea.setText("");
      try{
        ins=aftp.list();                              //获得所有文件和目录的输入数据流;
        while((ch=ins.read())>=0){
        buf.append((char)ch);
        }
        lsArea.append(buf.toString());
        ins.close();
      }catch(IOException e){}
  }
    public void downLoad(String dir,String fname){            //下载文件
    StringBuffer buf =new StringBuffer();
    buf.setLength(0);
    try{
    File f=new File(new File(dir),fname);   //通过路径和文件名够着一个文件
     RandomAccessFile file=new RandomAccessFile(f,"rw");
     ins=aftp.get(fname);                //得到所选文件的输入流
     while((ch=ins.read())>=0){          //读取数据流
    buf.append((char)ch);
    }
    file.writeBytes(buf.toString());   //将缓冲区的数据以字符串形式写入文件
    file.close();                        //关闭文件
    lblPrompt.setText("下载成功!");
    return;
    }  catch(Exception e){}
    lblPrompt.setText("下载成功!");
   }
   public static void main(String args[]){
   Font font=new Font("JFrame",Font.PLAIN,14);   //定义字体
   Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
   while(keys.hasMoreElements()){
   Object key=keys.nextElement();
   if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
    }  
 new UseFTP();
  } 
}