java实现通过ftp下载文件

  public void downloadFtpFiles() throws ParseException {
        String localDirs = PropertiesUtils.getProperties(Constants.LOCAL_DIR);
        String ftplocalDir = PropertiesUtils.getProperties(Constants.ftplocal_dir);
        FTPClient ftp=ftpConfig.connectFtpServer();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        String time= simpleDateFormat.format(new Date());
        String dirName=DateUtil.getBeforeDayYMD(time);
        try {
            //获取ftp目录下的文件夹名
            ftp.changeWorkingDirectory(ftp上路径);
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff:fs) {
                String fileName = ff.getName();
                boolean flag = false;
                // 切换目录,若当前是目录则返回true,否则返回false。
                flag = ftp.changeWorkingDirectory(fileName);
                if (flag) {
                    ftp.changeToParentDirectory();
                }
                if (flag) {
                    String localDir = 本地地址+ fileName;
                    logger.debug("开始下载" + fileName+dirName + "目录,本地目录为" + localDir);
                    File temp = new File(localDir);
                    if (!temp.exists()) {
                        temp.mkdirs();
                    }
                    //进入下载主目录。
                    ftp.changeWorkingDirectory(fileName);
//                                for (String dirName : Dirs) {
                    //遍历需要下载的文件夹 1、切换ftp远程目录,进入需要下载文件的目录.
                    boolean fl = ftp.changeWorkingDirectory(dirName);
                    if (fl) {
                        String localSubDirName = 本地地址+ File.separator + dirName;
                        File localSubDir = new File(localSubDirName);
                        if (!localSubDir.exists()) {
                            //当本地目录不存在时,创建本地目录.
                            localSubDir.mkdirs();
                        }
                        //获取该目录文件列表.
                        String[] fileNames = ftp.listNames();
                        if (null != fileNames && fileNames.length > 0) {
                            for (String fn : fileNames) {
                                File localFile = new File(localSubDirName + File.separator + fn);
                                if (!localFile.exists()) {
                                    FileOutputStream fos = new FileOutputStream(localFile);
                                    ftp.retrieveFile(fn, fos);
                                }
                            }
//                                            updateNotifyDate(fileName, dirName);
                            ftp.changeToParentDirectory();
                        }
                    }
//                                }
                    ftp.changeToParentDirectory();
                    logger.info(fileName+dirName + "下载成功!");
                }
//                        }
//                    }
//                }
            }
            ftp.logout();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("异常!"+e);
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    logger.error("io异常!"+ioe);
                }
            }
        }
    }

从FTP服务器下载指定文件夹中的所有最近更新的csv文件

	/** 
	 * Description: 从FTP服务器下载指定文件夹中的所有最近更新的csv文件 
	 * @param url FTP服务器hostname 
	 * @param port FTP服务器端口 
	 * @param username FTP登录账号 
	 * @param password FTP登录密码 
	 * @param remotePath FTP服务器上的相对路径 
	 * @param localPath 下载后保存到本地的路径 
	 * @param dateStr 下载的是哪一天的数据
	 * @return 下载的文件数
	 */  
	public static int downFileByPath(String url, int port, String username, String password, String remotePath, String localPath, String dateStr) {
		int filesSize = 0;
	    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 filesSize;  
	        }  
	        boolean result = ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录  
	        if(!result){ //转移到指定目录失败
	        	logger.error("找不到指定的ftp服务器目录:"+remotePath);
	        	return filesSize;  
	        }
	        
	        FTPFile[] fs = ftp.listFiles();
	        String ffName = "";
	        for(FTPFile ff:fs){
	        	ffName = ff.getName();
				if(ffName.endsWith(".csv") && ffName.contains(dateStr)){
					File localFile = new File(localPath+"/"+ffName);
	                OutputStream is = null;
	                File path = new File(localPath);
	                if(!((new File(localPath)).exists())){
	                	path.mkdirs();
	                }
	                is = new FileOutputStream(localFile);
	                ftp.retrieveFile(ffName, is);  
	                logger.debug(ffName+" 下载成功!");
	                filesSize++;
				}
	         }  
	        ftp.logout();  
	    } catch (IOException e) {
	        //e.printStackTrace();  
	    	logger.error("异常!"+e);
	    } finally {  
	        if (ftp.isConnected()) {  
	            try {  
	                ftp.disconnect();  
	            } catch (IOException ioe) {
	            	logger.error("io异常!"+ioe);
	            }  
	        }  
	    }  
	    return filesSize;
	}
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EviaHp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值