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

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

Introduction

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

Prototype

public long getSize() 

Source Link

Document

Return the file size in bytes.

Usage

From source file:nl.esciencecenter.xenon.adaptors.filesystems.ftp.FtpFileSystem.java

private PathAttributes convertAttributes(Path path, FTPFile attributes) {

    PathAttributesImplementation result = new PathAttributesImplementation();

    result.setPath(path);/*w  w  w  .  j  av a 2 s  .  c  o  m*/
    result.setDirectory(attributes.isDirectory());
    result.setRegular(attributes.isFile());
    result.setOther(attributes.isUnknown());
    result.setSymbolicLink(attributes.isSymbolicLink());

    result.setLastModifiedTime(attributes.getTimestamp().getTimeInMillis());
    result.setCreationTime(attributes.getTimestamp().getTimeInMillis());
    result.setLastAccessTime(attributes.getTimestamp().getTimeInMillis());

    result.setSize(attributes.getSize());

    Set<PosixFilePermission> permission = getPermissions(attributes);

    result.setExecutable(permission.contains(PosixFilePermission.OWNER_EXECUTE));
    result.setReadable(permission.contains(PosixFilePermission.OWNER_READ));
    result.setWritable(permission.contains(PosixFilePermission.OWNER_WRITE));

    result.setPermissions(permission);

    result.setGroup(attributes.getGroup());
    result.setOwner(attributes.getUser());

    return result;
}

From source file:nl.nn.adapterframework.filesystem.FtpFileSystem.java

@Override
public long getFileSize(FTPFile f, boolean isFolder) throws FileSystemException {
    return f.getSize();
}

From source file:onl.area51.filesystem.ftp.client.FTPClient.java

/**
 * Simple test to see if a remote file should be retrieved.
 * <p>/* w w w. ja  va  2 s  .c o m*/
 * This returns true if file does not exist, if the lengths don't match or the remote file's date is newer than the local file
 * <p>
 * @param file
 * @param ftp
 *             <p>
 * @return
 */
default boolean isFileRetrievable(File file, FTPFile ftp) {
    return !file.exists() || file.length() != ftp.getSize()
            || file.lastModified() < ftp.getTimestamp().getTimeInMillis();
}

From source file:onl.area51.filesystem.ftp.client.FTPClient.java

/**
 * Simple test to see if a remote file should be retrieved.
 * <p>/*from  www  . j  a va2 s.co  m*/
 * This returns true if file does not exist, if the lengths don't match or the remote file's date is newer than the local file
 * <p>
 * @param file
 * @param ftp
 *             <p>
 * @return
 */
default boolean isPathRetrievable(Path file, FTPFile ftp) throws IOException {
    return !Files.exists(file, LinkOption.NOFOLLOW_LINKS) || Files.size(file) != ftp.getSize()
            || Files.getLastModifiedTime(file, LinkOption.NOFOLLOW_LINKS).toMillis() < ftp.getTimestamp()
                    .getTimeInMillis();
}

From source file:onl.area51.gfs.grib2.job.GribRetriever.java

public Path retrieveOffset(Path dir, int offset, boolean forceRetrive) throws IOException {
    LOG.log(Level.INFO, () -> "Looking for GFS file for offset " + offset);
    String ending = String.format("z.pgrb2.0p25.f%03d", offset);

    @SuppressWarnings("ThrowableInstanceNotThrown")
    FTPFile remote = client.files().filter(f -> f.getName().startsWith("gfs.t"))
            .filter(f -> f.getName().endsWith(ending)).peek(f -> LOG.warning(f.getName())).findAny()
            .orElseThrow(() -> new FileNotFoundException("Unable to find GFS file for " + offset));

    Path file = dir.resolve(remote.getName());

    if (forceRetrive || client.isPathRetrievable(file, remote)) {
        LOG.log(Level.INFO, () -> "Retrieving " + remote.getSize() + " bytes to " + file);

        try (InputStream is = client.retrieveFileStream(remote)) {
            Files.copy(is, file, StandardCopyOption.REPLACE_EXISTING);
        }/*from  w  ww .  ja v  a  2 s.c o m*/

        LOG.log(Level.INFO, () -> "Retrieved " + remote.getSize() + " bytes");
    } else {
        LOG.log(Level.INFO, () -> "Skipping retrieval as local file is newer");
    }

    return file;
}

From source file:onl.area51.gfs.mapviewer.action.ImportGribAction.java

