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;    
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM