Example usage for org.apache.commons.net.ftp FTPFile getType

List of usage examples for org.apache.commons.net.ftp FTPFile getType

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPFile getType.

Prototype

public int getType() 

Source Link

Document

Return the type of the file (one of the _TYPE constants), e.g., if it is a directory, a regular file, or a symbolic link.

Usage

From source file:rems.Global.java

public static String[] GetFileList(InetAddress ftpServerAddrs, String serverAppDirectory, String dirName,
        String userName, String password) {
    String[] downloadFiles = new String[1];
    StringBuilder result = new StringBuilder();
    FTPClient ftpClient = new FTPClient();
    try {/*from   w  w  w  .j  a  v  a2s  . co m*/
        // pass directory path on server to connect  
        ftpClient.connect(ftpServerAddrs);
        // pass username and password, returned true if authentication is  
        // successful  
        boolean login = ftpClient.login(userName, password);
        if (login) {
            //System.out.println("Connection established...");
            // get all files from server and store them in an array of  
            // FTPFiles  
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getType() == FTPFile.FILE_TYPE) {
                    result.append(file.getName());
                    result.append("\n");
                }
            }
            result.replace(result.toString().lastIndexOf("\n"), result.toString().length(), "");
            // logout the user, returned true if logout successfully  
            boolean logout = ftpClient.logout();
            if (logout) {
                System.out.println("Connection close...");
            }
        } else {
            System.out.println("Connection fail...");
        }
        return result.toString().split("\\\n");
    } catch (SocketException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.writeToLog();
    } catch (IOException e) {
        Global.errorLog += Arrays.toString(e.getStackTrace());
        Global.writeToLog();
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            Global.errorLog += Arrays.toString(e.getStackTrace());
            Global.writeToLog();
        }
    }
    return downloadFiles;
}

From source file:ro.kuberam.libs.java.ftclient.FTP.FTP.java

private static void _generateResourceElement(XMLStreamWriter xmlWriter, FTPFile resource, InputStream is,
        String resourceAbsolutePath) throws IOException, Exception {

    String resourceName = resource.getName();
    String resourceType = ((resource.getType() == 1) ? "directory"
            : (((resource.getType() == 0) ? "file" : "link")));
    Calendar resourceTimeStamp = resource.getTimestamp();
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
    String lastModified = formatter.format(resourceTimeStamp.getTimeInMillis());
    lastModified = lastModified.replace(" ", "T");
    lastModified = lastModified.substring(0, 22) + ":" + lastModified.substring(22, 24);
    long resourceSize = resource.getSize();
    String user = resource.getUser();
    String userGroup = resource.getGroup();
    String permissions = resource.getRawListing().substring(0, 10);
    String linkTo = resource.getLink();

    GenerateResourceElement.run(is, xmlWriter, modulePrefix, moduleNsUri, resourceName, resourceType,
            resourceAbsolutePath, lastModified, resourceSize, user, userGroup, permissions, linkTo);
}