使用Java代码从ftp服务器下载文件的示例分享
下文笔者讲述使用java代码从服务器上下载文件的方法分享,如下所示
ftp中下载文件
实现思路:
使用commons-net包中的
retrieveFile方法即可下载ftp中的文件
例: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);
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


