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.mule.transport.ftps.FtpsMuleMessageFactory.java

@Override
protected String getMimeType(Object transportMessage) {
    if (transportMessage instanceof FTPFile) {
        FTPFile file = (FTPFile) transportMessage;

        return mimetypesFileTypeMap.getContentType(file.getName().toLowerCase());
    } else {//from  w ww. ja va 2 s .  c  om
        return null;
    }
}

From source file:org.nabucco.testautomation.engine.proxy.process.command.ftp.ListFilesCommand.java

private void addFileList(String pathname, FTPFile[] files, PropertyList properties) throws FTPException {

    if (files == null) {
        throw new FTPException("No fileList received");
    }/*w  w w  . ja va  2s.c o  m*/

    PropertyList directory = (PropertyList) PropertyFactory.getInstance().produceProperty(PropertyType.LIST);
    directory.setName(pathname);
    add(directory, properties);
    int i = 0;

    for (FTPFile file : files) {
        debug("File " + i + ": " + file.getName());
        TextProperty prop = (TextProperty) PropertyFactory.getInstance().produceProperty(PropertyType.TEXT);
        prop.setName("" + i++);
        prop.setValue(new File(file.getName()).getName());
        add(prop, directory);
    }
}

From source file:org.onebusaway.transit_data_federation.impl.realtime.orbcad.OrbcadRecordFtpSource.java

private List<String> getUpdatedFilesToDownload() throws IOException {
    long t1 = System.currentTimeMillis();

    FTPListParseEngine engine = _ftpClient.initiateListParsing(_dataDirectory);

    Set<String> paths = new HashSet<String>();
    List<String> toDownload = new ArrayList<String>();

    while (engine.hasNext()) {
        FTPFile[] files = engine.getNext(25); // "page size" you want
        for (FTPFile file : files) {
            String path = _dataDirectory + "/" + file.getName();
            paths.add(path);/* w w  w.  j  a  v  a  2 s  .com*/
            if (!_paths.contains(path))
                toDownload.add(path);
        }
    }

    _totalFtpFiles = paths.size();
    _newFtpFiles = toDownload.size();

    long t2 = System.currentTimeMillis();

    if (_log.isDebugEnabled())
        _log.debug("file listing time: " + (t2 - t1) + " totalFiles: " + paths.size() + " newFiles: "
                + toDownload.size());

    _paths = paths;

    if (_maxDownloadCount > 0 && toDownload.size() > _maxDownloadCount) {
        List<String> reduced = new ArrayList<String>(_maxDownloadCount);
        for (int i = 0; i < _maxDownloadCount; i++)
            reduced.add(toDownload.get(toDownload.size() - _maxDownloadCount + i));
        toDownload = reduced;
    }

    return toDownload;
}

From source file:org.openconcerto.ftp.FTPUtils.java

static public final void saveR(FTPClient ftp, File local) throws IOException {
    local.mkdirs();// w w w . java  2s.com
    for (FTPFile child : ftp.listFiles()) {
        final String childName = child.getName();
        if (childName.indexOf('.') != 0) {
            if (child.isDirectory()) {
                ftp.changeWorkingDirectory(childName);
                saveR(ftp, new File(local, childName));
                ftp.changeToParentDirectory();
            } else {
                final OutputStream outs = new FileOutputStream(new File(local, childName));
                ftp.retrieveFile(childName, outs);
                outs.close();
            }
        }
    }
}

From source file:org.openconcerto.ftp.FTPUtils.java

static public final void rmR(final FTPClient ftp, final String toRm) throws IOException {
    final String cwd = ftp.printWorkingDirectory();
    // si on ne peut cd, le dossier n'existe pas
    if (ftp.changeWorkingDirectory(toRm)) {
        recurse(ftp, new ExnClosure<FTPFile, IOException>() {
            @Override/*from w  w w. jav a2  s .  com*/
            public void executeChecked(FTPFile input) throws IOException {
                final boolean res;
                if (input.isDirectory())
                    res = ftp.removeDirectory(input.getName());
                else
                    res = ftp.deleteFile(input.getName());
                if (!res)
                    throw new IOException("unable to delete " + input);
            }
        }, RecursionType.DEPTH_FIRST);
    }
    ftp.changeWorkingDirectory(cwd);
    ftp.removeDirectory(toRm);
}

