JAVA 连接ftp上传下载报错 connection reset ,ftp.enterLocalPassiveMode(),

//代码中注释的
ftp.enterLocalPassiveMode();
这句话就是导致上传下载失败的原因,网上翻了翻,应该是和ftp服务器的防火墙等设置有关,前几天用的时候,程序中用ftp.enterLocalPassiveMode();还是正常的,今天就突然不行了,应该是公司的服务器相关设置被改了。具体大家情况可能不一样,网上文章说如果你服务器设置了需要LocalPassiveMode这种方式传输的话,如果程序里不写ftp.enterLocalPassiveMode();也会报 connection reset的错。自己也不懂,还没有去细看,只是暂时把问题解决了,记下来以后再来补充。第一次写博客,希望坚持下去。
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
                                   String fileName, String localPath)
{
    boolean result = false;
    logger.info("download fileName:"+fileName);
    FTPClient ftp = getFtpClient();
    try
    {
        int reply;
        logger.info("ftp.connect():"+fileName);
        ftp.connect(host, port);
        // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
        ftp.login(username, password);// 登录
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply))
        {
            logger.info("ftp.disconnect():"+fileName);
            ftp.disconnect();
            return result;
        }
        // Use passive mode to pass firewalls.
       // ftp.enterLocalPassiveMode();
        logger.info("ftp.changeWorkingDirectory():"+fileName);
        ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
        FTPFile[] fs = ftp.listFiles();
        for (FTPFile ff : fs)
        {
            if (ff.getName().equals(fileName))
            {
                File localFile = new File(localPath + "/" + ff.getName());

                OutputStream is = new FileOutputStream(localFile);
                ftp.retrieveFile(ff.getName(), is);
                is.close();
            }
        }

        ftp.logout();
        result = true;
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (ftp.isConnected())
        {
            try
            {
                logger.info("finally ftp.disconnect():"+"-------");
                ftp.disconnect();
            }
            catch (IOException ioe)
            {
                logger.info("catch ftp.disconnect():"+"-------");
            }
        }
    }
    return result;
}
//代码中注释的
ftp.enterLocalPassiveMode();
这句话就是导致上传下载失败的原因,网上翻了翻,应该是和ftp服务器的防火墙等设置有关,前几天用的时候,用ftp.enterLocalPassiveMode();还是正常的,今天就突然不行了,应该是公司的服务器相关设置被改了。

public static FTPFile[] list(String host, int port, String username, String password, String remotePath)
{
    FTPFile[] files = new FTPFile[1];
    FTPClient ftp = getFtpClient();
    try
    {
        int reply;
        ftp.connect(host, port);
        // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
        ftp.login(username, password);// 登录
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply))
        {
            ftp.disconnect();
            return files;
        }
        // Use passive mode to pass firewalls.
       // ftp.enterLocalPassiveMode();
        ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
        files = ftp.listFiles();

        ftp.logout();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (ftp.isConnected())
        {
            try
            {
                ftp.disconnect();
            }
            catch (IOException ioe)
            {
            }
        }
    }
    return files;
}

你可能感兴趣的:(Java,连接ftp异常记录)