Error while listing FTP file

Asked

Viewed 178 times

0

Good evening guys, I’m trying to make a simple program that lists the files of an FTP directory, and error is occurring.

package br.com.java;

import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPConnect {

public static void main(String[] args) throws SocketException, IOException{
    // TODO code application logic here
    FTPClient ftp = new FTPClient();
    ftp.connect("ftp.endereco.com.br");
    ftp.login("usuario","senha");
    if(FTPReply.isPositiveCompletion(ftp.getReplyCode()) ){
        System.out.println("Conectado!!");
        ftp.enterLocalActiveMode();
    }

    ftp.changeWorkingDirectory("/pasta/subpasta");
    System.out.println(ftp.getReplyString());
    System.out.println("porta: " + ftp.getDefaultPort());

    try {
        String[] arq = ftp.listNames();
        System.out.println("Listando arquivos \n");
        for (String f : arq){
            System.out.println(f);
        }
        ftp.logout();
        ftp.disconnect();
    } catch (NullPointerException e) {
        System.out.println(e.getMessage());
    }catch (SocketException es){
        System.out.println(es.getMessage());
    }
}

 }

The output result is:

run:
Conectado!!
250 CWD command successful

porta: 21
Listando arquivos 

null
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

Does anyone have any idea what might be going on?

1 answer

0

The problem does not appear to be Java, but rather the user’s permission on the server, User may not have access to list, check online on https://pt.infobyip.com/ftptest.php |If you have access to the server and it is Linux, run sudo chmod 777 /home/user/pasta_ftp
another detail is the firewall configuration port 20 (data).

  • When I try to do by CMD the information that appears is this: ftp> ls 
500 PORT/EPRT (Active Mode/Extended Active Mode) is not supported. Use PASV/EPSV
 instead of this
425 Unable to build data connection: Invalid argument
ftp> pasv
Comando inválido.
ftp>

  • Guys, I managed to resolve the issue by changing ftp.enterLocalActiveMode(); for ftp.enterLocalPassiveMode();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.