zoukankan      html  css  js  c++  java
  • 关于使用FTPClient下载文件,获取文件列表为空的情况。以及报错Host attempting data connection ip address is not same as server

    1. 获取ftp目录下文件列表为空的情况。

               1 FTPFile[] fs = ftp.listFiles(remotePath); 

               这里代码获取fs的长度为0.很苦恼。

               奇怪的是我在我本机的windows系统下搭建的ftp服务器正常。但在linux下面却不行。

               只需要在加入下一行代码即可。 

    //这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据
    ftp.enterLocalPassiveMode();

         2.下载文件报Host attempting data connection ip address is not same as server

            这个错误只有当我获取文件的地址为内网ip才会出现这种情况,需要修改一个设置。setRemoteVerificationEnabled

            服务器会获取自身Ip地址和提交的host进行匹配,当不一致时报出以上异常。

            将此参数设置为false即可。默认为true。

    ftp.setRemoteVerificationEnabled(false);


    ftp下载文件的代码如下:

    /**
         * /** Description: 从FTP服务器下载文件 *
         * @param url   FTP服务器hostname
         * @param port  FTP服务器端口
         * @param username  FTP登录账号
         * @param password FTP登录密码
         * @param remotePath FTP服务器上的相对路径
         * @param fileName  要下载的文件名
         * @param localPath 下载后保存到本地的路径
         * @return
         */
        public  boolean downFile(String remotePath, String fileName,
                String localPath) {
            boolean success = false;
            try {
                if(!ftp.isConnected()){
                    logger.debug("获取文件失败");
                    return false;
                }
                //这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。
                //为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据,但是在linux上或者其他服务器上面,
                //由于安全限制,可能某些端口没有开启,所以就出现阻塞。
                ftp.enterLocalPassiveMode();
                ftp.setRemoteVerificationEnabled(false);
                ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
                FTPFile[] fs = ftp.listFiles(remotePath);
                boolean fileExsit = false;
                for (FTPFile ff : fs) {
                    if (ff.getName().equals(fileName)) {
                        fileExsit = true;
                        File localFile = new File(localPath + "/" + ff.getName());
                        OutputStream is = new FileOutputStream(localFile);
                        ftp.retrieveFile(ff.getName(), is);
                        is.close();
                        break;
                    }
                }
                if(!fileExsit){
                    System.out.println("文件不存在");
                    return success;
                }
                success = true;
            } catch (IOException e) {
                e.printStackTrace();
                throw new ServiceException("下载文件失败");
            } 
            return success;
        }

              

  • 相关阅读:
    CodeForces 660D Number of Parallelograms
    【POJ 1082】 Calendar Game
    【POJ 2352】 Stars
    【POJ 2481】 Cows
    【POJ 1733】 Parity Game
    【NOI 2002】 银河英雄传说
    【NOI 2015】 程序自动分析
    【POJ 1704】 Georgia and Bob
    【HDU 2176】 取(m堆)石子游戏
    【SDOI 2016】 排列计数
  • 原文地址:https://www.cnblogs.com/hisunhyx/p/5029476.html
Copyright © 2011-2022 走看看