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

前言

连接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);
    }
}
posted @ 2021-08-28 10:05  前方一片光明  阅读(525)  评论(0编辑  收藏  举报