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:ch.cyberduck.core.ftp.parser.HPTru64ParserTest.java

/**
 * http://trac.cyberduck.ch/ticket/2246/*w  w w  . j  a  v a  2 s.  c  o m*/
 */
@Test
public void testParse() throws Exception {
    FTPFile parsed;

    parsed = parser.parseFTPEntry("drwxr-xr-x   7 ToysPKG  advertise   8192 Jun 24 11:58 Private Label Mock");
    assertNotNull(parsed);
    assertEquals(parsed.getName(), "Private Label Mock");
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals("ToysPKG", parsed.getUser());
    assertEquals("advertise", parsed.getGroup());
    assertEquals(8192, parsed.getSize());
    assertEquals(Calendar.JUNE, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(24, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1 ToysPKG  advertise24809879 Jun 25 10:54 TRU-Warning Guide Master CD.sitx");
    assertNull(parsed);
}

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
 *//*from   w  ww .  j av  a 2 s .  c  om*/
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:ch.cyberduck.core.ftp.parser.NTFTPEntryParserTest.java

@Test
public void testParseFieldsOnFile() throws Exception {
    FTPFile parsed = parser.parseFTPEntry("05-22-97  12:08AM                  5000000000 AUTOEXEC.BAK");
    assertNotNull("Could not parse entry.", parsed);
    assertEquals("Thu May 22 00:08:00 1997", df.format(parsed.getTimestamp().getTime()));
    assertTrue(parsed.isFile());/*  www  .j  av a 2s.  c o m*/
    assertEquals("AUTOEXEC.BAK", parsed.getName());
    assertEquals(5000000000l, parsed.getSize());
}

From source file:com.dp2345.plugin.ftp.FtpPlugin.java

@Override
public List<FileInfo> browser(String path) {
    List<FileInfo> fileInfos = new ArrayList<FileInfo>();
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        String urlPrefix = pluginConfig.getAttribute("urlPrefix");
        FTPClient ftpClient = new FTPClient();
        try {//www . j  av a 2s.  co  m
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())
                    && ftpClient.changeWorkingDirectory(path)) {
                for (FTPFile ftpFile : ftpClient.listFiles()) {
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.setName(ftpFile.getName());
                    fileInfo.setUrl(urlPrefix + path + ftpFile.getName());
                    fileInfo.setIsDirectory(ftpFile.isDirectory());
                    fileInfo.setSize(ftpFile.getSize());
                    fileInfo.setLastModified(ftpFile.getTimestamp().getTime());
                    fileInfos.add(fileInfo);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                }
            }
        }
    }
    return fileInfos;
}

From source file:com.hibo.bas.fileplugin.file.FtpPlugin.java

@Override
public List<FileInfo> browser(String path) {
    List<FileInfo> fileInfos = new ArrayList<FileInfo>();
    // PluginConfig pluginConfig = getPluginConfig();
    // if (pluginConfig != null) {
    Map<String, String> ftpInfo = getFtpInfo("all");
    String urlPrefix = DataConfig.getConfig("IMGFTPROOT");
    FTPClient ftpClient = new FTPClient();
    try {/*from w w w .  j  a  v  a  2  s  .co m*/
        ftpClient.connect(ftpInfo.get("host"), 21);
        ftpClient.login(ftpInfo.get("username"), ftpInfo.get("password"));
        ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) {
            for (FTPFile ftpFile : ftpClient.listFiles()) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(ftpFile.getName());
                fileInfo.setUrl(urlPrefix + path + ftpFile.getName());
                fileInfo.setIsDirectory(ftpFile.isDirectory());
                fileInfo.setSize(ftpFile.getSize());
                fileInfo.setLastModified(ftpFile.getTimestamp().getTime());
                fileInfos.add(fileInfo);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
            }
        }
    }
    // }
    return fileInfos;
}

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

