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:co.cask.hydrator.action.ftp.FTPCopyAction.java

@Override
public void run(ActionContext context) throws Exception {
    Path destination = new Path(config.getDestDirectory());
    FileSystem fileSystem = FileSystem.get(new Configuration());
    destination = fileSystem.makeQualified(destination);
    if (!fileSystem.exists(destination)) {
        fileSystem.mkdirs(destination);//from  ww  w  .j ava  2  s . c  o m
    }

    FTPClient ftp;
    if ("ftp".equals(config.getProtocol().toLowerCase())) {
        ftp = new FTPClient();
    } else {
        ftp = new FTPSClient();
    }
    ftp.setControlKeepAliveTimeout(5);
    // UNIX type server
    FTPClientConfig ftpConfig = new FTPClientConfig();
    // Set additional parameters required for the ftp
    // for example config.setServerTimeZoneId("Pacific/Pitcairn")
    ftp.configure(ftpConfig);
    try {
        ftp.connect(config.getHost(), config.getPort());
        ftp.enterLocalPassiveMode();
        String replyString = ftp.getReplyString();
        LOG.info("Connected to server {} and port {} with reply from connect as {}.", config.getHost(),
                config.getPort(), replyString);

        // Check the reply code for actual success
        int replyCode = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(replyCode)) {
            ftp.disconnect();
            throw new RuntimeException(String.format("FTP server refused connection with code %s and reply %s.",
                    replyCode, replyString));
        }

        if (!ftp.login(config.getUserName(), config.getPassword())) {
            LOG.error("login command reply code {}, {}", ftp.getReplyCode(), ftp.getReplyString());
            ftp.logout();
            throw new RuntimeException(String.format(
                    "Login to the FTP server %s and port %s failed. " + "Please check user name and password.",
                    config.getHost(), config.getPort()));
        }

        FTPFile[] ftpFiles = ftp.listFiles(config.getSrcDirectory());
        LOG.info("listFiles command reply code: {}, {}.", ftp.getReplyCode(), ftp.getReplyString());
        // Check the reply code for listFiles call.
        // If its "522 Data connections must be encrypted" then it means data channel also need to be encrypted
        if (ftp.getReplyCode() == 522 && "sftp".equalsIgnoreCase(config.getProtocol())) {
            // encrypt data channel and listFiles again
            ((FTPSClient) ftp).execPROT("P");
            LOG.info("Attempting command listFiles on encrypted data channel.");
            ftpFiles = ftp.listFiles(config.getSrcDirectory());
        }
        for (FTPFile file : ftpFiles) {
            String source = config.getSrcDirectory() + "/" + file.getName();

            LOG.info("Current file {}, source {}", file.getName(), source);
            if (config.getExtractZipFiles() && file.getName().endsWith(".zip")) {
                copyZip(ftp, source, fileSystem, destination);
            } else {
                Path destinationPath = fileSystem.makeQualified(new Path(destination, file.getName()));
                LOG.debug("Downloading {} to {}", file.getName(), destinationPath.toString());
                try (OutputStream output = fileSystem.create(destinationPath)) {
                    InputStream is = ftp.retrieveFileStream(source);
                    ByteStreams.copy(is, output);
                }
            }
            if (!ftp.completePendingCommand()) {
                LOG.error("Error completing command.");
            }
        }
        ftp.logout();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (Throwable e) {
                LOG.error("Failure to disconnect the ftp connection.", e);
            }
        }
    }
}

