zoukankan      html  css  js  c++  java
  • 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;    
        }
  • 相关阅读:
    6.12白书第五章图论总结——司雨寒
    【司雨寒】最短路专题总结
    十三 十四周总结
    13周总结--苏康
    JuneX_13
    12总结--苏康
    十二周总结
    每周总结(5.30)——何贤拓
    进阶实验4-3.1 家谱处理 (30分)
    进阶实验2-3.1 海盗分赃 (25分)--递推
  • 原文地址:https://www.cnblogs.com/tiandi/p/5936218.html
Copyright © 2011-2022 走看看