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.springframework.integration.ftp.filters.FtpSystemMarkerFilePresentFileListFilter.java

@Override
protected String getFilename(FTPFile file) {
    return file.getName();
}

From source file:org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizer.java

@Override
protected String getFilename(FTPFile file) {
    return (file != null ? file.getName() : null);
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();/* w w  w .j  ava 2s.c  o  m*/

    this.inboundLs.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(List.class));
    List<String> files = (List<String>) receive.getPayload();
    assertEquals(2, files.size());
    assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));

    FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
    for (FTPFile ftpFile : ftpFiles) {
        if (!ftpFile.isDirectory()) {
            assertTrue(files.contains(ftpFile.getName()));
        }
    }
}

From source file:org.syncany.connection.plugins.ftp.FtpTransferManager.java

@Override
public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) throws StorageException {
    connect();/*from   w w w  .  j a  v  a2  s  .c  om*/

    try {
        // List folder
        String remoteFilePath = getRemoteFilePath(remoteFileClass);
        FTPFile[] ftpFiles = ftp.listFiles(remoteFilePath + "/");

        // Create RemoteFile objects
        Map<String, T> remoteFiles = new HashMap<String, T>();

        for (FTPFile file : ftpFiles) {
            try {
                T remoteFile = RemoteFile.createRemoteFile(file.getName(), remoteFileClass);
                remoteFiles.put(file.getName(), remoteFile);
            } catch (Exception e) {
                logger.log(Level.INFO, "Cannot create instance of " + remoteFileClass.getSimpleName()
                        + " for file " + file + "; maybe invalid file name pattern. Ignoring file.");
            }
        }

        return remoteFiles;
    } catch (IOException ex) {
        forceFtpDisconnect();

        logger.log(Level.SEVERE, "Unable to list FTP directory.", ex);
        throw new StorageException(ex);
    }
}

From source file:org.syncany.plugins.ftp.FtpTransferManager.java

@Override
public boolean testRepoFileExists() {
    try {/*ww  w .j  a  va  2  s  .  c  om*/
        SyncanyRemoteFile repoFile = new SyncanyRemoteFile();
        String repoFilePath = getRemoteFile(repoFile);

        String repoFileParentPath = (repoFilePath.indexOf("/") != -1)
                ? repoFilePath.substring(0, repoFilePath.lastIndexOf("/"))
                : "";
        FTPFile[] listRepoFile = ftp.listFiles(repoFileParentPath);

        if (listRepoFile != null) {
            for (FTPFile ftpFile : listRepoFile) {
                if (ftpFile.getName().equals(repoFile.getName())) {
                    logger.log(Level.INFO,
                            "testRepoFileExists: Repo file exists, list(repo) contained 'syncany' file.");
                    return true;
                }
            }

            logger.log(Level.INFO,
                    "testRepoFileExists: Repo file DOES NOT exist: list(repo) DID NOT contain 'syncany' file:\n"
                            + StringUtil.join(listRepoFile, "\n"));
            return false;
        } else {
            logger.log(Level.INFO, "testRepoFileExists: Repo file DOES NOT exist: list(repo) was NULL.");
            return false;
        }
    } catch (Exception e) {
        logger.log(Level.INFO, "testRepoFileExists: Target does NOT exist. Chdir threw exception.", e);
        return false;
    }
}

From source file:org.tinygroup.vfs.impl.FtpFileFilterByName.java

public boolean accept(FTPFile ftpFile) {
    return fileName.equals(ftpFile.getName());
}

From source file:org.ut.biolab.medsavant.shared.util.SeekableFTPStream.java

@Override
public long length() {
    if (length == 0) {
        FTPFile[] files;//from w  w w  .  j a  va  2s.  c  o m
        try {
            files = listFiles(fileName);
        } catch (IOException e) {
            try {
                disconnect();
                files = listFiles(fileName);
            } catch (IOException e1) {
                LOG.warn("Unable to reconnect getting length");
                return 0;
            }

        }
        for (int i = 0; i < files.length; i++) {
            FTPFile file = files[i];
            if (file != null) {
                if (file.getName().equals(fileName)) {
                    length = file.getSize();
                    break;
                }
            }
        }
    }
    return length;

}

From source file:patcher.FXMLDocumentController.java

private ArrayList<String> getMissing(FTPClient ftp, File[] filesList) throws IOException {
    ArrayList<String> missing = new ArrayList<>();
    FTPFile[] files = ftp.listFiles(folder);
    for (FTPFile file : files) {
        boolean found = false;
        for (File test : filesList) {
            if (test.getName().equals(file.getName())) {
                found = true;// ww w .  j a v  a  2 s . c o m
            }
        }
        if (found == false) {
            if (file.isFile()) {
                missing.add(file.getName());
            }

        }
    }
    return missing;
}

From source file:Proiect.uploadFTP.java

public void actionFTP() {
    adressf.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {
            InetAddress thisIp;/*from w  ww. ja va  2s .  c  o m*/
            try {
                thisIp = InetAddress.getLocalHost();
                titleFTP.setText("Connection: " + thisIp.getHostAddress() + " -> " + adressf.getText());
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            saveState();
            uploadFTP.dispose();
            tree.dispose();
        }
    });

    connect.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            FTPClient client = new FTPClient();
            FileInputStream fis = null;
            String pass = String.valueOf(passf.getPassword());
            try {
                if (filename == null) {
                    status.setText("File does not exist!");
                } else {
                    // Server address
                    client.connect(adressf.getText());
                    // Login credentials
                    client.login(userf.getText(), pass);
                    if (client.isConnected()) {
                        status.setText("Succesfull transfer!");
                        // File type
                        client.setFileType(FTP.BINARY_FILE_TYPE);
                        // File location
                        File file = new File(filepath);
                        fis = new FileInputStream(file);
                        // Change the folder on the server
                        client.changeWorkingDirectory(folderf.getText());
                        // Save the file on the server
                        client.storeFile(filename, fis);
                    } else {
                        status.setText("Transfer failed!");
                    }
                }
                client.logout();
            } catch (IOException e1) {
                Encrypter.printException(e1);
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    client.disconnect();
                } catch (IOException e1) {
                    Encrypter.printException(e1);
                }
            }
        }
    });

    browsef.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int retval = chooserf.showOpenDialog(chooserf);
            if (retval == JFileChooser.APPROVE_OPTION) {
                status.setText("");
                filename = chooserf.getSelectedFile().getName().toString();
                filepath = chooserf.getSelectedFile().getPath();
                filenf.setText(chooserf.getSelectedFile().getName().toString());
            }
        }
    });

    adv.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            tree.setSize(220, uploadFTP.getHeight());
            tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
            tree.setResizable(false);
            tree.setIconImage(Toolkit.getDefaultToolkit()
                    .getImage(getClass().getClassLoader().getResource("assets/ico.png")));
            tree.setUndecorated(true);
            tree.getRootPane().setBorder(BorderFactory.createLineBorder(Encrypter.color_black, 2));
            tree.setVisible(true);
            tree.setLayout(new BorderLayout());

            JLabel labeltree = new JLabel("Server documents");
            labeltree.setOpaque(true);
            labeltree.setBackground(Encrypter.color_light);
            labeltree.setBorder(BorderFactory.createMatteBorder(8, 10, 10, 0, Encrypter.color_light));
            labeltree.setForeground(Encrypter.color_blue);
            labeltree.setFont(Encrypter.font16);

            JButton refresh = new JButton("");
            ImageIcon refresh_icon = getImageIcon("assets/icons/refresh.png");
            refresh.setIcon(refresh_icon);
            refresh.setBackground(Encrypter.color_light);
            refresh.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
            refresh.setForeground(Encrypter.color_black);
            refresh.setFont(Encrypter.font16);
            refresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

            final FTPClient client = new FTPClient();
            DefaultMutableTreeNode top = new DefaultMutableTreeNode(adressf.getText());
            DefaultMutableTreeNode files = null;
            DefaultMutableTreeNode leaf = null;

            final JTree tree_view = new JTree(top);
            tree_view.setForeground(Encrypter.color_black);
            tree_view.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
            tree_view.putClientProperty("JTree.lineStyle", "None");
            tree_view.setBackground(Encrypter.color_light);
            JScrollPane scrolltree = new JScrollPane(tree_view);
            scrolltree.setBackground(Encrypter.color_light);
            scrolltree.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));

            UIManager.put("Tree.textBackground", Encrypter.color_light);
            UIManager.put("Tree.selectionBackground", Encrypter.color_blue);
            UIManager.put("Tree.selectionBorderColor", Encrypter.color_blue);

            tree_view.updateUI();

            final String pass = String.valueOf(passf.getPassword());
            try {
                client.connect(adressf.getText());
                client.login(userf.getText(), pass);
                client.enterLocalPassiveMode();
                if (client.isConnected()) {
                    try {
                        FTPFile[] ftpFiles = client.listFiles();
                        for (FTPFile ftpFile : ftpFiles) {
                            files = new DefaultMutableTreeNode(ftpFile.getName());
                            top.add(files);
                            if (ftpFile.getType() == FTPFile.DIRECTORY_TYPE) {
                                FTPFile[] ftpFiles1 = client.listFiles(ftpFile.getName());
                                for (FTPFile ftpFile1 : ftpFiles1) {
                                    leaf = new DefaultMutableTreeNode(ftpFile1.getName());
                                    files.add(leaf);
                                }
                            }
                        }
                    } catch (IOException e1) {
                        Encrypter.printException(e1);
                    }
                    client.disconnect();
                } else {
                    status.setText("Failed connection!");
                }
            } catch (IOException e1) {
                Encrypter.printException(e1);
            } finally {
                try {
                    client.disconnect();
                } catch (IOException e1) {
                    Encrypter.printException(e1);
                }
            }

            tree.add(labeltree, BorderLayout.NORTH);
            tree.add(scrolltree, BorderLayout.CENTER);
            tree.add(refresh, BorderLayout.SOUTH);

            uploadFTP.addComponentListener(new ComponentListener() {

                public void componentMoved(ComponentEvent e) {
                    tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
                }

                public void componentShown(ComponentEvent e) {
                }

                public void componentResized(ComponentEvent e) {
                }

                public void componentHidden(ComponentEvent e) {
                }
            });

            uploadFTP.addWindowListener(new WindowListener() {
                public void windowActivated(WindowEvent e) {
                    tree.toFront();
                }

                public void windowOpened(WindowEvent e) {
                }

                public void windowIconified(WindowEvent e) {
                }

                public void windowDeiconified(WindowEvent e) {
                }

                public void windowDeactivated(WindowEvent e) {
                }

                public void windowClosing(WindowEvent e) {
                }

                public void windowClosed(WindowEvent e) {
                }
            });

            refresh.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    tree.dispose();
                    tree.setVisible(true);
                }
            });
        }
    });

}

From source file:proyectoftp.controlador.Controlador.java

private void descargarFichero() {

    int pos, correcto;
    boolean descargar;
    String fichero;//from  w  w  w .  j  a v a2s. c o m
    FTPFile fFTP;

    JFileChooser exploradorArchivos = new JFileChooser();
    pos = vistaPrincipal.devolverPulsadoLista();

    if (pos <= -1) {
        JOptionPane.showMessageDialog(vistaPrincipal, "Debes de pulsar un archivo para poder descargarlo.",
                "Error", JOptionPane.ERROR_MESSAGE);
    } else {
        fichero = vistaPrincipal.devolverObjeto(pos);
        fFTP = new FTPFile();
        fFTP.setName(fichero);
        if (fFTP.isDirectory()) {
            JOptionPane.showMessageDialog(vistaPrincipal, "Debes seleccionar un fichero para descargar.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            correcto = exploradorArchivos.showOpenDialog(vistaPrincipal);
            if (correcto == JFileChooser.APPROVE_OPTION) {
                File file = exploradorArchivos.getSelectedFile();

                try {

                    descargar = clienteFtp.descargarFichero(file.getCanonicalPath(), fFTP.getName());
                    comprobarDescargas(descargar);
                } catch (IOException ex) {
                    Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }

    }
}