Search in sources :

Example 61 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project Lucee by lucee.

the class Ftp method removeRecursive.

private static void removeRecursive(AFTPClient client, String path, int type) throws IOException {
    // directory
    if (FTPFile.DIRECTORY_TYPE == type) {
        if (!path.endsWith("/"))
            path += "/";
        // first we remove the children
        FTPFile[] children = client.listFiles(path);
        for (FTPFile child : children) {
            if (child.getName().equals(".") || child.getName().equals(".."))
                continue;
            removeRecursive(client, path + child.getName(), child.getType());
        }
        // then the directory itself
        client.removeDirectory(path);
    } else // file
    if (FTPFile.FILE_TYPE == type) {
        client.deleteFile(path);
    }
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 62 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project Lucee by lucee.

the class Ftp method existsFile.

/* *
     * check if file or directory exists if it exists return FTPFile otherwise null
     * @param client
     * @param strPath
     * @return FTPFile or null
     * @throws IOException
     * @throws PageException
     * /
    private FTPFile exists(FTPClient client, String strPath) throws PageException, IOException {
        strPath=strPath.trim();
        
        // get parent path
        FTPPath path=new FTPPath(client.printWorkingDirectory(),strPath);
        String name=path.getName();
        print.out("path:"+name);
        
        // when directory
        FTPFile[] files=null;
        try {
            files = client.listFiles(path.getPath());
        } catch (IOException e) {}
        
        if(files!=null) {
            for(int i=0;i<files.length;i++) {
                if(files[i].getName().equalsIgnoreCase(name)) {
                    return files[i];
                }
            }
            
        }
        return null;
    }*/
private FTPFile existsFile(AFTPClient client, String strPath, boolean isFile) throws PageException, IOException {
    strPath = strPath.trim();
    if (strPath.equals("/")) {
        FTPFile file = new FTPFile();
        file.setName("/");
        file.setType(FTPFile.DIRECTORY_TYPE);
        return file;
    }
    // get parent path
    FTPPath path = new FTPPath(client, strPath);
    String p = path.getPath();
    String n = path.getName();
    strPath = p;
    if ("//".equals(p))
        strPath = "/";
    if (isFile)
        strPath += n;
    // when directory
    FTPFile[] files = null;
    try {
        files = client.listFiles(p);
    } catch (IOException e) {
    }
    if (files != null) {
        for (int i = 0; i < files.length; i++) {
            if (files[i].getName().equalsIgnoreCase(n)) {
                return files[i];
            }
        }
    }
    return null;
}
Also used : FTPFile(org.apache.commons.net.ftp.FTPFile) IOException(java.io.IOException) FTPPath(lucee.runtime.net.ftp.FTPPath)

Example 63 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project Lucee by lucee.

the class Ftp method actionExistsFile.

/**
 * check if a file exists or not
 * @return FTPCLient
 * @throws IOException
 * @throws PageException
 */
private AFTPClient actionExistsFile() throws PageException, IOException {
    required("remotefile", remotefile);
    AFTPClient client = getClient();
    FTPFile file = existsFile(client, remotefile, true);
    Struct cfftp = writeCfftp(client);
    cfftp.setEL(RETURN_VALUE, Caster.toBoolean(file != null && file.isFile()));
    cfftp.setEL(SUCCEEDED, Boolean.TRUE);
    stoponerror = false;
    return client;
}
Also used : AFTPClient(lucee.runtime.net.ftp.AFTPClient) FTPFile(org.apache.commons.net.ftp.FTPFile) Struct(lucee.runtime.type.Struct)

Example 64 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project Lucee by lucee.

the class Ftp method actionExists.

/**
 * check if a file or directory exists
 * @return FTPCLient
 * @throws PageException
 * @throws IOException
 */
private AFTPClient actionExists() throws PageException, IOException {
    required("item", item);
    AFTPClient client = getClient();
    FTPFile file = existsFile(client, item, false);
    Struct cfftp = writeCfftp(client);
    cfftp.setEL(RETURN_VALUE, Caster.toBoolean(file != null));
    cfftp.setEL(SUCCEEDED, Boolean.TRUE);
    return client;
}
Also used : AFTPClient(lucee.runtime.net.ftp.AFTPClient) FTPFile(org.apache.commons.net.ftp.FTPFile) Struct(lucee.runtime.type.Struct)

Example 65 with FTPFile

use of org.apache.commons.net.ftp.FTPFile in project Lucee by lucee.

the class Ftp method actionListDir.

/**
 * List data of a ftp connection
 * @return FTPCLient
 * @throws PageException
 * @throws IOException
 */
private AFTPClient actionListDir() throws PageException, IOException {
    required("name", name);
    required("directory", directory);
    AFTPClient client = getClient();
    FTPFile[] files = client.listFiles(directory);
    if (files == null)
        files = new FTPFile[0];
    pageContext.setVariable(name, toQuery(files, "ftp", directory, client.getRemoteAddress().getHostName()));
    writeCfftp(client);
    return client;
}
Also used : AFTPClient(lucee.runtime.net.ftp.AFTPClient) FTPFile(org.apache.commons.net.ftp.FTPFile)

Aggregations

FTPFile (org.apache.commons.net.ftp.FTPFile)120 IOException (java.io.IOException)59 FTPClient (org.apache.commons.net.ftp.FTPClient)34 Test (org.junit.Test)32 File (java.io.File)28 InputStream (java.io.InputStream)16 ArrayList (java.util.ArrayList)15 FrameworkException (org.structr.common.error.FrameworkException)15 Tx (org.structr.core.graph.Tx)15 FtpTest (org.structr.web.files.FtpTest)15 FileOutputStream (java.io.FileOutputStream)11 OutputStream (java.io.OutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BuildException (org.apache.tools.ant.BuildException)8 List (java.util.List)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 LiteralExpression (org.springframework.expression.common.LiteralExpression)5 HashSet (java.util.HashSet)4