How to convert FTPFIle to File using Java?

假装没事ソ 提交于 2019-12-12 05:18:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!