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

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

Introduction

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

Prototype

public Calendar getTimestamp() 

Source Link

Document

Returns the file timestamp.

Usage

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

@Test
public void testWindowsNTSystem() {
    FTPFileEntryParser parser = new FTPParserSelector().getParser("Windows_NT version 5.0");

    FTPFile parsed;

    // #5505/*from  w w w . ja va2  s  . co m*/
    parsed = parser.parseFTPEntry(
            "drwxrwxrwx   1 owner    group               0 Dec  5  0:45 adele.handmadebyflloyd.com");
    assertNotNull(parsed);
    assertEquals("adele.handmadebyflloyd.com", parsed.getName());
    assertEquals("owner", parsed.getUser());
    assertEquals("group", parsed.getGroup());
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.DECEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(5, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    // #5505
    parsed = parser.parseFTPEntry("drwxrwxrwx   1 owner    group               0 Jan 22  2009 contact");
    assertNotNull(parsed);
    assertEquals("contact", parsed.getName());
    assertEquals("owner", parsed.getUser());
    assertEquals("group", parsed.getGroup());
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.JANUARY, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(22, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertEquals(2009, parsed.getTimestamp().get(Calendar.YEAR));
}

From source file:com.connection.factory.FtpConnectionApacheLib.java

@Override
public List<RemoteFileObject> readAllFilesInCurrentPath(String remotePath) {
    List<RemoteFileObject> willReturnObject = new ArrayList<>();
    try {/*from  w  w w  . ja  v  a 2s  .  c  o  m*/
        FTPFile[] fileListTemp = _ftpObj.listFiles(remotePath);
        for (FTPFile each : fileListTemp) {
            RemoteFileObject objectTemp = null;
            if (each.isFile()) {

                objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                //  System.out.println(each);
                objectTemp.setFileName(each.getName());
                objectTemp.setAbsolutePath(remotePath);
                objectTemp.setFileSize(each.getSize());
                objectTemp.setFileType();
                objectTemp.setDate(each.getTimestamp().getTime());
                willReturnObject.add(objectTemp);
            }
        }
    } catch (IOException | ConnectionException ex) {
        return null;
    }
    return willReturnObject.isEmpty() ? null : willReturnObject;
}

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

@Test
public void testMVSParser() {
    FTPFileEntryParser parser = new FTPParserSelector()
            .getParser("MVS is the operating system of this server. FTP Server is running on z/OS.");

    FTPFile parsed;

    final String entry = "drwxr-xr-x   6 START2   SYS1        8192 Oct 28  2008 ADCD";
    parser.preParse(new ArrayList<String>(Arrays.asList("total 66", entry)));
    // #7717/*w ww  .  jav a 2  s.com*/
    parsed = parser.parseFTPEntry(entry);
    assertNotNull(parsed);
    assertEquals("ADCD", parsed.getName());
    assertEquals("START2", parsed.getUser());
    assertEquals("SYS1", parsed.getGroup());
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.OCTOBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(28, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertEquals(2008, parsed.getTimestamp().get(Calendar.YEAR));
}

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

/**
 * http://trac.cyberduck.ch/ticket/1076/*from w ww  . j a v a  2s . c  o  m*/
 *
 * @
 */
@Test
public void testSizeWithIndicator() {
    FTPFileEntryParser parser = new FTPParserSelector().getParser("UNIX");

    FTPFile parsed;

    parsed = parser.parseFTPEntry("-rw-rw-rw- 1 ftp operator 9.0M Mar 22 17:44 Cyberduck-2.7.3.dmg");
    assertNotNull(parsed);
    assertTrue(parsed.isFile());
    assertEquals("Cyberduck-2.7.3.dmg", parsed.getName());
    assertEquals((long) (9.0 * 1048576), parsed.getSize());
    assertEquals(parsed.getUser(), "ftp");
    assertEquals(parsed.getGroup(), "operator");
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.MARCH, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(22, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    parsed = parser.parseFTPEntry("-rw-rw-rw- 1 ftp operator 61.8M Mar 7 18:42 GC Wayfinding pics.zip ");
    assertNotNull(parsed);
    assertTrue(parsed.isFile());
    assertEquals((long) (61.8 * 1048576), parsed.getSize());
    assertEquals("ftp", parsed.getUser());
    assertEquals("operator", parsed.getGroup());
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.MARCH, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(7, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    parsed = parser.parseFTPEntry("-rw-rw-rw- 1 ftp operator 172.4k Mar 7 16:01 HEALY071.TXT ");
    assertNotNull(parsed);
    assertTrue(parsed.isFile());
    assertEquals((long) (172.4 * 1024), parsed.getSize());
    assertEquals(parsed.getUser(), "ftp");
    assertEquals(parsed.getGroup(), "operator");
    assertNotNull(parsed.getTimestamp());
    assertEquals(Calendar.MARCH, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(7, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
}

From source file:it.greenvulcano.util.file.RegExFileFilter.java

public boolean accept(FTPFile file) {
    boolean fileTypeMatches = false;
    boolean nameMatches = false;
    boolean isModified = false;

    boolean isFile = !file.isDirectory();
    fileTypeMatches = ((fileType == ALL) || ((fileType == FILES_ONLY) && isFile)
            || ((fileType == DIRECTORIES_ONLY) && !isFile));

    if (fileTypeMatches) {
        if (pattern != null) {
            Matcher m = pattern.matcher(file.getName());
            nameMatches = m.matches();/*from  w w w. java  2  s .  c  om*/
        } else {
            nameMatches = true;
        }

        if (nameMatches) {
            if (checkLastModified) {
                isModified = selectModifiedSince ? (file.getTimestamp().getTimeInMillis() > lastTimestamp)
                        : (file.getTimestamp().getTimeInMillis() <= lastTimestamp);
            } else {
                isModified = true;
            }
        }
    }

    return fileTypeMatches && nameMatches && isModified;
}

From source file:com.toolsverse.io.FtpUtils.java

/**
 * Gets the list of FileResource objects from the folder + filename. filename can be a mask, for example: /usr/test/*.txt. 
 * If includeFolders == true recursively includes sub-folders. 
 *
 * @see com.toolsverse.io.FileResource/*  w w  w .j  a  v a  2s . c  o m*/
 *
 * @param folder the folder
 * @param filename the filename
 * @param includeFolders the include folders flag. If equals to true recursively includes sub-folders
 * @return the list of FileResource objects
 * @throws Exception in case of any error
 */
public List<FileResource> getFileList(String folder, String filename, boolean includeFolders) throws Exception {
    List<FileResource> remoteFileList = new ArrayList<FileResource>();

    folder = !Utils.isNothing(folder) ? folder : ".";

    FTPFile[] files = ftpClient.listFiles(folder);

    if (files == null || files.length == 0)
        return null;

    for (FTPFile file : files) {
        boolean isDir = file.isDirectory();

        if ((includeFolders || !isDir)
                && FilenameUtils.wildcardMatch(file.getName(), filename, IOCase.INSENSITIVE)) {
            FileResource fileResource = new FileResource();

            if (Utils.isNothing(file.getName()) || "..".equalsIgnoreCase(file.getName())
                    || ".".equalsIgnoreCase(file.getName()))
                continue;

            fileResource.setPath(folder + "/" + file.getName());
            fileResource.setName(file.getName());
            fileResource.setIsDirectory(isDir);
            fileResource.setSize(!isDir ? file.getSize() : 0);
            fileResource
                    .setLastModified(file.getTimestamp() != null ? file.getTimestamp().getTimeInMillis() : 0);

            remoteFileList.add(fileResource);
        }
    }

    return remoteFileList;
}

From source file:dk.dma.dmiweather.service.FTPLoader.java

/**
 * Copied the files from DMIs ftp server to the local machine
 *
 * @return a Map with a local file and the time the file was created on the FTP server
 */// w  w  w . ja va2  s . c o  m
private Map<File, Instant> transferFilesIfNeeded(FTPClient client, String directoryName, List<FTPFile> files)
        throws IOException {

    File current = new File(tempDirLocation, directoryName);
    if (newestDirectories.isEmpty()) {
        // If we just started check if there is data from an earlier run and delete it
        File temp = new File(tempDirLocation);
        if (temp.exists()) {
            File[] oldFolders = temp.listFiles(new PatternFilenameFilter(FOLDER_PATTERN));
            if (oldFolders != null) {
                List<File> foldersToDelete = Lists.newArrayList(oldFolders);
                foldersToDelete.remove(current);
                for (File oldFolder : foldersToDelete) {
                    log.info("deleting old GRIB folder {}", oldFolder);
                    deleteRecursively(oldFolder);
                }
            }
        }
    }

    if (!current.exists()) {
        if (!current.mkdirs()) {
            throw new IOException("Unable to create temp directory " + current.getAbsolutePath());
        }
    }

    Stopwatch stopwatch = Stopwatch.createStarted();
    Map<File, Instant> transferred = new HashMap<>();
    for (FTPFile file : files) {
        File tmp = new File(current, file.getName());
        if (tmp.exists()) {
            long localSize = Files.size(tmp.toPath());
            if (localSize != file.getSize()) {
                log.info("deleting {} local file has size {}, remote is {}", tmp.getName(), localSize,
                        file.getSize());
                if (!tmp.delete()) {
                    log.warn("Unable to delete " + tmp.getAbsolutePath());
                }
            } else {
                // If the file has the right size we assume it was copied correctly (otherwise we needed to hash them)
                log.info("Reusing already downloaded version of {}", tmp.getName());
                transferred.put(tmp, file.getTimestamp().toInstant());
                continue;
            }
        }
        if (tmp.createNewFile()) {
            log.info("downloading {}", tmp.getName());

            // this often fails with java.net.ConnectException: Operation timed out
            int count = 0;
            while (count++ < MAX_TRIES) {
                try (FileOutputStream fout = new FileOutputStream(tmp)) {
                    client.retrieveFile(file.getName(), fout);
                    fout.flush();
                    break;
                } catch (IOException e) {
                    log.warn(String.format("Failed to transfer file %s, try number %s", file.getName(), count),
                            e);
                }
            }
        } else {
            throw new IOException("Unable to create temp file on disk.");
        }

        transferred.put(tmp, file.getTimestamp().toInstant());
    }
    log.info("transferred weather files in {} ms", stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
    return transferred;
}

From source file:com.connection.factory.FtpConnectionApacheLib.java

@Override
public List<RemoteFileObject> readAllFilesWalkinPath(String remotePath) {
    List<RemoteFileObject> willReturnObject = new ArrayList<>();
    Queue<RemoteFileObject> directorylist = new LinkedBlockingQueue<>();
    RemoteFileObject object = null;/*  w  w w .  j  a  v  a2s  .c o  m*/
    object = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
    object.setDirectPath(remotePath);
    directorylist.add(object);
    try {
        while (!directorylist.isEmpty()) {
            object = directorylist.poll();
            FTPFile[] fileListTemp = _ftpObj.listFiles(object.getPath());
            for (FTPFile each : fileListTemp) {
                RemoteFileObject objectTemp = null;
                if (each.isDirectory()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(object.getPath());
                    directorylist.add(objectTemp);
                } else if (each.isFile()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(object.getPath());
                    objectTemp.setFileSize(each.getSize());
                    objectTemp.setFileType();
                    objectTemp.setDate(each.getTimestamp().getTime());
                    willReturnObject.add(objectTemp);
                }
            }
            object = null;
            fileListTemp = null;
        }

    } catch (IOException ex) {
        return null;
    } catch (ConnectionException ex) {

    }
    return willReturnObject;
}

From source file:com.connection.factory.FtpConnectionApacheLib.java

@Override
public void readAllFilesWalkingPathWithListener(FileListener listener, String remotePath) {
    // List<RemoteFileObject> willReturnObject = new ArrayList<>();
    Queue<RemoteFileObject> directorylist = new LinkedBlockingQueue<>();
    RemoteFileObject object = null;//from ww  w . ja v a2  s. c  o m
    object = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
    object.setDirectPath(remotePath);
    directorylist.add(object);
    try {
        while (!directorylist.isEmpty()) {
            object = directorylist.poll();
            FTPFile[] fileListTemp = _ftpObj.listFiles(object.getPath());
            for (FTPFile each : fileListTemp) {
                RemoteFileObject objectTemp = null;
                if (each.isDirectory()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(object.getPath());
                    directorylist.add(objectTemp);
                } else if (each.isFile()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(object.getPath());
                    objectTemp.setFileSize(each.getSize());
                    objectTemp.setFileType();
                    objectTemp.setDate(each.getTimestamp().getTime());
                    listener.handleRemoteFile(object);
                }
            }
            object = null;
            fileListTemp = null;
        }

    } catch (IOException | ConnectionException ex) {
        //    return null;
    }
    //  return willReturnObject;

    //  return willReturnObject;
}

From source file:lucee.runtime.tag.Ftp.java

/**
 * List data of a ftp connection/*from  w w w.j  a va  2  s.co m*/
 * @return FTPCLient
 * @throws PageException
 * @throws IOException
 */
private FTPClient actionListDir() throws PageException, IOException {
    required("name", name);
    required("directory", directory);

    FTPClient client = getClient();
    FTPFile[] files = client.listFiles(directory);
    if (files == null)
        files = new FTPFile[0];

    String[] cols = new String[] { "attributes", "isdirectory", "lastmodified", "length", "mode", "name",
            "path", "url", "type", "raw" };
    String[] types = new String[] { "VARCHAR", "BOOLEAN", "DATE", "DOUBLE", "VARCHAR", "VARCHAR", "VARCHAR",
            "VARCHAR", "VARCHAR", "VARCHAR" };

    lucee.runtime.type.Query query = new QueryImpl(cols, types, 0, "query");

    // translate directory path for display
    if (directory.length() == 0)
        directory = "/";
    else if (directory.startsWith("./"))
        directory = directory.substring(1);
    else if (directory.charAt(0) != '/')
        directory = '/' + directory;
    if (directory.charAt(directory.length() - 1) != '/')
        directory = directory + '/';

    pageContext.setVariable(name, query);
    int row = 0;
    for (int i = 0; i < files.length; i++) {
        FTPFile file = files[i];
        if (file.getName().equals(".") || file.getName().equals(".."))
            continue;
        query.addRow();
        row++;
        query.setAt("attributes", row, "");
        query.setAt("isdirectory", row, Caster.toBoolean(file.isDirectory()));
        query.setAt("lastmodified", row, new DateTimeImpl(file.getTimestamp()));
        query.setAt("length", row, Caster.toDouble(file.getSize()));
        query.setAt("mode", row, FTPConstant.getPermissionASInteger(file));
        query.setAt("type", row, FTPConstant.getTypeAsString(file.getType()));
        //query.setAt("permission",row,FTPConstant.getPermissionASInteger(file));
        query.setAt("raw", row, file.getRawListing());
        query.setAt("name", row, file.getName());
        query.setAt("path", row, directory + file.getName());
        query.setAt("url", row,
                "ftp://" + client.getRemoteAddress().getHostName() + "" + directory + file.getName());
    }
    writeCfftp(client);
    return client;
}