From source file:org.openconcerto.ftp.FTPUtils.java

static public final void recurse(FTPClient ftp, ExnClosure<FTPFile, ?> c, RecursionType type)
        throws IOException {
    for (FTPFile child : ftp.listFiles()) {
        if (child.getName().indexOf('.') != 0) {
            if (type == RecursionType.BREADTH_FIRST)
                c.executeCheckedWithExn(child, IOException.class);
            if (child.isDirectory()) {
                ftp.changeWorkingDirectory(child.getName());
                recurse(ftp, c, type);//  ww  w  .j av a  2 s  .c  om
                ftp.changeToParentDirectory();
            }
            if (type == RecursionType.DEPTH_FIRST)
                c.executeCheckedWithExn(child, IOException.class);
        }
    }
}

From source file:org.openconcerto.ftp.IFtp.java

public List<File> sync(File ldir, final CopyFileListener l, boolean forceUpload)
        throws IOException, NoSuchAlgorithmException {
    // there might be more than one md5 per file, if an error occured during the last sync()
    final ListMap<String, String> nameToMD5s = new ListMap<String, String>();
    final Map<String, FTPFile> ftpFiles = new HashMap<String, FTPFile>();
    final FTPFile[] listFiles = this.listFiles();
    if (listFiles == null) {
        System.out.println("IFtp.sync(): listFiles null :" + ldir.getName());
        return new ArrayList<File>();
    }/*from w  w  w. j  av  a 2  s .  c  o  m*/

    for (int i = 0; i < listFiles.length; i++) {
        FTPFile rFile = listFiles[i];
        // Oui ca arrive!
        if (rFile != null) {
            // some FTP servers returns 450 when NLST an empty directory, so use LIST
            final String name = rFile.getName();
            if (name != null) {
                if (name.endsWith(MD5_SUFFIX)) {
                    // originalName_a045d5e6.md5
                    final int underscore = name.length() - MD5_SUFFIX.length() - MD5_LENGTH - 1;
                    if (underscore >= 0) {
                        final String fname = name.substring(0, underscore);
                        final String md5 = name.substring(underscore + 1, underscore + 1 + MD5_LENGTH);
                        nameToMD5s.add(fname, md5);
                    }
                } else {
                    ftpFiles.put(name, rFile);
                }
            } else {
                System.out.println("IFtp.sync(): rFile.getName() null : [" + i + "]" + ldir.getName());
            }
        } else {
            System.out.println("IFtp.sync(): rFile null : [" + i + "]" + ldir.getName());
        }

    }

    final List<File> uploaded = new ArrayList<File>();
    final List<File> dirs = new ArrayList<File>();
    for (final File lFile : ldir.listFiles()) {
        if (lFile.isFile() && lFile.canRead()) {
            final String lName = lFile.getName();
            final List<String> md5List = nameToMD5s.getNonNull(lName);
            final String lMD5 = MessageDigestUtils.getMD5(lFile);
            boolean shouldUpload = true;
            if (!forceUpload && ftpFiles.containsKey(lFile.getName())) {
                final long lSize = lFile.length();
                final long rSize = ftpFiles.get(lFile.getName()).getSize();
                shouldUpload = lSize != rSize || !lMD5.equalsIgnoreCase(CollectionUtils.getSole(md5List));
            }
            if (shouldUpload) {
                // delete all previous
                for (final String md5 : md5List) {
                    this.deleteFile(md5ToName(lName, md5));
                }
                if (l != null) {
                    final long fileSize = lFile.length();
                    this.streamListeners.set(new CopyStreamAdapter() {
                        @Override
                        public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
                                long streamSize) {
                            l.bytesTransferred(lFile, totalBytesTransferred, bytesTransferred, fileSize);
                        }
                    });
                }
                this.storeFile(lName, new FileInputStream(lFile));
                // don't signal the MD5 file to the listener
                this.streamListeners.set(null);
                this.storeFile(md5ToName(lName, lMD5), new StringInputStream(lMD5 + "\t" + lName));
                uploaded.add(lFile);
            }
        } else if (lFile.isDirectory())
            dirs.add(lFile);
        // ignore other types of file
    }
    for (final File dir : dirs) {
        this.makeDirectory(dir.getName());
        if (!this.changeWorkingDirectory(dir.getName()))
            throw new IllegalStateException("could not cd to " + dir.getName());
        uploaded.addAll(this.sync(dir, l, forceUpload));
        this.changeToParentDirectory();
    }
    return uploaded;
}

