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;
posted @ 2020-08-08 21:48  soldier_cnblogs  阅读(3559)  评论(0编辑  收藏  举报