private void selectDestName(FTPClient client, FTPFile remoteFile) throws Exception {
    SwingUtilities.invokeLater(() -> {
        // Default to the remote file name
        fileChooser.setSelectedFile(new File(remoteFile.getName()));

        if (fileChooser.showSaveDialog(Main.getFrame()) == JFileChooser.APPROVE_OPTION) {
            File localFile = fileChooser.getSelectedFile();

            ProgressDialog.copy(remoteFile.getName(), remoteFile.getSize(),
                    () -> client.retrieveFileStream(remoteFile.getName()), () -> {
                        Main.setStatus("Disconnecting, transfer complete.");
                        SwingUtils.executeTask(client::close);
                        OpenGribAction.getInstance().open(localFile);
                    }, () -> {/*from  ww w.j  a  va2  s  . c  om*/
                        Main.setStatus("Disconnecting, transfer failed");
                        SwingUtils.executeTask(client::close);
                    }, localFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } else {
            Main.setStatus("Disconnecting, no transfer performed.");
            SwingUtils.executeTask(client::close);
        }

    });
}

From source file:org.abstracthorizon.proximity.storage.remote.CommonsNetFtpRemotePeer.java

/**
 * Construct item properties from get response.
 * /*from   w  w  w . j av a  2  s  . co m*/
 * @param path the path
 * @param originatingUrlString the originating url string
 * @param remoteFile the remote file
 * 
 * @return the item properties
 * 
 * @throws MalformedURLException the malformed URL exception
 */
protected ItemProperties constructItemPropertiesFromGetResponse(String path, String originatingUrlString,
        FTPFile remoteFile) throws MalformedURLException {
    URL originatingUrl = new URL(originatingUrlString);
    ItemProperties result = new HashMapItemPropertiesImpl();
    result.setDirectoryPath(
            FilenameUtils.separatorsToUnix(FilenameUtils.getPath(FilenameUtils.getFullPath(path))));
    result.setDirectory(remoteFile.isDirectory());
    result.setFile(remoteFile.isFile());
    result.setLastModified(remoteFile.getTimestamp().getTime());
    result.setName(FilenameUtils.getName(originatingUrl.getPath()));
    if (result.isFile()) {
        result.setSize(remoteFile.getSize());
    } else {
        result.setSize(0);
    }
    result.setRemoteUrl(originatingUrl.toString());
    return result;
}

From source file:org.alfresco.bm.file.FtpTestFileService.java

/**
 * Does a listing of files on the FTP server
 *///  w  w w .  j ava2  s  .  c  o m
@Override
protected List<FileData> listRemoteFiles() {
    // Get a list of files from the FTP server
    FTPClient ftp = null;
    FTPFile[] ftpFiles = new FTPFile[0];
    try {
        ftp = getFTPClient();
        if (!ftp.changeWorkingDirectory(ftpPath)) {
            throw new IOException("Failed to change directory (leading '/' could be a problem): " + ftpPath);
        }
        ftpFiles = ftp.listFiles();
    } catch (IOException e) {
        throw new RuntimeException("FTP file listing failed: " + this, e);
    } finally {
        try {
            if (null != ftp) {
                ftp.logout();
                ftp.disconnect();
            }
        } catch (IOException e) {
            logger.warn("Failed to close FTP connection: " + e.getMessage());
        }
    }
    // Index each of the files
    List<FileData> remoteFileDatas = new ArrayList<FileData>(ftpFiles.length);
    for (FTPFile ftpFile : ftpFiles) {
        String ftpFilename = ftpFile.getName();
        // Watch out for . and ..
        if (ftpFilename.equals(".") || ftpFilename.equals("..")) {
            continue;
        }
        String ftpExtension = FileData.getExtension(ftpFilename);
        long ftpSize = ftpFile.getSize();

        FileData remoteFileData = new FileData();
        remoteFileData.setRemoteName(ftpFilename);
        remoteFileData.setExtension(ftpExtension);
        remoteFileData.setSize(ftpSize);

        remoteFileDatas.add(remoteFileData);
    }
    // Done
    return remoteFileDatas;
}

From source file:org.apache.camel.component.file.remote.FtpConsumer.java

private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file) {
    RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>();

    answer.setEndpointPath(endpointPath);
    answer.setFile(file);//  ww  w .j a  v  a  2s.  com
    answer.setFileNameOnly(file.getName());
    answer.setFileLength(file.getSize());
    answer.setDirectory(file.isDirectory());
    if (file.getTimestamp() != null) {
        answer.setLastModified(file.getTimestamp().getTimeInMillis());
    }
    answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost());

    // absolute or relative path
    boolean absolute = FileUtil.hasLeadingSeparator(absolutePath);
    answer.setAbsolute(absolute);

    // create a pseudo absolute name
    String dir = FileUtil.stripTrailingSeparator(absolutePath);
    String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getName());
    // if absolute start with a leading separator otherwise let it be relative
    if (absolute) {
        absoluteFileName = "/" + absoluteFileName;
    }
    answer.setAbsoluteFilePath(absoluteFileName);

    // the relative filename, skip the leading endpoint configured path
    String relativePath = ObjectHelper.after(absoluteFileName, endpointPath);
    // skip leading /
    relativePath = FileUtil.stripLeadingSeparator(relativePath);
    answer.setRelativeFilePath(relativePath);

    // the file name should be the relative path
    answer.setFileName(answer.getRelativeFilePath());

    answer.setCharset(endpoint.getCharset());
    return answer;
}

From source file:org.apache.camel.component.file.remote.strategy.CachedFtpChangedExclusiveReadLockStrategy.java

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file,
        Exchange exchange) throws Exception {

    boolean exclusive = false;

    FTPFile target = file.getFile();/* ww w .j a  va  2s  .  co m*/

    FTPFile newStats = target;
    FTPFile oldStats = fileCache.get(target.getName());

    if (oldStats != null) {
        // There is no "created time" available when using FTP. So we can't check the minAge.
        if (newStats.getSize() >= getMinLength()
                && (oldStats.getSize() == newStats.getSize()
                        && oldStats.getTimestamp().equals(newStats.getTimestamp()))
                && ((System.currentTimeMillis()
                        - newStats.getTimestamp().getTimeInMillis()) >= getCheckInterval())) {
            LOG.debug("File [{}] seems to have stopped changing. Attempting to grab a read lock...", target);
            exclusive = true;
        } else {
            LOG.debug("File [{}] is still changing. Will check it again on the next poll.", target);
            fileCache.put(target.getName(), newStats);
            exclusive = false;
        }
    } else {
        LOG.debug("File [{}] is not yet known. Will add it to the cache and check again on the next poll.",
                target);
        fileCache.put(target.getName(), newStats);
        exclusive = false;
    }

    if (exclusive) {
        LOG.debug("Got an exclusive lock for file [{}].", target);
        fileCache.remove(target.getName());
    }
    return exclusive;
}