From source file:org.openo.commontosca.catalog.ftp.FtpUtil.java

/**
 * download ftp file./* w  w  w .j  a va2  s  .c o  m*/
 * 
 * @param ftpFile ftp file to download
 * @param relativeLocalPath relative local path
 * @param relativeRemotePath relative remote path
 */
private static void downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {
    if (ftpFile.isFile()) {
        if (ftpFile.getName().indexOf("?") == -1) {
            OutputStream outputStream = null;
            try {
                File locaFile = new File(relativeLocalPath + ftpFile.getName());
                if (locaFile.exists()) {
                    return;
                } else {
                    outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName());
                    ftp.retrieveFile(ftpFile.getName(), outputStream);
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (Exception e1) {
                logger.error(e1);
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e1) {
                    logger.error("OutputStream error !");
                }
            }
        }
    } else {
        String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
        String newRemote = new String(relativeRemotePath + ftpFile.getName().toString());
        File fl = new File(newlocalRelatePath);
        if (!fl.exists()) {
            fl.mkdirs();
        }
        try {
            newlocalRelatePath = newlocalRelatePath + '/';
            newRemote = newRemote + "/";
            String currentWorkDir = ftpFile.getName().toString();
            boolean changedir = ftp.changeWorkingDirectory(currentWorkDir);
            if (changedir) {
                FTPFile[] files = null;
                files = ftp.listFiles();
                for (int i = 0; i < files.length; i++) {
                    downloadFile(files[i], newlocalRelatePath, newRemote);
                }
            }
            if (changedir) {
                ftp.changeToParentDirectory();
            }
        } catch (Exception e1) {
            logger.error(e1);
        }
    }
}

From source file:org.openspice.vfs.ftp.FtpVFolder.java

private List list(final boolean want_folders, final boolean want_files) {
    //      System.err.println( "folders = " + want_folders );
    //      System.err.println( "files = " + want_files );
    try {//w w w.  j a v a  2  s. c o  m
        final FTPClient ftpc = this.fvol.getConnectedFTPClient();
        final FTPFile[] files = ftpc.listFiles(this.path);
        //         {
        //            System.err.println( "List of files is " + files.length + " long" );
        //            for ( int n = 0; n < files.length; n++ ) {
        //               System.err.println( "["+n+"]. " + files[n] + " " + ( files[n].isDirectory() ? "d" : "-" ) + ( files[n].isFile() ? "f" : "-" ) );
        //            }
        //         }
        final List answer = new ArrayList();
        for (int i = 0; i < files.length; i++) {
            final FTPFile file = files[i];
            if (want_folders && file.isDirectory()) {
                //               System.err.println( "adding FOLDER " + file );
                answer.add(new FtpVFolder(this.fvol, FtpTools.folderName(this.path, file.getName())));
            } else if (want_files && (file.isFile() || file.isSymbolicLink())) {
                //               System.err.println( "adding FILE " + file );
                answer.add(FtpVFile.uncheckedMake(this.fvol, FtpTools.fileName(this.path, file.getName())));
            }
        }
        return answer;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.oss.bonita.utils.ftp.FtpTransfer.java

public boolean fileExists(FTPFile[] ftpFiles, String fileName) {
    if (ftpFiles != null && ftpFiles.length > 0) {
        for (FTPFile file : ftpFiles) {
            if (file.isFile()) {
                if (fileName.equals(file.getName())) {
                    return true;
                }/*  w  w w .java  2 s.  c  om*/
            }
        }
    }
    return false;
}