Java——通过Java代码从ftp服务器下载文件

作者专注于Java、架构、Linux、小程序、爬虫、自动化等技术。 工作期间含泪整理出一些资料,微信搜索【javaUp】,回复 【java】【黑客】【爬虫】【小程序】【面试】等关键字免费获取资料。技术交流、项目合作可私聊。 微信:shuhao-99999 

前言

连接ftp服务器方式:通过Java代码连接ftp服务器 

实现

下载文件实现方式:

public void downloadFile(String localPath, String ftpPath, String fileName) {
    if (!localPath.endsWith("/")) {
        localPath = localPath + "/";
    }
    FTPClient client = ftpClientManager.getClient();
    File localFile = new File(localPath + fileName);
    try {
        client.changeWorkingDirectory(ftpPath);
        OutputStream is = new FileOutputStream(localFile);
        client.retrieveFile(fileName, is);
        is.close();
        logger.info("success to download file: " + localFile.getAbsolutePath());
    } catch (Exception e) {
        logger.error("faild to download file " + localFile.getAbsolutePath() + " because " + e.getMessage());
        System.exit(0);
    }
}

原文地址:https://www.cnblogs.com/shuhao66666/p/15196335.html