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

              

  • 相关阅读:
    nginx高级玩法之根据来源ip分流
    ubuntu上的 /dev/loop0 到 /dev/loop18占到100%的处理
    nginx: [warn] conflicting server name "aaa.bbbb.com" on 0.0.0.0:80, ignored
    nginx报警:nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size
    nginx配置socket连接
    Syntax error: "(" unexpected shell里面的报错解决
    docker批量操作容器
    ubuntu18.04安装docker和开通对外2375端口(方便portainer管理)
    Cadence OrCad Allegro SPB 16.6 下载及安装破解指南
    关于XILINX芯片IO管脚的上拉电阻的疑问
  • 原文地址:https://www.cnblogs.com/hisunhyx/p/5029476.html
Copyright © 2011-2022 走看看