-1

this is my code:

String serverAddress = "ftp://ftp.nasdaqtrader.com/symboldirectory/"; // ftp server address 
    int port = 21; // ftp uses default port Number 21

    FTPClient ftpClient = new FTPClient();
    try {

        ftpClient.connect(serverAddress, port);

        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE/FTP.ASCII_FILE_TYPE);
        String remoteFilePath = "/nasdaqtraded.txt";
        File localfile = new File(System.getProperty("user.dir")+"\\src\\test\\resources\\stocks.txt");
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localfile));
        boolean success = ftpClient.retrieveFile(remoteFilePath, outputStream);
        outputStream.close();

        if (success) {
            System.out.println("Ftp file successfully download.");
        }

    } catch (IOException ex) {
        System.out.println("Error occurs in downloading files from ftp Server : " + ex.getMessage());
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

And i am running it from localhost so i can download a list of stocks from nasdaq site, problem is it gives me this error:

Error occurs in downloading files from ftp Server : ftp://ftp.nasdaqtrader.com/symboldirectory/: invalid IPv6 address

I understand that is because i am trying to download the file from localhost, is there any way around it?

I am just trying to download this file: ftp://ftp.nasdaqtrader.com/symboldirectory/nasdaqtraded.txt

to my computer, that's it.

totothegreat
  • 1,633
  • 4
  • 27
  • 59
  • How are you resolving the server name to localhost (`::1` for IPv6)? Does your DNS (`AAAA` record) or hosts file have an IPv6 entry to resolve that correctly? – Ron Maupin Jul 22 '18 at 18:25
  • Can you speak english too? – totothegreat Jul 22 '18 at 18:44
  • I believe that my comment was perfect English (my native tongue is the Texas dialect of English). You must somehow resolve the server name into an IPv6 address. The IPv6 localhost address is `::1`. IP (both IPv4 and IPv6) cannot use names, only addresses. You either need a DNS server that can translate a name into an address, or have something like a hosts file on your host to translate the name for you. You could also simply use the localhost address (again, it is `::1` for IPv6, or `127.0.0.1` for IPv4). – Ron Maupin Jul 22 '18 at 18:52

2 Answers2

2

The FTPClient class's connect() method expects to be passed the hostname of the server to connect to.

As with all classes derived from SocketClient, you must first connect to the server with connect before doing anything, and finally disconnect after you're completely finished interacting with the server.

However, your code is passing in a URI, which is being misinterpreted as an IPv6 address (probaby because it contains a colon).

You should instead connect() to the hostname of the server.

String hostname = "ftp.nasdaqtrader.com";
int port = 21;

FTPClient ftpClient = new FTPClient();

try {
   ftpClient.connect(hostname, port);
Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
-1

I had same issue and found this helpful

Read a file from NASDAQ FTP Server

Maven Dependencies:

    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>3.6</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>

Java Code:

    FTPClient ftpClient = new FTPClient();
    ftpClient.setStrictReplyParsing(false);

    int portNumber = 21;
    String pass = "anonymous";

    try {
        // connect to NASDAQ FTP
        ftpClient.connect("ftp.nasdaqtrader.com", portNumber);
        ftpClient.login(pass, pass);

        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.ASCII_FILE_TYPE);

        if (ftpClient.isConnected()) {
            log.debug("connection successful");

            ftpClient.changeWorkingDirectory("/SymbolDirectory");

            String remoteFile = "nasdaqlisted.txt";

            InputStream in = new BufferedInputStream(ftpClient.retrieveFileStream(remoteFile));

            String text = IOUtils.toString(in, StandardCharsets.UTF_8.name());

            if(text != null) {
                log.debug("write successful; \n {}", text);
            }
        }
        ftpClient.logout();
    } catch (IOException e) {
        log.error("connection failed", e);
    } finally {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            log.error("failed to disconnect", e);
        }
    }
silver_fox
  • 1,057
  • 1
  • 5
  • 4