0

This is re-worded from a previous question (which was probably a bit unclear).

I want to download a text file via FTP from a remote server, read the contents of the text file into a string and then discard the file. I don't need to actually save the file.

I am using the Apache Commons library so I have:

import org.apache.commons.net.ftp.FTPClient;

Can anyone help please, without simply redirecting me to a page with lots of possible answers on?

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
Kevmeister
  • 147
  • 1
  • 1
  • 12
  • 2
    C'mon, man. It's right in the javadocs: http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html – duffymo Jul 25 '12 at 16:47

3 Answers3

3

Not going to do the work for you, but once you have your connection established, you can call retrieveFile and pass it an OutputStream. You can google around and find the rest...

 FTPClient ftp = new FTPClient();
 ...
 ByteArrayOutputStream myVar = new ByteArrayOutputStream();
 ftp.retrieveFile("remoteFileName.txt", myVar);

ByteArrayOutputStream

retrieveFile

Mike
  • 3,186
  • 3
  • 26
  • 32
2

Normally I'd leave a comment asking 'What have you tried?'. But now I'm feeling more generous :-)

Here you go:

private void ftpDownload() {
    FTPClient ftp = null;
    try {
        ftp = new FTPClient();
        ftp.connect(mServer);

        try {
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                throw new Exception("Connect failed: " + ftp.getReplyString());
            }
            if (!ftp.login(mUser, mPassword)) {
                throw new Exception("Login failed: " + ftp.getReplyString());
            }
            try {
                ftp.enterLocalPassiveMode();
                if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
                    Log.e(TAG, "Setting binary file type failed.");
                }
                transferFile(ftp);
            } catch(Exception e) {
                handleThrowable(e);
            } finally {
                if (!ftp.logout()) {
                    Log.e(TAG, "Logout failed.");
                }
            }
        } catch(Exception e) {
            handleThrowable(e);
        } finally {
            ftp.disconnect();
        }
    } catch(Exception e) {
        handleThrowable(e);
    }
}

private void transferFile(FTPClient ftp) throws Exception {
    long fileSize = getFileSize(ftp, mFilePath);
    InputStream is = retrieveFileStream(ftp, mFilePath);
    downloadFile(is, buffer, fileSize);
    is.close();

    if (!ftp.completePendingCommand()) {
        throw new Exception("Pending command failed: " + ftp.getReplyString());
    }
}

private InputStream retrieveFileStream(FTPClient ftp, String filePath)
throws Exception {
    InputStream is = ftp.retrieveFileStream(filePath);
    int reply = ftp.getReplyCode();
    if (is == null
            || (!FTPReply.isPositivePreliminary(reply)
                    && !FTPReply.isPositiveCompletion(reply))) {
        throw new Exception(ftp.getReplyString());
    }
    return is;
}

private byte[] downloadFile(InputStream is, long fileSize)
throws Exception {
    byte[] buffer = new byte[fileSize];
    if (is.read(buffer, 0, buffer.length)) == -1) {
        return null;
    }
    return buffer; // <-- Here is your file's contents !!!
}

private long getFileSize(FTPClient ftp, String filePath) throws Exception {
    long fileSize = 0;
    FTPFile[] files = ftp.listFiles(filePath);
    if (files.length == 1 && files[0].isFile()) {
        fileSize = files[0].getSize();
    }
    Log.i(TAG, "File size = " + fileSize);
    return fileSize;
}
Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
  • duffymo - You'll find my response to your advice somewhere on the Internet. C'mon man, just Google it! – Kevmeister Jul 25 '12 at 17:36
  • Dheera - an extremely verbose piece of code which resulted in so many errors that Eclipse crashed on me. – Kevmeister Jul 25 '12 at 17:38
  • Can anyone give me a more helpful solution please? – Kevmeister Jul 25 '12 at 17:39
  • 1
    @Kevmeister Do you seriously expect people to provide you a working solution for your project on a platter? Then StackOverflow is not the place for it. This code was taken from my project after stripping away irrelevant parts of the code. So of course it won't compile. Try to understand it and adapt the code for your need. – Dheeraj Vepakomma Jul 25 '12 at 17:44
  • 1
    No I don't expect that, but neither do I expect a big lump of uncommented code to be thrown at me. I need help, not noob bashing from the big bad forum moderator. – Kevmeister Jul 25 '12 at 18:03
0

You can just skip the download to local filesystem part and do:

    FTPClient ftpClient = new FTPClient();
    try {
       ftpClient.connect(server, port);
       ftpClient.login(user, pass);
       ftpClient.enterLocalPassiveMode();
       ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

       InputStream inputStream = ftpClient.retrieveFileStream("/folder/file.dat");
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "Cp1252"));

       while(reader.ready()) {
           System.out.println(reader.readLine()); // Or whatever
       }
       inputStream.close();

    } catch (IOException ex) {
         ex.printStackTrace();
    } finally {
         try {
             if (ftpClient.isConnected()) {
                 ftpClient.logout();
                 ftpClient.disconnect();
             }
         } catch (IOException ex) {
               ex.printStackTrace();
         }
    }