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:org.oss.bonita.utils.ftp.FtpTransfer.java

public boolean folderExists(FTPFile[] ftpFiles, String FolderName) {
    if (ftpFiles != null && ftpFiles.length > 0) {
        for (FTPFile file : ftpFiles) {
            if (!file.isFile()) {
                if (FolderName.equals(file.getName())) {
                    return true;
                }//w ww.  j  a  v a 2 s  .  co  m
            }
        }
    }
    return false;
}

From source file:org.ow2.proactive.scheduler.examples.FTPConnector.java

private List<String> downloadDirectory(FTPClient ftpClient, String parentDir, String currentDir, String saveDir)
        throws IOException {
    List<String> filesRelativePathName = new ArrayList<>();
    String dirToList = parentDir;
    if (!currentDir.isEmpty()) {
        dirToList = Paths.get(dirToList, currentDir).toString();
    }/*from   ww  w. j a v  a2  s .co m*/
    FTPFile[] subFiles = ftpClient.listFiles(dirToList);
    for (FTPFile aFile : subFiles) {
        String currentFileName = aFile.getName();
        if (currentFileName.equals(CURRENT_FOLDER) || currentFileName.equals(PARENT_FOLDER)) {
            // skip parent directory and the directory itself
            continue;
        }
        String remoteFilePath = Paths.get(parentDir, currentDir, currentFileName).toString();
        String savePath = Paths.get(saveDir, parentDir, currentDir, currentFileName).toString();
        if (aFile.isDirectory()) {
            // create the directory savePath inside saveDir
            makeDirectories(savePath);

            // download the sub directory
            filesRelativePathName.addAll(downloadDirectory(ftpClient, dirToList, currentFileName, saveDir));
        } else {
            // download the file
            filesRelativePathName.add(downloadSingleFile(ftpClient, remoteFilePath, savePath));
        }
    }
    return filesRelativePathName;
}

From source file:org.paxle.crawler.ftp.impl.FtpUrlConnection.java

private static void formatStdDirlisting(final OutputStream into, final FTPListParseEngine fileParseEngine) {
    final Formatter writer = new Formatter(into);
    FTPFile[] files;/*from w  w w.j  a v  a 2 s. c  o  m*/
    while (fileParseEngine.hasNext()) {
        files = fileParseEngine.getNext(16);
        for (final FTPFile file : files) {
            if (file == null)
                continue;

            // directory
            char c;
            switch (file.getType()) {
            case FTPFile.DIRECTORY_TYPE:
                c = 'd';
                break;
            case FTPFile.SYMBOLIC_LINK_TYPE:
                c = 's';
                break;
            default:
                c = '-';
                break;
            }
            writer.format("%c", Character.valueOf(c));

            // permissions
            for (final int access : FILE_ACCESS_MODES) {
                writer.format("%c%c%c",
                        Character.valueOf(file.hasPermission(access, FTPFile.READ_PERMISSION) ? 'r' : '-'),
                        Character.valueOf(file.hasPermission(access, FTPFile.WRITE_PERMISSION) ? 'w' : '-'),
                        Character.valueOf(file.hasPermission(access, FTPFile.EXECUTE_PERMISSION) ? 'x' : '-'));
            }

            // other information
            writer.format("  %2d", Integer.valueOf(file.getHardLinkCount()));
            writer.format("  %8s", file.getUser());
            writer.format("  %8s", file.getGroup());
            writer.format("  %12d", Long.valueOf(file.getSize()));
            writer.format("  %1$tY-%1$tm-%1$td %1$tH:%1$tM",
                    Long.valueOf(file.getTimestamp().getTimeInMillis()));
            writer.format("  %s", file.getName());

            writer.format("%s", System.getProperty("line.separator"));
        }
    }

    writer.flush();
}

From source file:org.paxle.crawler.ftp.impl.FtpUrlConnection.java

