java 通过ftp获取文件_java – 从ftp获取最新文件

尝试创建一个简单的插件,只需连接到ftp站点,查找最新的文件,然后下载它.但是,它没有获取最新文件.

我正在使用org.apache.commons.net.ftp.ftpclient来处理所有事情.

这是我的代码

public static void main(String[] args)

{

FTPClient client = new FTPClient();

try

{

client.connect(host);

client.login(user, pwd);

FTPFile[] files = client.listFiles();

FTPFile lastFile = lastFileModified(files);

System.out.println(lastFile.getName());

client.disconnect();

}

catch(SocketException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

catch(IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static FTPFile lastFileModified(FTPFile[] files) {

Date lastMod = files[0].getTimestamp().getTime();

FTPFile choice = null;

for (FTPFile file : files) {

if (file.getTimestamp().getTime().after(lastMod)) {

choice = file;

lastMod = file.getTimestamp().getTime();

}

}

return choice;

}

它获取文件列表,然后返回一个文件,它不是最新的文件.有没有其他方法可以使用FTPClient比较文件修改日期,或者任何人都可以指出我正在做错的方向.谢谢.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个简单的示例代码,它使用 Apache Commons Net 库来连接到 FTP 服务器并获取文件: ``` import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; public class FTPDownloadFile { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); String remoteFile1 = "/test/video.mp4"; FileOutputStream fos = new FileOutputStream("D:/Downloads/video.mp4"); BufferedOutputStream bos = new BufferedOutputStream(fos); InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = inputStream.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); inputStream.close(); boolean success = ftpClient.completePendingCommand(); if (success) { System.out.println("File #1 has been downloaded successfully."); } } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } } ``` 在这段代码中,首先使用 `FTPClient.connect` 方法连接到 FTP 服务器,然后使用 `FTPClient.login` 方法登录。接下来,使用 `FTPClient.enterLocalPassiveMode` 方法将客户端设置为被动模式,这样就可以在防火墙或 NAT 后面的服务器上进行连接。 接下 ### 回答2: Java 通过 FTP 获取文件的代码可以使用 Apache Commons Net 库,下面是一个简单的示例: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class FtpClientExample { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "ftp_username"; String password = "ftp_password"; String remoteFilePath = "/path/to/remote/file.txt"; String localFilePath = "C:/path/to/local/file.txt"; FTPClient ftpClient = new FTPClient(); try { // 连接到 FTP 服务器 ftpClient.connect(server, port); ftpClient.login(username, password); // 设置文件传输类型为二进制 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 下载文件到本地 OutputStream outputStream = new FileOutputStream(localFilePath); ftpClient.retrieveFile(remoteFilePath, outputStream); outputStream.close(); // 断开与 FTP 服务器的连接 ftpClient.logout(); ftpClient.disconnect(); System.out.println("文件下载成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中,首先设置了 FTP 服务器的连接信息,包括服务器地址、端口、用户名和密码,以及远程文件路径和本地文件路径。然后使用 Apache Commons Net 库创建 FTPClient 对象,连接 FTP 服务器并登录。接着设置文件传输类型为二进制,然后调用 `retrieveFile()` 方法下载文件到本地。最后断开与 FTP 服务器的连接。 需要注意的是,上述示例仅适用于小文件下载,如果需要下载大文件或者支持断点续传等高级功能,还需要进一步的处理。 ### 回答3: 以下是Java通过FTP获取文件的代码: ```java import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import java.io.FileOutputStream; import java.io.IOException; public class FTPDemo { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; String remoteFile = "/path/to/file.txt"; String localFile = "localfile.txt"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); FileOutputStream outputStream = new FileOutputStream(localFile); boolean success = ftpClient.retrieveFile(remoteFile, outputStream); outputStream.close(); if (success) { System.out.println("文件下载成功!"); } else { System.out.println("文件下载失败!"); } ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } } ``` 请注意替换`server`、`port`、`user`、`pass`、`remoteFile`和`localFile`的值为实际的FTP服务器信息和文件路径。该代码使用了Apache Commons Net库,确保已将该库添加到项目中。首先建立FTP连接,然后登录FTP服务器,设置被动模式和文件类型。然后创建文件输出流,通过`retrieveFile`方法从远程FTP服务器下载文件到本地。最后关闭输出流和FTP连接,并在控制台打印下载结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值