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:Main.java

public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    String[] names = client.listNames();
    for (String name : names) {
        System.out.println("Name = " + name);
    }//  w  w  w.  ja va 2s  .  c  o  m

    FTPFile[] ftpFiles = client.listFiles();
    for (FTPFile ftpFile : ftpFiles) {
        // Check if FTPFile is a regular file
        if (ftpFile.getType() == FTPFile.FILE_TYPE) {
            System.out.println("FTPFile: " + ftpFile.getName() + "; "
                    + FileUtils.byteCountToDisplaySize(ftpFile.getSize()));
        }
    }
    client.logout();
    client.disconnect();
}

From source file:com.peter.javaautoupdater.JavaAutoUpdater.java

public static void run(String ftpHost, int ftpPort, String ftpUsername, String ftpPassword,
        boolean isLocalPassiveMode, boolean isRemotePassiveMode, String basePath, String softwareName,
        String args[]) {/*from  w w w.ja  v  a  2  s.  co m*/
    System.out.println("jarName=" + jarName);
    for (String arg : args) {
        if (arg.toLowerCase().trim().equals("-noautoupdate")) {
            return;
        }
    }
    JavaAutoUpdater.basePath = basePath;
    JavaAutoUpdater.softwareName = softwareName;
    JavaAutoUpdater.args = args;

    if (!jarName.endsWith(".jar") || jarName.startsWith("JavaAutoUpdater-")) {
        if (isDebug) {
            jarName = "test.jar";
        } else {
            return;
        }
    }
    JProgressBarDialog d = new JProgressBarDialog(new JFrame(), "Auto updater", true);

    d.progressBar.setIndeterminate(true);
    d.progressBar.setStringPainted(true);
    d.progressBar.setString("Updating");
    //      d.addCancelEventListener(this);
    Thread longRunningThread = new Thread() {
        public void run() {
            d.progressBar.setString("checking latest version");
            System.out.println("checking latest version");

            FTPClient ftp = new FTPClient();
            try {
                ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
                ftp.connect(ftpHost, ftpPort);
                int reply = ftp.getReplyCode();
                System.out.println("reply=" + reply + ", " + FTPReply.isPositiveCompletion(reply));
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    JOptionPane.showMessageDialog(null, "FTP server refused connection", "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                d.progressBar.setString("connected to ftp");
                System.out.println("connected to ftp");
                boolean success = ftp.login(ftpUsername, ftpPassword);
                if (!success) {
                    ftp.disconnect();
                    JOptionPane.showMessageDialog(null, "FTP login fail, can't update software", "error",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (isLocalPassiveMode) {
                    ftp.enterLocalPassiveMode();
                }
                if (isRemotePassiveMode) {
                    ftp.enterRemotePassiveMode();
                }
                FTPFile[] ftpFiles = ftp.listFiles(basePath, new FTPFileFilter() {
                    @Override
                    public boolean accept(FTPFile file) {
                        if (file.getName().startsWith(softwareName)) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
                if (ftpFiles.length > 0) {
                    FTPFile targetFile = ftpFiles[ftpFiles.length - 1];
                    System.out.println("targetFile : " + targetFile.getName() + " , " + targetFile.getSize()
                            + "!=" + new File(jarName).length());
                    if (!targetFile.getName().equals(jarName)
                            || targetFile.getSize() != new File(jarName).length()) {
                        int r = JOptionPane.showConfirmDialog(null,
                                "Confirm to update to " + targetFile.getName(), "Question",
                                JOptionPane.YES_NO_OPTION);
                        if (r == JOptionPane.YES_OPTION) {
                            //ftp.enterRemotePassiveMode();
                            d.progressBar.setString("downloading " + targetFile.getName());
                            ftp.setFileType(FTP.BINARY_FILE_TYPE);
                            ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);

                            d.progressBar.setIndeterminate(false);
                            d.progressBar.setMaximum(100);
                            CopyStreamAdapter streamListener = new CopyStreamAdapter() {

                                @Override
                                public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
                                        long streamSize) {
                                    int percent = (int) (totalBytesTransferred * 100 / targetFile.getSize());
                                    d.progressBar.setValue(percent);
                                }
                            };
                            ftp.setCopyStreamListener(streamListener);
                            try (FileOutputStream fos = new FileOutputStream(targetFile.getName())) {
                                ftp.retrieveFile(basePath + "/" + targetFile.getName(), fos);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            d.progressBar.setString("restarting " + targetFile.getName());
                            restartApplication(targetFile.getName());
                        }
                    }

                }
                ftp.logout();
                ftp.disconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
    d.thread = longRunningThread;
    d.setVisible(true);
}

From source file:info.track_mate.util.DefaultFTPClient.java

/** {@inheritDoc} */
public Collection<FTPFile> listFiles() throws Exception {
    org.apache.commons.net.ftp.FTPFile[] ftpFiles = ftpClient.listFiles();
    Collection<FTPFile> toReturn = new ArrayList<FTPFile>();
    for (org.apache.commons.net.ftp.FTPFile ftpFile : ftpFiles) {
        toReturn.add(new DefaultFTPFile(ftpFile.getName(), ftpFile.getSize()));
    }/*  w w w.j  a  va  2s  .co  m*/
    return toReturn;
}

From source file:ca.ualberta.physics.cssdp.file.remote.protocol.FtpConnection.java

@Override
public List<RemoteFile> ls(String path) {

    logger.debug("Listing files at " + path);

    try {/*from  w w  w.  ja va 2  s  .  c o m*/
        ftpClient.enterLocalPassiveMode();
        FTPFile[] files = ftpClient.listFiles(path);

        if (files == null || files.length == 0) {
            return new ArrayList<RemoteFile>();
        }

        List<RemoteFile> list = new ArrayList<RemoteFile>();
        for (FTPFile file : files) {

            String name = file.getName();
            long size = file.getSize();
            boolean isDir = file.isDirectory();
            LocalDateTime modifiedTstamp = new LocalDateTime(file.getTimestamp());

            RemoteFile remoteFile = new RemoteFile("ftp://" + getHostEntry().getHostname() + path + "/" + name,
                    size, modifiedTstamp, isDir);
            list.add(remoteFile);
        }

        return list;

    } catch (SocketTimeoutException timeout) {
        throw new ProtocolException("Timedout listing " + path, true, timeout);

    } catch (IOException e) {
        throw new ProtocolException("Could not get file listing for " + path, false, e);
    }

}

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

@Test
public void testParse() throws Exception {
    FTPFile parsed;

    // #3701/*ww  w . j  a  v a  2  s  . co m*/
    parsed = parser.parseFTPEntry("12-04-06  12:43PM                65335 fon1.kucuk.jpg");
    assertNotNull(parsed);
    assertEquals("fon1.kucuk.jpg", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals(65335, parsed.getSize());
    assertEquals(2006, parsed.getTimestamp().get(Calendar.YEAR));
    assertEquals(Calendar.DECEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(4, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
}

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

@Test
public void testParse() throws Exception {
    FTPFile parsed;

    parsed = parser.parseFTPEntry("-rwx------          17      332      640 Dec 20 08:54 file 1");
    assertNotNull(parsed);//from  w w  w  .ja  v  a 2s .c  om
    assertEquals("file 1", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals(640, parsed.getSize());

    parsed = parser.parseFTPEntry("drwx------             folder          2 Dec 20 08:55 folder1");
    assertNotNull(parsed);
    assertEquals("folder1", parsed.getName());
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals(Calendar.DECEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(20, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertFalse(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertFalse(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));
    assertTrue(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));
}

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

@Test
public void testParse() throws Exception {
    FTPFile parsed;

    // #3689//from w w w  .  j a va  2 s .  c  o  m
    parsed = parser.parseFTPEntry("drwxr-xr-x+  5 niels    staff          7 Sep  6 13:46 data");
    assertNotNull(parsed);
    assertEquals(parsed.getName(), "data");
    assertEquals(FTPFile.DIRECTORY_TYPE, parsed.getType());
    assertEquals(7, parsed.getSize());
    assertEquals(Calendar.SEPTEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(6, parsed.getTimestamp().get(Calendar.DAY_OF_MONTH));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.USER_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.GROUP_ACCESS, FTPFile.EXECUTE_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.READ_PERMISSION));
    assertTrue(parsed.hasPermission(FTPFile.WORLD_ACCESS, FTPFile.EXECUTE_PERMISSION));
}

From source file:com.stacksync.desktop.connection.plugins.ftp.FtpTransferManager.java

@Override
public Map<String, RemoteFile> list() throws StorageException {
    connect();/*from ww  w. j av  a 2s  . c  o m*/

    try {
        Map<String, RemoteFile> files = new HashMap<String, RemoteFile>();
        FTPFile[] ftpFiles = ftp.listFiles(getConnection().getPath());

        for (FTPFile f : ftpFiles) {
            files.put(f.getName(), new RemoteFile(f.getName(), f.getSize(), f));
            if (f.isDirectory()) {
                files.putAll(getDirectoryList(f.getName()));
            }
        }

        return files;
    } catch (IOException ex) {
        logger.error("Unable to list FTP directory.", ex);
        throw new StorageException(ex);
    }
}

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

@Test
public void testParse() throws Exception {
    FTPFile parsed;

    // #5437/*from   w  ww.  j  a  v a 2  s  .  c  om*/
    parsed = parser.parseFTPEntry("-rw-r--r--    1 3642     3643          106 Nov 15 22:20 index.html");
    assertNotNull(parsed);
    assertEquals("index.html", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals(106, parsed.getSize());
    assertEquals(Calendar.NOVEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(15, 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));
}

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

@Test
public void testParse() throws Exception {
    FTPFile parsed;

    // #3119//from  w  w w. j  a  va 2 s  .  c  om
    parsed = parser.parseFTPEntry("-rw-r--r-- 1 ftp ftp         100847 Sep 10  2004 octfront2.jpg");
    assertNotNull(parsed);
    assertEquals("octfront2.jpg", parsed.getName());
    assertEquals(FTPFile.FILE_TYPE, parsed.getType());
    assertEquals(100847, parsed.getSize());
    assertEquals(Calendar.SEPTEMBER, parsed.getTimestamp().get(Calendar.MONTH));
    assertEquals(10, 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));
}