本文介绍了如何将FTPFile列表转换为Java中的文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FTP文件列表,我只能将第一个 FTPFile 转换为 File 。我正在使用 org.apache.commons.net.ftp 库。

我的代码第一次正常工作:

$ b $()ftpClient.sendNoOp()){
if(file.getName()){
$ b

  != null ||!file.getName()。equals()){
InputStream iStream = ftpClient.retrieveFileStream(file.getName());
文件file2 = File.createTempFile(xml,null);
FileUtils.copyInputStreamToFile(iStream,file2);
iStream.close();





$ b $ p $从这段代码中遍历循环只有时间和 FTPFile 被转换为 File ,之后它会得到由 ftpClient.retrieveFileStream(file.getName())中生成的code> null
b请帮助。

解决方案

在下载文件后,您必须完成传输。这就是你能够下载你的第一个文件并与下载第二个文件有关的原因。

  success = ftpClient。 completePendingCommand(); 
if(success){
System.out.println(文件已成功下载);





$ b如果问题仍然存在,你可以调用ftpClient.getReplyCode()你知道为什么你无法下载该文件。通常,retrieveFileStream在无法打开数据连接时返回null。

I have a list of FTP files and I can only convert the first FTPFile to File. I am using org.apache.commons.net.ftp library.
My code is works fine for the first time:

for (FTPFile file : files) {
  if(ftpClient.sendNoOp()){
   if(file.getName()!=null || !file.getName().equals("")) {
           InputStream iStream=ftpClient.retrieveFileStream(file.getName());
           File file2 = File.createTempFile("xml", null);
           FileUtils.copyInputStreamToFile(iStream, file2);
           iStream.close();
                }
  }
}

From this code the loop traverse only time and the FTPFile is converted to File and after that it will get exception which is created by null value generated in ftpClient.retrieveFileStream(file.getName()).
Please help.

解决方案

You will have to finalize the transfer after you download the file. That is the reason you are able to download your first file and having issue with the download of second file.

 success = ftpClient.completePendingCommand();
 if (success) {
                System.out.println("File has been downloaded successfully.");
  }

If still issue persists, you can invoke ftpClient.getReplyCode() which will let you know why you are not able to download the file. Generally retrieveFileStream returns null when it cannot open the data connection

这篇关于如何将FTPFile列表转换为Java中的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:38