@Test
public void testParse() {
    FTPFile parsed;

    parsed = parser.parseFTPEntry(//  www. jav  a 2 s  .  co m
            "-rw-r--r--   1  freebox  freebox 2064965868 Apr 15 21:17 M6 - Capital 15-04-2007 21h37 1h40m.ts");
    assertNotNull(parsed);
    assertEquals(parsed.getName(), "M6 - Capital 15-04-2007 21h37 1h40m.ts");
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals("freebox", parsed.getUser());
    assertEquals("freebox", parsed.getGroup());
    assertEquals(2064965868, parsed.getSize());
    assertEquals(Calendar.APRIL, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(15, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 75906880 Sep 08 06:33 Direct 8 - Gym direct - 08-09-2007 08h30 1h08m.ts");
    assertNotNull(parsed);
    assertEquals("Direct 8 - Gym direct - 08-09-2007 08h30 1h08m.ts", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals("freebox", parsed.getUser());
    assertEquals("freebox", parsed.getGroup());
    assertEquals(75906880, parsed.getSize());
    assertEquals(Calendar.SEPTEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(8, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.WRITE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));

    parsed = parser.parseFTPEntry(
            "-rw-r--r--   1  freebox  freebox 1171138668 May 19 17:20 France 3 national - 19-05-2007 18h15 1h05m.ts");
    assertNotNull(parsed);
    assertEquals("France 3 national - 19-05-2007 18h15 1h05m.ts", parsed.getName());
}

From source file:com.knowbout.epg.processor.Downloader.java

private boolean downloadFile(FTPFile remoteFile) throws IOException {
    boolean success = false;
    File file = new File(destinationFolder + File.separator + remoteFile.getName());
    long lastModified = remoteFile.getTimestamp().getTimeInMillis();
    log.debug("Remote file is " + remoteFile.getName() + " local file is " + file.getAbsoluteFile()
            + " does it exist:" + file.exists());
    if (forceDownload || !file.exists() || (file.lastModified() < lastModified)) {
        log.debug("Downloading " + remoteFile.getName() + " " + remoteFile.getSize() + " to "
                + file.getAbsolutePath());

        FileOutputStream fos = new FileOutputStream(file);
        client.retrieveFile(remoteFile.getName(), fos);
        fos.close();/*from  www  . ja  va 2  s  . c om*/
        fos.flush();
        file.setLastModified(lastModified);
        success = true;
    }
    return success;
}

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

@Override
public void readAllFilesInCurrentPathWithListener(FileListener listener, String remotePath) {

    try {//from  w w w  .  j  a  v a2 s .  c  o m
        FTPFile[] fileListTemp = _ftpObj.listFiles(remotePath);
        for (FTPFile each : fileListTemp) {
            RemoteFileObject objectTemp = null;
            if (each.isFile()) {
                objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                objectTemp.setFileName(each.getName());
                objectTemp.setAbsolutePath(remotePath);
                objectTemp.setFileSize(each.getSize());
                objectTemp.setFileType();
                objectTemp.setDate(each.getTimestamp().getTime());
                listener.handleRemoteFile(objectTemp);
            }
        }
    } catch (IOException | ConnectionException ex) {

    }

}

From source file:jcr.FTPParser.java

private void parse(String workingDirectory) throws RepositoryException, IOException, Exception {
    Node node = JcrUtils.getOrCreateByPath(jcrServerNodePath + workingDirectory, null, session);
    String encoded = new String(workingDirectory.getBytes("UTF-8"), "ISO-8859-1");
    FTPFile[] files = client.listFiles(encoded);
    logger.log(Level.INFO, "Parse {0} item in directory: {1}", new Object[] { files.length, workingDirectory });
    for (FTPFile file : files) {
        if (file.isDirectory()) {
            parse(workingDirectory + "/" + file.getName());
        } else {/*from w ww  .  ja  v a 2s.co  m*/
            String remote = workingDirectory + "/" + file.getName();
            if (remote.endsWith(".md5") || remote.endsWith(".md5sum")) {
                parseChecksumFile(remote);
            } else if (remote.endsWith(".tgz.desc") || remote.endsWith(".zip.desc")) {
                parseDescriptorFile(remote);
            } else {
                parseFile(remote, file.getSize());
            }
        }
    }
}

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

@Override
public List<RemoteFileObject> readAllFilesInCurrentPath(String remotePath) {
    List<RemoteFileObject> willReturnObject = new ArrayList<>();
    try {// www.j a v a  2s.  c om
        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;
}