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

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Return the name of the file.

Usage

From source file:fr.lille1.car.burihabwa.rest.utils.FTPAdapterImpl.java

@Override
public boolean exists(final String path) throws IOException {
    if (!this.client.isConnected()) {
        this.authenticate();
    }//from  www . j a  va 2  s  .  c  om
    String parentDirectory = getParentDirectory(path);
    String file = getFile(path);
    if (!this.client.changeWorkingDirectory(parentDirectory)) {
        return false;
    }

    FTPFile[] files = this.client.listFiles();
    for (FTPFile f : files) {
        if (f.isFile() && f.getName().equalsIgnoreCase(file)) {
            this.client.changeWorkingDirectory("");
            return true;
        }
    }
    this.client.changeWorkingDirectory("");
    return false;
}

From source file:com.naryx.tagfusion.cfm.tag.net.ftp.FtpList.java

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    cfFTPData ftpdata = getFTPData(_session, argStruct);

    boolean soe = getNamedBooleanParam(argStruct, "stoponerror", true);
    boolean passive = getNamedBooleanParam(argStruct, "passive", false);
    String directory = getNamedStringParam(argStruct, "directory", "/");
    if (!directory.endsWith("/"))
        directory += "/";

    // Perform the directory listing
    FTPFile[] files;/*from  w w w .j  a v  a 2  s. c o m*/
    try {
        ftpdata.lock();
        ftpdata.setPassive(passive);
        files = ftpdata.listFiles(directory);
        if (soe && !ftpdata.isSucceeded())
            throwException(_session, ftpdata.getErrorText());

    } finally {
        ftpdata.unlock();
    }

    // Returning a query
    cfQueryResultData queryFile = new cfQueryResultData(new String[] { "name", "path", "url", "length",
            "lastmodified", "attributes", "isdirectory", "mode" }, "CFFTP");

    if (!ftpdata.isSucceeded() || files.length == 0)
        return queryFile;

    // Fill out the query
    if (!directory.startsWith("/"))
        directory = "/" + directory;

    List<Map<String, cfData>> resultQuery = new ArrayList<Map<String, cfData>>(files.length);
    for (FTPFile ftpfile : files) {

        Map<String, cfData> HM = new FastMap<String, cfData>();

        HM.put("name", new cfStringData(ftpfile.getName()));
        HM.put("length", new cfNumberData(ftpfile.getSize()));
        HM.put("lastmodified", new cfDateData(ftpfile.getTimestamp().getTime().getTime()));
        HM.put("isdirectory", cfBooleanData.getcfBooleanData(ftpfile.isDirectory()));

        HM.put("path", new cfStringData(directory + ftpfile.getName()));
        HM.put("url", new cfStringData("ftp://" + ftpdata.getServer() + directory + ftpfile.getName()));
        HM.put("attributes", new cfStringData(ftpfile.isDirectory() ? "Directory" : "Normal"));

        String mode = getMode(ftpfile, FTPFile.USER_ACCESS);
        mode += getMode(ftpfile, FTPFile.GROUP_ACCESS);
        mode += getMode(ftpfile, FTPFile.WORLD_ACCESS);
        HM.put("mode", new cfStringData(mode));

        resultQuery.add(HM);
    }

    queryFile.populateQuery(resultQuery);
    return queryFile;
}

From source file:ch.cyberduck.core.ftp.parser.NTFTPEntryParserTest.java