From source file:fr.acxio.tools.agia.ftp.FTPDownloadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);/*from  w w w .  j  ava2  s  . c  o  m*/
    try {
        URI aRemoteBaseURI = new URI(remoteBaseDir);
        URI aRemoteBasePath = new URI(aRemoteBaseURI.toASCIIString() + SEPARATOR);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : [{}] {} ({})", aClient.getRemoteAddress().toString(),
                    aRemoteBaseURI.toASCIIString(), regexFilename);
        }

        FTPFile[] aRemoteFiles = aClient.listFiles(aRemoteBaseURI.toASCIIString(), aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aRemoteFiles.length);
        }

        for (FTPFile aRemoteFile : aRemoteFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            File aLocalFile = new File(localBaseDir, aRemoteFile.getName());
            URI aRemoteTFile = aRemoteBasePath.resolve(aRemoteFile.getName());

            FileOutputStream aOutputStream = new FileOutputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Downloading : {} => {}", aRemoteTFile.toASCIIString(),
                            aLocalFile.getAbsolutePath());
                }

                aClient.retrieveFile(aRemoteTFile.toASCIIString(), aOutputStream);
                if (removeRemote) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info(" Deleting : {}", aRemoteTFile.toASCIIString());
                    }

                    aClient.deleteFile(aRemoteTFile.toASCIIString());
                }

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }

            } finally {
                aOutputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

From source file:ilarkesto.integration.ftp.FtpClient.java

public FTPFile getFile(String path) {
    Filepath filepath = new Filepath(path);
    String parentPath = filepath.getParentAsString();
    String name = filepath.getLastElementName();
    for (FTPFile ftpFile : listFiles(parentPath)) {
        if (ftpFile.getName().equals(name))
            return ftpFile;
    }// w  w  w .j av  a 2  s. co  m
    return null;
}

From source file:de.ep3.ftpc.model.CrawlerDirectories.java

/**
 * Provides the next file or directory in the current stack.
 *
 * Since this is a state machine, it will return a different results on
 * consecutive calls./* ww  w  . j  a  v a2s  .  c o m*/
 *
 * @return File, directory or null, if finished.
 * @throws IOException
 */
public CrawlerFile getNextFile() throws IOException {
    if (directoryStack.isEmpty()) {
        if (includePaths.isEmpty()) {
            return null;
        }

        String nextIncludePath = includePaths.firstElement();
        includePaths.remove(nextIncludePath);

        directoryStack.push(new CrawlerDirectory(ftpClient, nextIncludePath));
    }

    CrawlerDirectory currentDirectory = directoryStack.peek();

    FTPFile file = currentDirectory.getNextFile();

    if (file == null) {
        directoryStack.pop();

        return getNextFile();
    }

    if (file.getType() == FTPFile.DIRECTORY_TYPE) {
        String nextPath = currentDirectory.getPath() + file.getName();

        if (!excludePaths.contains(nextPath)) {
            directoryStack.push(new CrawlerDirectory(ftpClient, nextPath));
        }
    }

    return new CrawlerFile(file, currentDirectory.getPath());
}

From source file:de.aw.awlib.fragments.AWRemoteFileChooser.java

/**
 * Wird ein Directory ausgwaehlt, wird in dieses Directory gewechselt.
 *//*w w w . java 2 s.  c om*/
@Override
public void onRecyclerItemClick(View v, int position, FTPFile file) {
    if (file.isDirectory()) {
        String filename = file.getName();
        if (filename.equals("..")) {
            if (mDirectoyList.size() != 0) {
                mDirectoyList.remove(mDirectoyList.size() - 1);
            }
            mUri = Uri.parse("/");
            for (int i = 0; i < mDirectoyList.size(); i++) {
                String dir = mDirectoyList.get(i);
                mUri = withAppendedPath(mUri, dir);
            }
        } else {
            mDirectoyList.add(filename);
            mUri = withAppendedPath(mUri, filename);
        }
        getExecuter().listFilesInDirectory(mUri.getEncodedPath(), mFileFilter);
    } else {
        super.onRecyclerItemClick(v, position, file);
    }
}

From source file:com.jaeksoft.searchlib.scheduler.task.TaskFtpXmlFeed.java

@Override
public void execute(Client client, TaskProperties properties, Variables variables, TaskLog taskLog)
        throws SearchLibException {
    String server = properties.getValue(propServer);
    String path = properties.getValue(propPath);
    String login = properties.getValue(propLogin);
    String password = properties.getValue(propPassword);
    String fileNamePattern = properties.getValue(propFileNamePattern);
    boolean deleteAfterLoad = Boolean.TRUE.toString().equals(properties.getValue(propDeleteAfterLoad));
    boolean truncateWhenFilesFound = Boolean.TRUE.toString()
            .equals(properties.getValue(propTruncateIndexWhenFilesFound));
    Pattern pattern = null;/*from   w w  w  . j  a va  2 s .  co  m*/
    if (fileNamePattern != null && fileNamePattern.length() > 0)
        pattern = Pattern.compile(fileNamePattern);

    String p = properties.getValue(propBuffersize);
    String xsl = properties.getValue(propXsl);
    File xmlTempResult = null;
    int bufferSize = 50;
    if (p != null && p.length() > 0)
        bufferSize = Integer.parseInt(p);
    HttpDownloader httpDownloader = client.getWebCrawlMaster().getNewHttpDownloader(true);
    FTPClient ftp = null;
    InputStream inputStream = null;
    try {
        // FTP Connection
        ftp = new FTPClient();
        checkConnect(ftp, server, login, password);
        FTPFile[] files = ftp.listFiles(path, new FtpFileInstance.FtpInstanceFileFilter(true, false, null));
        if (files == null)
            return;
        // Sort by ascendant filename
        String[] fileNames = new String[files.length];
        int i = 0;
        for (FTPFile file : files)
            fileNames[i++] = file.getName();
        Arrays.sort(fileNames);
        int ignored = 0;
        int loaded = 0;
        boolean bAlreadyTruncated = false;
        for (String fileName : fileNames) {
            String filePathName = FilenameUtils.concat(path, fileName);
            if (pattern != null)
                if (!pattern.matcher(fileName).find()) {
                    ignored++;
                    continue;
                }
            if (truncateWhenFilesFound && !bAlreadyTruncated) {
                client.deleteAll();
                bAlreadyTruncated = true;
            }
            taskLog.setInfo("Working on: " + filePathName);
            inputStream = ftp.retrieveFileStream(filePathName);
            Node xmlDoc = null;
            if (xsl != null && xsl.length() > 0) {
                xmlTempResult = File.createTempFile("ossftpfeed", ".xml");
                DomUtils.xslt(new StreamSource(inputStream), xsl, xmlTempResult);
                xmlDoc = DomUtils.readXml(new StreamSource(xmlTempResult), false);
            } else
                xmlDoc = DomUtils.readXml(new StreamSource(inputStream), false);
            client.updateXmlDocuments(xmlDoc, bufferSize, null, httpDownloader, taskLog);
            client.deleteXmlDocuments(xmlDoc, bufferSize, taskLog);
            inputStream.close();
            inputStream = null;
            if (!ftp.completePendingCommand())
                throw new SearchLibException("FTP Error");
            if (xmlTempResult != null) {
                xmlTempResult.delete();
                xmlTempResult = null;
            }
            checkConnect(ftp, server, login, password);
            if (deleteAfterLoad)
                ftp.deleteFile(filePathName);
            loaded++;
        }
        taskLog.setInfo(loaded + " file(s) loaded - " + ignored + " file(s) ignored");
    } catch (XPathExpressionException e) {
        throw new SearchLibException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SearchLibException(e);
    } catch (ParserConfigurationException e) {
        throw new SearchLibException(e);
    } catch (SAXException e) {
        throw new SearchLibException(e);
    } catch (IOException e) {
        throw new SearchLibException(e);
    } catch (URISyntaxException e) {
        throw new SearchLibException(e);
    } catch (InstantiationException e) {
        throw new SearchLibException(e);
    } catch (IllegalAccessException e) {
        throw new SearchLibException(e);
    } catch (ClassNotFoundException e) {
        throw new SearchLibException(e);
    } catch (TransformerException e) {
        throw new SearchLibException(e);
    } finally {
        if (xmlTempResult != null)
            xmlTempResult.delete();
        IOUtils.close(inputStream);
        try {
            if (ftp != null)
                if (ftp.isConnected())
                    ftp.disconnect();
        } catch (IOException e) {
            Logging.warn(e);
        }
        if (httpDownloader != null)
            httpDownloader.release();
    }
}

From source file:ilarkesto.integration.ftp.FtpClient.java

public List<FTPFile> listFiles(String path) {
    ArrayList<FTPFile> ret = new ArrayList<FTPFile>();
    try {/*  w ww . ja  va 2  s .  c  o  m*/
        for (FTPFile file : client.listFiles(path)) {
            String name = file.getName();
            if (name.equals("."))
                continue;
            if (name.equals(".."))
                continue;
            ret.add(file);
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    return ret;
}

From source file:domain.FTP.java

public void descargarArchivos() {

    log.log("descargando Archivos de " + downloadpath, false);

    archivosFTP = null;//from www . j  a  va  2 s .com
    try {

        ftpClient.connect(server, port);
        ftpClient.login(user, password);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
        ftpClient.changeWorkingDirectory(downloadpath);

        archivosFTP = ftpClient.listFiles();

        ftpClient.logout();
        ftpClient.disconnect();
    } catch (IOException ex) {

        log.log(ex.getMessage(), true);
        Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
    }

    Integer porcentaje = 0;
    Integer conteo = 1;
    Integer fin;
    /*
     2 - 9
     0 - 7
     */
    fin = archivosFTP.length;

    for (FTPFile arch : archivosFTP) {

        String ls_nombre_archivo = arch.getName();
        System.out.println("tempfiles\\" + ls_nombre_archivo);
        // log.log("Bajando Archivo: "+ls_nombre_archivo);            
        descargarArchivo(ls_nombre_archivo);

        porcentaje = ((conteo * 7) / fin) + 2;

        conteo++;

        ventana.setBar(porcentaje);
    }
}

From source file:ch.cyberduck.core.ftp.FTPListResponseReader.java

@Override
public AttributedList<Path> read(final Path directory, final List<String> replies,
        final ListProgressListener listener)
        throws IOException, FTPInvalidListException, ConnectionCanceledException {
    final AttributedList<Path> children = new AttributedList<Path>();
    // At least one entry successfully parsed
    boolean success = false;
    // Call hook for those implementors which need to perform some action upon the list after it has been created
    // from the server stream, but before any clients see the list
    parser.preParse(replies);/*from  ww  w.j a va  2  s.c o m*/
    for (String line : replies) {
        final FTPFile f = parser.parseFTPEntry(line);
        if (null == f) {
            continue;
        }
        final String name = f.getName();
        if (!success) {
            if (lenient) {
                // Workaround for #2410. STAT only returns ls of directory itself
                // Workaround for #2434. STAT of symbolic link directory only lists the directory itself.
                if (directory.getName().equals(name)) {
                    log.warn(String.format("Skip %s matching parent directory name", f.getName()));
                    continue;
                }
                if (name.contains(String.valueOf(Path.DELIMITER))) {
                    if (!name.startsWith(directory.getAbsolute() + Path.DELIMITER)) {
                        // Workaround for #2434.
                        log.warn(String.format("Skip %s with delimiter in name", name));
                        continue;
                    }
                }
            }
        }
        success = true;
        if (name.equals(".") || name.equals("..")) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Skip %s", f.getName()));
            }
            continue;
        }
        final Path parsed = new Path(directory, PathNormalizer.name(name),
                f.getType() == FTPFile.DIRECTORY_TYPE ? EnumSet.of(Path.Type.directory)
                        : EnumSet.of(Path.Type.file));
        switch (f.getType()) {
        case FTPFile.SYMBOLIC_LINK_TYPE:
            parsed.setType(EnumSet.of(Path.Type.file, Path.Type.symboliclink));
            // Symbolic link target may be an absolute or relative path
            final String target = f.getLink();
            if (StringUtils.isBlank(target)) {
                log.warn(String.format("Missing symbolic link target for %s", parsed));
                final EnumSet<AbstractPath.Type> type = parsed.getType();
                type.remove(AbstractPath.Type.symboliclink);
            } else if (StringUtils.startsWith(target, String.valueOf(Path.DELIMITER))) {
                parsed.setSymlinkTarget(new Path(target, EnumSet.of(Path.Type.file)));
            } else if (StringUtils.equals("..", target)) {
                parsed.setSymlinkTarget(directory);
            } else if (StringUtils.equals(".", target)) {
                parsed.setSymlinkTarget(parsed);
            } else {
                parsed.setSymlinkTarget(new Path(directory, target, EnumSet.of(Path.Type.file)));
            }
            break;
        }
        if (parsed.isFile()) {
            parsed.attributes().setSize(f.getSize());
        }
        parsed.attributes().setOwner(f.getUser());
        parsed.attributes().setGroup(f.getGroup());
        Permission.Action u = Permission.Action.none;
        if (f.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION)) {
            u = u.or(Permission.Action.read);
        }
        if (f.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION)) {
            u = u.or(Permission.Action.write);
        }
        if (f.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION)) {
            u = u.or(Permission.Action.execute);
        }
        Permission.Action g = Permission.Action.none;
        if (f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION)) {
            g = g.or(Permission.Action.read);
        }
        if (f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION)) {
            g = g.or(Permission.Action.write);
        }
        if (f.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION)) {
            g = g.or(Permission.Action.execute);
        }
        Permission.Action o = Permission.Action.none;
        if (f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION)) {
            o = o.or(Permission.Action.read);
        }
        if (f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION)) {
            o = o.or(Permission.Action.write);
        }
        if (f.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION)) {
            o = o.or(Permission.Action.execute);
        }
        final Permission permission = new Permission(u, g, o);
        if (f instanceof FTPExtendedFile) {
            permission.setSetuid(((FTPExtendedFile) f).isSetuid());
            permission.setSetgid(((FTPExtendedFile) f).isSetgid());
            permission.setSticky(((FTPExtendedFile) f).isSticky());
        }
        parsed.attributes().setPermission(permission);
        final Calendar timestamp = f.getTimestamp();
        if (timestamp != null) {
            parsed.attributes().setModificationDate(timestamp.getTimeInMillis());
        }
        children.add(parsed);
    }
    if (!success) {
        throw new FTPInvalidListException(children);
    }
    return children;
}

From source file:at.beris.virtualfile.client.ftp.FtpClient.java

@Override
public FTPFile getFileInfo(final String path) throws IOException {
    LOGGER.debug("getFileInfo (path: {})", path);
    return executionHandler(new Callable<FTPFile>() {
        @Override//  w ww. ja  v a 2s . c  o m
        public FTPFile call() throws Exception {
            if ("/".equals(path)) {
                FTPFile rootFile = new FTPFile();
                rootFile.setName("/");
                rootFile.setTimestamp(GregorianCalendar.getInstance());
                return rootFile;
            } else {
                String lastPathPart = UrlUtils.getLastPathPart(path);
                String parentPath = UrlUtils.getParentPath(path);
                ftpClient.changeWorkingDirectory(parentPath);
                FTPFile[] ftpFiles = ftpClient.listFiles();
                for (FTPFile ftpFile : ftpFiles) {
                    if (ftpFile.getName().equals(lastPathPart)) {
                        return ftpFile;
                    }
                }
                return new FTPFile();
            }
        }
    });
}