问题
I need to read all the files from a shared location and returns a File Map. I use FTPClient to access the shared location. Using FTPClient I able to retrieve all the File as a FTPFile. But I want Convert FTPFile to File. please see the code.
FTPFile[] ftpFiles = ftpClient.listFiles(folderPath);
Note:- I Don't want to Create new connection every time. I want to read all in one connection
回答1:
Looks like this is very old question but just wanted to update what I have done.
InputStream iStream=ftpClient.retrieveFileStream(ftpFile.getName());
File file = File.createTempFile("tmp", null);
FileUtils.copyInputStreamToFile(iStream, file);
Hopefully this is helpful.
回答2:
If you only want get the name, try this code:
private File[] getRemoteFilesInFolder() {
FTPFile[] elements;
File[] files;
try {
elements = ftpClient.listFiles();
files = new File[elements.length];
for(int i=0; i< elements.length; i++) {
files[i] = new File(elements[i].getName());
}
return files;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
来源:https://stackoverflow.com/questions/18311309/how-to-convert-ftpfile-to-file-using-java