@Test
public void testElectic() throws Exception {
    FTPFile parsed = parser
            .parseFTPEntry("09-04-06  11:28AM                  149 gearkommandon with spaces.txt");
    assertNotNull(parsed);/*from   ww w.j  av  a2s .c  om*/
    assertEquals("gearkommandon with spaces.txt", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals(Calendar.SEPTEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(4, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertEquals(2006, parsed.getTimestamp().get(Calendar.YEAR));
}

From source file:ch.cyberduck.core.ftp.parser.FreeboxFTPEntryParserTest.java

@Test
public void testParse() {
    FTPFile parsed;

    parsed = parser.parseFTPEntry(/*  ww  w  . ja  v a2s. c om*/
            "-rw-r--r--   1  freebox  freebox 2064965868 Apr 15 21:17 M6 - Capital 15-04-2007 21h37 1h40m.ts");
    assertNotNull(parsed);
    assertEquals(parsed.getName(), "M6 - Capital 15-04-2007 21h37 1h40m.ts");
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals("freebox", parsed.getUser());
    assertEquals("freebox", parsed.getGroup());
    assertEquals(2064965868, parsed.getSize());
    assertEquals(Calendar.APRIL, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(15, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 75906880 Sep 08 06:33 Direct 8 - Gym direct - 08-09-2007 08h30 1h08m.ts");
    assertNotNull(parsed);
    assertEquals("Direct 8 - Gym direct - 08-09-2007 08h30 1h08m.ts", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals("freebox", parsed.getUser());
    assertEquals("freebox", parsed.getGroup());
    assertEquals(75906880, parsed.getSize());
    assertEquals(Calendar.SEPTEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(8, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 1171138668 May 19 17:20 France 3 national - 19-05-2007 18h15 1h05m.ts");
    assertNotNull(parsed);
    assertEquals("France 3 national - 19-05-2007 18h15 1h05m.ts", parsed.getName());
}

From source file:com.stacksync.desktop.connection.plugins.ftp.FtpTransferManager.java

@Override
public Map<String, RemoteFile> list() throws StorageException {
    connect();//w  ww  .j  av a 2  s  .c o m

    try {
        Map<String, RemoteFile> files = new HashMap<String, RemoteFile>();
        FTPFile[] ftpFiles = ftp.listFiles(getConnection().getPath());

        for (FTPFile f : ftpFiles) {
            files.put(f.getName(), new RemoteFile(f.getName(), f.getSize(), f));
            if (f.isDirectory()) {
                files.putAll(getDirectoryList(f.getName()));
            }
        }

        return files;
    } catch (IOException ex) {
        logger.error("Unable to list FTP directory.", ex);
        throw new StorageException(ex);
    }
}

From source file:ch.cyberduck.core.ftp.parser.RumpusFTPEntryParserTest.java

@Test
public void testParse() throws Exception {
    FTPFile parsed;

    parsed = parser.parseFTPEntry("drwxr-xr-x               folder        0 Oct 18 13:02 Akrilik");
    assertNotNull(parsed);//from  www.j a  v a 2s .c o  m
    assertEquals("Akrilik", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals(Calendar.OCTOBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(18, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));

    parsed = parser.parseFTPEntry("drwxrwxrwx               folder        0 Oct 11 14:53 Uploads");
    assertNotNull(parsed);
    assertEquals("Uploads", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals(Calendar.OCTOBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(11, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));

    parsed = parser.parseFTPEntry("-rw-r--r--        0      589878   589878 Oct 15 13:03 WebDAV SS.bmp");
    assertNotNull(parsed);
    assertEquals("WebDAV SS.bmp", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals(Calendar.OCTOBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(15, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
}

From source file:models.Indexer.java

public int indexFtpServer() {
    try {// w w  w  .  j ava2  s.c om
        ftpClient.connect(ftp_address);
        boolean login = ftpClient.login("anonymous", "");
        if (login) {
            System.out.println("Connection established...");

            // get all files from server and store them in an array of  
            // FTPFiles  
            FTPFile[] files = ftpClient.listFiles();
            ArrayList<String> all_files = new ArrayList<>();
            for (FTPFile cur_file : files) {
                if (cur_file.getType() == FTPFile.FILE_TYPE) {
                    String file_name = cur_file.getName();
                    if (file_name.endsWith(".pdf"))
                        all_files.add(file_name);
                }
            }
            downloadAndIndex(all_files);
            // 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...");
        }

    } catch (IOException ex) {
        System.out.println("Error to connect ftp server where the corpus is stored");
        Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return num_of_docs_indexed;
}

From source file:models.LocalIndexer.java

public int indexFtpServer() {
    try {/*from   w  ww .j  ava 2s .c om*/
        ftpClient.connect(ftp_address);
        boolean login = ftpClient.login("anonymous", "");
        if (login) {
            System.out.println("Connection established...");

            // get all files from server and store them in an array of  
            // FTPFiles  
            FTPFile[] files = ftpClient.listFiles();
            ArrayList<String> all_files = new ArrayList<>();
            for (FTPFile cur_file : files) {
                if (cur_file.getType() == FTPFile.FILE_TYPE) {
                    String file_name = cur_file.getName();
                    if (file_name.endsWith(".pdf"))
                        all_files.add(file_name);
                }
            }
            downloadAndIndex(all_files);
            // 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...");
        }

    } catch (IOException ex) {
        System.out.println("Error to connect ftp server where the corpus is stored");
        Logger.getLogger(LocalIndexer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return num_of_docs_indexed;
}

From source file:at.beris.virtualfile.provider.FtpClientFileOperationProvider.java

@Override
public List<at.beris.virtualfile.File> list(FileModel model, Filter filter) throws IOException {
    List<FTPFile> ftpFileList = client.list(resolveUrl(model).getPath());
    List<at.beris.virtualfile.File> fileList = new ArrayList<>();

    String parentPath = model.getUrl().getPath();
    for (FTPFile ftpFile : ftpFileList) {
        FileModel childModel = new FileModel();
        childModel.setParent(model);//ww  w  .  j a v a2  s .c  o m
        String childPath = parentPath + ftpFile.getName() + (ftpFile.isDirectory() ? "/" : "");
        at.beris.virtualfile.File childFile = fileContext.newFile(UrlUtils.newUrl(model.getUrl(), childPath));
        FtpFileTranslator.fillModel(childModel, ftpFile, client);
        childFile.setModel(childModel);
        if (filter == null || filter.filter(childFile)) {
            fileList.add(childFile);
        }
    }
    return fileList;
}

From source file:com.atomicleopard.thundr.ftp.FtpSession.java

public boolean getFile(FTPFile file, OutputStream os) {
    return file != null && file.isFile() && getFile(file.getName(), os);
}