private FTPFile findFile() throws IOException {
    // get all files in the current directory
    FTPFile[] files = client.listFiles(path);

    // loop through the files to find our requested file
    for (FTPFile nextFile : files) {
        if (nextFile.getName().equals(this.file)) {
            return nextFile;
        }//from w  ww.  j av a 2 s  .c om
    }
    return null;
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

public void testGetName() throws Exception {
    FTPFile file = getSomeFile();
    assertFalse(file.getName().contains("/"));
    assertEquals(file.getName(), adapter.getName(new FtpItem(file, null)));

    FTPFile dir = getSomeDir();/* w  ww  . java 2 s  .co  m*/
    assertFalse(dir.getName().contains("/"));
    assertEquals(dir.getName(), adapter.getName(new FtpItem(dir, null)));
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

private String pathFromFile(FTPFile file) throws Exception {
    return ftp.printWorkingDirectory().substring(rootPath.length()) + "/" + file.getName();
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

private FTPFile getSomeFile() {
    try {// www  . j a v  a2s . co m
        FTPFile[] items = ftp.listFiles(rootPath);
        for (FTPFile item : items) {
            if (item.isDirectory() && !item.getName().startsWith(".")) {
                FTPFile[] items2 = ftp.listFiles(item.getName());
                if (items2 == null) {
                    continue;
                }
                ftp.changeWorkingDirectory(rootPath + item.getName());
                for (FTPFile item2 : items2) {
                    if (!item2.isDirectory() && !item2.isSymbolicLink() && !item2.getName().startsWith(".")
                            && item2.getName().contains(".")) {
                        return item2;
                    }
                }
            }
        }
        return null;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.punksearch.crawler.adapters.FtpAdapterTest.java

private FTPFile getSomeDir(int level) throws Exception {
    String curPath = "/";
    FTPFile result = null;/*  w ww.j a v  a  2 s .  c  om*/
    for (int cur = 0; cur <= level; cur++) {
        FTPFile[] items = ftp.listFiles(curPath);
        for (FTPFile item : items) {
            if (item.isDirectory() && !item.getName().startsWith(".")) {
                curPath += "/" + item.getName();
                result = item;
                break;
            }
        }
    }
    return result;
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_// w w w  .  j a v a  2s  . co m
 *
 * @param parentEntry _more_
 * @param rootDirPath _more_
 * @param parentPath _more_
 * @param file _more_
 *
 * @return _more_
 */
public String getSynthId(Entry parentEntry, String rootDirPath, String parentPath, FTPFile file) {
    String id = parentPath + "/" + file.getName();
    id = RepositoryUtil.encodeBase64(id.getBytes()).replace("\n", "");

    return Repository.ID_PREFIX_SYNTH + parentEntry.getId() + ":" + id;
}

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_/*  ww  w  . j  av a2  s  .co  m*/
 *
 * @param request _more_
 * @param parentEntry _more_
 * @param id _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public Entry makeSynthEntry(Request request, Entry parentEntry, String id) throws Exception {
    Object[] values = parentEntry.getValues();
    if (values == null) {
        return null;
    }
    String baseDir = (String) values[COL_BASEDIR];
    String server = (String) values[COL_SERVER];
    MyFTPFile myFtpFile = getFileFromId(parentEntry, id, baseDir);
    if (myFtpFile == null) {
        return null;
    }
    if (myFtpFile.path.equals(baseDir)) {
        return parentEntry;
    }
    FTPFile ftpFile = myFtpFile.ftpFile;
    TypeHandler handler = (ftpFile.isDirectory() ? getRepository().getTypeHandler(TypeHandler.TYPE_GROUP)
            : getRepository().getTypeHandler(TypeHandler.TYPE_FILE));
    handler = this;
    String synthId = Repository.ID_PREFIX_SYNTH + parentEntry.getId() + ":" + id;

    boolean isDir = ftpFile.isDirectory();
    Entry entry = (isDir ? (Entry) new Entry(synthId, handler, true) : new Entry(synthId, handler));

    String name = IOUtil.getFileTail(ftpFile.getName());
    entry.setIsLocalFile(true);
    Entry parent;
    if (myFtpFile.path.equals(baseDir)) {
        parent = (Entry) parentEntry;
    } else {
        File tmp = new File(myFtpFile.path);
        String parentPath = tmp.getParent().replace("\\", "/");
        String parentId = getSynthId(parentEntry, baseDir, parentPath);
        if (parentPath.equals(baseDir)) {
            parent = (Entry) parentEntry;
        } else {
            parent = (Entry) getEntryManager().getEntry(request, parentId, false, false);
        }
    }

    double maxSize = 0;
    if (values[COL_MAXSIZE] != null) {
        maxSize = ((Double) values[COL_MAXSIZE]).doubleValue();
    }

    long dttm = ftpFile.getTimestamp().getTime().getTime();
    /*
      String datePattern = (String)values[COL_DATE_PATTERN];
      //TODO cache the compiled patterns
      if(datePattern!=null && datePattern.length()>0)  {
      Pattern p = Pattern.compile(datePattern);
      Matcher m = p.matcher(path);
      if(m.matches()) {
          String dateString = m.group(1);
          String dateFormat = (String)values[COL_DATE_FORMAT];
          Date date=null;
          if(dateFormat!=null && dateFormat.length()==0) {
               date = new SimpleDateFormat(dateFormat).parse(dateString);
            
          } else {
               date = DateUtil.parse(dateString);
          }
          dttm  = date.getTime();
      }
      }
     */
    Resource resource;
    if (isDir) {
        resource = new Resource("ftp://" + server + myFtpFile.path, Resource.TYPE_URL);
    } else {
        if (ftpFile.getSize() > 1000000 * maxSize) {
            resource = new Resource("ftp://" + server + myFtpFile.path, Resource.TYPE_URL);
        } else {
            resource = new Resource(name, Resource.TYPE_REMOTE_FILE);

        }
        resource.setFileSize(ftpFile.getSize());
    }
    entry.initEntry(name, "", parent, getUserManager().getLocalFileUser(), resource, "", dttm, dttm, dttm, dttm,
            null);

    return entry;
}