Upload several txt files to FTP with Java

0

Greetings, and thanks in advance. The question is that I have been doing a small program in Java to upload several txt files to an FTP, I have managed to upload it with one specifying its path and name.Extension, but I want to do everything for example txt, in other languages by example when saying * .txt, you parsea everything and whatever they are this extension goes up. Here I leave part of the code:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;  
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import javax.swing.*;

class Subiendo_archivo {

public static void main(String[] args) {

    Configuracion configuracion = new Configuracion();
    configuracion.leerFichero();

    configuracion.setIpHost(configuracion.arregloDatos[0]);
    configuracion.getIpHost();
    configuracion.setUser(configuracion.arregloDatos[1]);
    configuracion.getUser();
    configuracion.setPassword(configuracion.arregloDatos[2]);
    configuracion.getPassword();
    String ipHOST = configuracion.ipHost;
    String usuario = configuracion.user;
    String pass = configuracion.password;

    FTPClient client = new FTPClient();

    String ftp = ipHOST; //
    File file1 = new File("D:\prueba.txt");
    try{
        client.connect(ftp,21);
        boolean login = client.login(usuario, pass);
        if (login){
            client.changeWorkingDirectory("/test");//Cambiar directorio de trabajo
            System.out.println("Iniciando sesión Satisfactoriamente");
            int replay = client.getReplyCode();
            if (FTPReply.isPositiveCompletion(replay)){
                File file = new File("D:\prueba.txt");
                FileInputStream input = new FileInputStream(file);
                client.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
                client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                client.enterLocalPassiveMode();
                System.out.println("Subió satisfactoriamente el archivo");


                if (!client.storeFile(file.getName(),input)){
                    System.out.println("Subida fallida!");
                }
                input.close();
            }
   // Cuando cierras sesión el método logout regresa "true".
            boolean logout = client.logout();
            if (logout){
                System.out.println("Salir del servidor FTP");
            }
        }else{
            System.out.println("Falló inciar sesión");
        }
    }
    catch (Exception e){
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "Error al subir fichero" +e);
    }
    finally{
        try{
            client.disconnect();
        }
        catch (IOException e){
            e.printStackTrace();
        }

    }
   file1.delete();
  }
} 
    
asked by Rafael Barrientos 26.05.2017 в 14:17
source

2 answers

1

The File class allows its medoto listFiles to obtain the files of a directory that contain certain similarities in the name.

With this you get all the txt files in a directory.

File dir = new File("/users/blah/dirname"); // El directorio
File[] files = dir.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.toLowerCase().endsWith(".txt");
    }
});

Then you just have to iterate over the files array with the code you have.

    
answered by 26.05.2017 / 14:24
source
0

Using the Lithorell code It works for me >

public class Main {

public static void main(String[] args) {
// write your code here
    File dir = new File("D:\habana\"); // El directorio
    File[] files = dir.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".txt");
        }
    });
    for (int i=0; i<files.length; i++){
        System.out.println(files[i]);
    }
 }
}

It leaves: D: \ habana \ new 1.txt D: \ havana \ test.txt D: \ habana \ Telefonos.txt D: \ habana \ thesis university of the villas.txt

    
answered by 26.05.2017 в 20:19