up

  1.  
    1. import java.io.IOException;   
    2. import java.io.OutputStream;   
    3.   
    4. import org.apache.commons.io.FileUtils;   
    5. import org.apache.commons.io.IOUtils;   
    6. import org.apache.commons.net.ftp.FTPClient;   
    7. import org.apache.commons.net.ftp.FTPFile;   
    8.   
    9. /**  
    10.  * 使用commons的net包进行ftp链接.  
    11.  * 相关包:commons-net-1.4.1.jar ; commons-io-1.2.jar;jakarta-oro-2.0.8.jar测试通过.可以列出ftp上的文件  
    12.  * 通过把ftp服务器上的文件流连接到outSteam及可以把文件下载到本机的目录..限制如果目录为中文则需要处理.最好使用英文文件名  
    13.  * @author xzgf  
    14.  * email: 
    15.  * 
       
    16.  * @create 2007-2-11  
    17.  *  
    18.  */  
    19. public class ListFtpFile {   
    20.   
    21.     private FTPClient ftpClient = new FTPClient();   
    22.        
    23.     private OutputStream outSteam = null;   
    24.        
    25.     /**  
    26.      * ftp服务器地址  
    27.      */  
    28.     private String hostName = "127.0.0.1";   
    29.        
    30.     /**  
    31.      * 登录名  
    32.      */  
    33.     private String userName = "1";   
    34.        
    35.     /**  
    36.      * 登录密码  
    37.      */  
    38.     private String password = "1";   
    39.        
    40.     /**  
    41.      * 需要访问的远程目录  
    42.      */  
    43.     private String remoteDir = "/ClientTest";   
    44.        
    45.     /**  
    46.      * 登录方法  
    47.      *  
    48.      */  
    49.     private void login() {   
    50.         try {   
    51.             //链接到ftp服务器   
    52.             ftpClient.connect(hostName);   
    53.             System.out.println("连接到ftp服务器:" + hostName + " 成功..开始登录");   
    54.             //登录.用户名 密码   
    55.             ftpClient.login(userName, password);   
    56.             System.out.println("登录成功.");   
    57.                
    58.             FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);   
    59.             System.out.println("目录" + remoteDir + "下的文件:");   
    60.             if(remoteFiles != null) {   
    61.                 for(int i=0;i
    62.                     String name = remoteFiles[i].getName();   
    63.                     long length = remoteFiles[i].getSize();   
    64.                     String readableLength = FileUtils.byteCountToDisplaySize(length);   
    65.                     System.out.println(name + ":/t/t" + readableLength);   
    66.                 }   
    67.             }   
    68.                
    69.         } catch (Exception e) {   
    70.             e.printStackTrace();   
    71.         } finally {   
    72.             //使用IO包关闭流   
    73.             IOUtils.closeQuietly(outSteam);   
    74.             try {   
    75.                 ftpClient.disconnect();   
    76.             } catch (IOException ioe) {   
    77.                 ioe.printStackTrace();   
    78.             }   
    79.         }   
    80.     }   
    81.        
    82.     public static void main(String[] args) {   
    83.         ListFtpFile listFtpfiles = new ListFtpFile();   
    84.         listFtpfiles.login();   
    85.     }   
    86. }   
  2. private JButton getJButton() {
      if (jButton == null) {
       try {
        jButton = new JButton();
        jButton.setBounds(new Rectangle(420, 6, 58, 25));
        jButton.setText("浏览");
        jButton.addActionListener(new java.awt.event.ActionListener() {
         //@SuppressWarnings("deprecation")
         @SuppressWarnings("deprecation")
         public void actionPerformed(java.awt.event.ActionEvent e) {
          System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
          if(e.getSource()==jButton){
           JFrame jf1 = new JFrame();
           FileDialog fd1 = new FileDialog(jf1,"选择要发送的文件",FileDialog.LOAD);
           fd1.show();
           String st = fd1.getDirectory() + fd1.getFile();
           
           if(st!=null){
            try {
             
             jTextField.setText(st);
             
            } catch (Exception e1) {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值