java ftp下载文件

1、使用官方正规的jar

commons-net-1.4.1.jar

jakarta-oro-2.0.8.jar

注意:使用ftp从windows服务器下载文件和从linux服务器下载文件不一样

2、用ftp从linux服务器下载文件

System.out.println(new Date()+"  开始进入ftpDownload定时器");
        
        //ftp服务器登录凭证
        String host=PropertiesManager.getProperty("ftpHost");
        int port=Integer.parseInt(PropertiesManager.getProperty("ftpPort"));
        String user=PropertiesManager.getProperty("ftpUser");
        String password=PropertiesManager.getProperty("ftpPassword");
        
        //获取时间字段信息
        SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String today1 = sdf1.format(date);
        String today = sdf.format(date);
                
        String txtFileDir="/";
        String txtSaveDir="E:/dataCenter/shengzhan/";
        
        //检查本地磁盘目录是否存在txt文件
        boolean flag = isTxtExit(today1,txtSaveDir);
        System.out.println(new Date()+"  判断txt文件是否存在:"+flag);
        FlagUtil.ftpDownloadRunning=true;
        
        //讲txt的下载操作和解析操作分成2个独立的操作进行,排除互相间的干扰
        if(flag==false)//文件不存在进行ftp下载操作
        {
            FTPClient ftp=null;            
            try
            {
                //ftp的数据下载
                ftp=new FTPClient();
                ftp.connect(host, port);   
                ftp.login(user, password);
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                
                //设置linux环境
                FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX);
                ftp.configure(conf);
                
                //判断是否连接成功
                int reply = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply))
                {
                    ftp.disconnect();
                    System.out.println("FTP server refused connection.");
                    return;
                }
                
                //设置访问被动模式
                ftp.setRemoteVerificationEnabled(false);
                ftp.enterLocalPassiveMode();
                
                
                //检索ftp目录下所有的文件,利用时间字符串进行过滤
                boolean dir = ftp.changeWorkingDirectory(txtFileDir);
                if (dir) 
                { 
                    FTPFile[]fs = ftp.listFiles(); 
                    for(FTPFile f:fs)
                    { 
                          if(f.getName().indexOf(today1+"2000")>0)
                          {  
                              System.out.println(new Date()+"  ftpDownload定时器下载txt成功");        
                              File localFile = new File(txtSaveDir+f.getName());    
                              OutputStream ios = new FileOutputStream(localFile);     
                              ftp.retrieveFile(f.getName(), ios);  
                              ios.close();   
                              break;
                           }    
                    }
                }
            } 
            catch (Exception e)
            {
                e.printStackTrace();
                System.out.println(new Date()+"  ftp下载txt文件发生错误");
            }
            finally
            {
                if(ftp != null)  try {ftp.disconnect();} catch (IOException ioe) {}  
            }

3、使用ftp从windows服务器下载文件

public static boolean downFile(  
            String url, //FTP服务器hostname  
            int port,//FTP服务器端口  
            String username, //FTP登录账号  
            String password, //FTP登录密码  
            String remotePath,//FTP服务器上的相对路径   
            String fileName,//要下载的文件名  
            String localPath//下载后保存到本地的路径 

            ) {    
        boolean success = false;    
        FTPClient ftp = new FTPClient();    
        try {    
            int reply;    
            ftp.connect(url, port);    
            //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器     
            ftp.login(username, password);//登录     
            reply = ftp.getReplyCode();    
            if (!FTPReply.isPositiveCompletion(reply)) {    
                ftp.disconnect();    
                return success;    
            }   
            System.out.println("aaa");
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录     
            FTPFile[] fs = ftp.listFiles();  
            
            for(FTPFile ff:fs){ 
             System.out.println("bb" + fs);
             
                if(ff.getName().equals(fileName)){  
                 System.out.println("dd");
                    File localFile = new File(localPath+"/"+ff.getName());    
                    OutputStream is = new FileOutputStream(localFile);     
                    ftp.retrieveFile(ff.getName(), is);  
                    System.out.println("ccc" +ff.getName()+fileName);
                    is.close();    
                }    
            }    
            ftp.logout();    
            success = true;    
        } catch (IOException e) {    
            e.printStackTrace();    
        } finally {    
            if (ftp.isConnected()) {    
                try {    
                    ftp.disconnect();    
                } catch (IOException ioe) {    
                }    
            }    
        }    
        return success;    
    }

 

posted @ 2016-10-07 16:34  JackGIS  阅读(45311)  评论(8编辑  收藏  举报