java ftp下载文件,可以创建ftpclient 但是进行下载时 提示connection refused

文章讨论了从ApacheCommonsNet3.9.0升级到3.6.0时遇到的问题,指出3.6.0版本可能忽略了某些FTP服务器的安全要求,导致在3.9.0版本下能正常下载但在3.6.0中出现问题。
摘要由CSDN通过智能技术生成

org.apache.commons.net 3.9.0和3.6.0的区别
将3.9.0修改成了3.6.0可以正常实现下载。
然而我之前使用3.9.0的版本下载是没有问题的。猜测是不同ftp服务器存在安全需求,而3.6.0版本忽略的这种安全问题 可以进行连接下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现FTP下载远程服务器指定目录下所有文件的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.SocketException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FTPDownloadDemo { public static void main(String[] args) { String server = "your.ftp.server.com"; int port = 21; String user = "username"; String password = "password"; String remoteDirPath = "/remote/directory/path"; String localDirPath = "/local/directory/path"; FTPClient ftpClient = new FTPClient(); try { // 连接FTP服务器 ftpClient.connect(server, port); // 登录FTP服务器 ftpClient.login(user, password); // 检查连接和登录是否成功 int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.err.println("FTP server refused connection."); return; } System.out.println("Connected to FTP server."); // 切换到指定目录 ftpClient.changeWorkingDirectory(remoteDirPath); // 获取指定目录下的所有文件 FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.isFile()) { // 下载文件 downloadFile(ftpClient, file.getName(), localDirPath); } } // 登出FTP服务器 ftpClient.logout(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 下载文件 * @param ftpClient FTP客户端 * @param fileName 文件名 * @param localDirPath 本地目录路径 * @throws IOException */ private static void downloadFile(FTPClient ftpClient, String fileName, String localDirPath) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(localDirPath + "/" + fileName); ftpClient.retrieveFile(fileName, fos); } finally { if (fos != null) { fos.close(); } } } } ``` 需要注意的是,上述代码使用了Apache Commons Net库来实现FTP操作,需要在项目中引用该库。可以从Apache官网下载该库或者通过Maven等构建工具添加依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值