springboot使用FTPClient连接报错:java.io.ioexception: connection is not open


报错代码:

        FTPClient ftpClient = new FTPClient();
        ftpClient.setConnectTimeout(1000 * 30);//设置连接超时时间
        ftpClient.setControlEncoding("utf-8");//设置ftp字符集
        ftpClient.enterLocalPassiveMode();//设置被动模式,文件传输端口设置
        try {
            //设置文件传输模式为二进制,可以保证传输的内容不会被改变
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                LOGGER.error("connect ftp {} failed", host);
                ftpClient.disconnect();
                return null;
            }
            LOGGER.info("replyCode==========={}", replyCode);
        } catch (IOException e) {
            LOGGER.error("connect fail ------->>>{}", e.getMessage());
            return null;
        }
        return ftpClient;

解决办法

将 设置文件传输模式为二进制,可以保证传输的内容不会被改变 调整到登录之后
即将 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 调整到 ftpClient.login(username, password); 后面
        FTPClient ftpClient = new FTPClient();
        ftpClient.setConnectTimeout(1000 * 30);//设置连接超时时间
        ftpClient.setControlEncoding("utf-8");//设置ftp字符集
        ftpClient.enterLocalPassiveMode();//设置被动模式,文件传输端口设置
        try {
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                LOGGER.error("connect ftp {} failed", host);
                ftpClient.disconnect();
                return null;
            }
            //设置文件传输模式为二进制,可以保证传输的内容不会被改变
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            LOGGER.info("replyCode==========={}", replyCode);
        } catch (IOException e) {
            LOGGER.error("connect fail ------->>>{}", e.getMessage());
            return null;
        }
        return ftpClient;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM