List of usage examples for org.apache.commons.net.ftp FTPClient listFiles
public FTPFile[] listFiles() throws IOException
From source file:org.structr.ftp.FtpFilesTest.java
public void test04MoveFileToRoot() { FTPClient ftp = setupFTPClient(); final String name1 = "file1"; final String name2 = "dir1"; try (final Tx tx = app.tx()) { FTPFile[] files = ftp.listFiles(); assertNotNull(files);//from ww w . j a v a2s. c o m assertEquals(0, files.length); // Create files by API methods createFTPFile(null, name1); tx.success(); } catch (Exception ex) { logger.log(Level.WARNING, "", ex); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { // Create folder in / createFTPDirectory(null, name2); tx.success(); } catch (Exception ex) { logger.log(Level.WARNING, "", ex); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { // Move file to dir ftp.rename("/" + name1, "/" + name2 + "/" + name1); tx.success(); } catch (Exception ex) { logger.log(Level.WARNING, "", ex); fail("Unexpected exception: " + ex.getMessage()); } String[] fileNames = null; try (final Tx tx = app.tx()) { ftp.changeWorkingDirectory("/" + name2); fileNames = ftp.listNames(); assertNotNull(fileNames); assertEquals(1, fileNames.length); assertEquals(name1, fileNames[0]); // Move file back to / ftp.rename("/" + name2 + "/" + name1, "/" + name1); tx.success(); } catch (Exception ex) { logger.log(Level.WARNING, "", ex); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { ftp.changeWorkingDirectory("/"); tx.success(); } catch (Exception ex) { logger.log(Level.WARNING, "", ex); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { fileNames = ftp.listNames(); assertNotNull(fileNames); assertEquals(2, fileNames.length); assertEquals(name2, fileNames[0]); assertEquals(name1, fileNames[1]); ftp.disconnect(); tx.success(); } catch (Exception ex) { logger.log(Level.WARNING, "", ex); fail("Unexpected exception: " + ex.getMessage()); } }
From source file:Proiect.uploadFTP.java
public void actionFTP() { adressf.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { InetAddress thisIp;/*from w w w . j a v a 2 s .co 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:rems.Global.java
public static String[] GetFileList(InetAddress ftpServerAddrs, String serverAppDirectory, String dirName, String userName, String password) { String[] downloadFiles = new String[1]; StringBuilder result = new StringBuilder(); FTPClient ftpClient = new FTPClient(); try {//from w ww . j a va2 s. c o m // pass directory path on server to connect ftpClient.connect(ftpServerAddrs); // pass username and password, returned true if authentication is // successful boolean login = ftpClient.login(userName, password); if (login) { //System.out.println("Connection established..."); // get all files from server and store them in an array of // FTPFiles FTPFile[] files = ftpClient.listFiles(); for (FTPFile file : files) { if (file.getType() == FTPFile.FILE_TYPE) { result.append(file.getName()); result.append("\n"); } } result.replace(result.toString().lastIndexOf("\n"), result.toString().length(), ""); // logout the user, returned true if logout successfully boolean logout = ftpClient.logout(); if (logout) { System.out.println("Connection close..."); } } else { System.out.println("Connection fail..."); } return result.toString().split("\\\n"); } catch (SocketException e) { Global.errorLog += Arrays.toString(e.getStackTrace()); Global.writeToLog(); } catch (IOException e) { Global.errorLog += Arrays.toString(e.getStackTrace()); Global.writeToLog(); } finally { try { ftpClient.disconnect(); } catch (IOException e) { Global.errorLog += Arrays.toString(e.getStackTrace()); Global.writeToLog(); } } return downloadFiles; }
From source file:TrabajoFinalJava.CrearListaFicheros.java
@Override public void run() { String ftpSsrver = "127.0.0.1"; String ftpUser = "solera"; String ftpPass = "solera"; FTPClient cFtp = new FTPClient(); try {/*from w w w. j ava 2s. c o m*/ cFtp.connect(ftpSsrver); boolean login = cFtp.login(ftpUser, ftpPass); System.out.print("conexion ftp para ver ficheros establecida"); cFtp.enterLocalPassiveMode(); String[] archivos = cFtp.listNames(); FTPFile[] detalles = cFtp.listFiles(); archivos = cFtp.listNames(); for (int i = 0; i < archivos.length; i++) { arrayArchivos.add(archivos[i]); arrayArchivosDetalles.add(archivos[i]); } cFtp.logout(); cFtp.disconnect(); System.out.println("Conexion Finalizada, buenas tardes."); } catch (IOException ioe) { System.out.println("error" + ioe.toString()); } }
From source file:TrabajoFinalJava.FormularioVerFicheros.java
public void run() { //************************INICIO****INTERFAZ************************************************************************** JFrame principal = new JFrame("GESTOR DESCARGAS"); //Colores// w ww . ja v a 2 s.c o m Color nuevoColor = new Color(167, 220, 231); principal.getContentPane().setBackground(nuevoColor); JLabel tituloPrincipal = new JLabel("GESTOR DESCARGAS"); JLabel tituloVentana = new JLabel("VER FICHEROS"); JTextArea cajaFicheros = new JTextArea(12, 20); JButton mostrar = new JButton("MOSTRAR FICHEROS"); JButton atras = new JButton("ATRAS"); JButton salir = new JButton("SALIR"); cajaFicheros.setEditable(false); //Recojo la fuente que se esta utilizando actualmente. Font auxFont = tituloPrincipal.getFont(); //Aplico la fuente actual, y al final le doy el tamao del texto... tituloPrincipal.setFont(new Font(auxFont.getFontName(), auxFont.getStyle(), 30)); tituloVentana.setFont(new Font(auxFont.getFontName(), auxFont.getStyle(), 30)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); //Asignamos la constante EXIT_ON_CLOSE, cierra la ventana al pulsar la X. principal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Asignamos al JFrame el Layout que usaremos, GridBagLayout principal.setLayout(gridbag); //aadir botones al layout gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weighty = 0.1; gbc.fill = GridBagConstraints.HORIZONTAL; principal.add(tituloPrincipal, gbc); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weighty = 0.1; gbc.fill = GridBagConstraints.NONE; principal.add(tituloVentana, gbc); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weighty = 0.1; gbc.fill = GridBagConstraints.HORIZONTAL; principal.add(new JScrollPane(cajaFicheros), gbc); gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weighty = 0.1; gbc.fill = GridBagConstraints.NONE; principal.add(mostrar, gbc); gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weighty = 0.1; gbc.fill = GridBagConstraints.NONE; principal.add(atras, gbc); gbc.gridx = 1; gbc.gridy = 4; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weighty = 0.1; gbc.fill = GridBagConstraints.NONE; principal.add(salir, gbc); //cajaFicheros.setEditable(false); //Hace visible el panel principal.setVisible(true); principal.setSize(650, 350); principal.setLocationRelativeTo(null); principal.setResizable(false); //principal.pack(); mostrar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String ftpSsrver = "127.0.0.1"; String ftpUser = "solera"; String ftpPass = "solera"; FTPClient cFtp = new FTPClient(); try { cFtp.connect(ftpSsrver); boolean login = cFtp.login(ftpUser, ftpPass); System.out.print("conexion ftp para ver ficheros establecida"); cFtp.enterLocalPassiveMode(); String[] archivos = cFtp.listNames(); FTPFile[] detalles = cFtp.listFiles(); archivos = cFtp.listNames(); for (int i = 0; i < archivos.length; i++) { /* cajaFicheros.selectAll(); cajaFicheros.replaceSelection(""); */ arrayArchivos.add(archivos[i].toString()); System.out.println(arrayArchivos.get(i)); cajaFicheros.append(System.getProperty("line.separator")); cajaFicheros.append(arrayArchivos.get(i)); } cFtp.logout(); cFtp.disconnect(); System.out.println("Conexion Finalizada, buenas tardes."); } catch (IOException ioe) { System.out.println("error" + ioe.toString()); } } }); try { salir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } catch (Exception e) { } try { atras.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FormularioAccesoFtp accesoFtp = new FormularioAccesoFtp(); accesoFtp.inicioFtp(); principal.setVisible(false); } }); } catch (Exception e) { } }