• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps FTP download problems

Valten1992

Newbie
Huzzah! My first problem post!(not sure if that is a good thing or a bad thing.....:confused:)

A problem that has been frustrating me for the last few days is downloading from an FTP for my Android app. Up till now, the app has been reading from local files to get its data but the final version shall read the files on a FTP server. The problem is that while the experiment app I made can connect to the server, downloading seems to not work. No file is downloaded at all, knowing me Im probably missing something obvious.

My onCreate
Code:
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        
         ftpConnect("domain","username", "password", portnum);
         
        
         try
         {
             String s = Environment.getExternalStorageDirectory().toString();
             File f = new File(s+"/???/item");
             f.mkdirs();
             ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
         }
         catch(Exception e)
         {
             Toast toast = Toast.makeText(getApplicationContext(), "Download error: "+e.getMessage(), Toast.LENGTH_SHORT);
             toast.show();
         }
        
         ftpDisconnect();
        
        
    }
Names hidden to protect identity ;)


And the download method

Code:
public boolean ftpDownload(String srcFilePath, File desFilePath)
    {
        boolean status = false;
        try {
            
            FileOutputStream desFileStream = new FileOutputStream(desFilePath);
            
            status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
            desFileStream.close();
            Toast toast = Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT);
            toast.show();

            return status;
        } catch (Exception e) {
            Toast toast = Toast.makeText(getApplicationContext(), "Download error"+e.getMessage(), Toast.LENGTH_SHORT);
            toast.show();
        }
        

        return status;
    }
Unrelated, but how exactly do you make your own custom logs to display errors? I've been using toasts up until now. I try Log.e("Error", e.getMessage()) but it does not display.
 
This is probably more suited to our development forums. This area is used to discuss applications in general from a user standpoint. I can move the thread if you'd like.
 
In FTP, the initial connection to port 21 is only used for sending commands and receiving status replies in reaction to those commands. Data is transferred on a separate FTP data connection

In the default FTP active mode, it's the FTP server establishes the FTP data connection back with the client. If the FTP client is behind a NAT device or a firewall, the server is not going to be able to connect. The symptom of this problem is as you describe, the FTP exchange works until it comes time to transfer data.

In FTP passive mode, the FTP client establishes the FTP data connection. To enter passive mode, you need to send a PASV command to the server just before you send the RETR command. The reply to the PASV command will include the port number on the server to which the client needs to open the FTP data connection. See RFC 959 for details.

Does mFTPClient enter FTP passive mode in retrieveFile? If not, you'll need enter passive mode before before calling retrieveFile or setup mFTPClient to enter passive mode in retrieveFile.
 
I was working on it a bit more yesterday and noticed two things:

1.On the path I specify in the download method to download the file from the server to, an empty text file is created with the name of the file I want to to download. If I do not give a file name in the destination path I get a (Is a Directory error, which is caused by not specifying an actual file from what I understand)

2.My connection method causes the client to enter passive method after conecting to the server so it might not be that.

Here's my connection method:

Code:
public boolean ftpConnect(String host, String username,
            String password, int port)
    {
        try{
            mFTPClient = new FTPClient();
            // connecting to the host
            
                mFTPClient.connect(host, port);
            

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);

                /* Set File Transfer Mode
                 *
                 * To avoid corruption issue you must specified a correct
                 * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
                 * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
                 * for transferring text, image, and compressed files.
                 */
        
                mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
                mFTPClient.enterLocalPassiveMode();

                return status;
            }
        } catch(Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        return false;
    }
 
With Apache Commons, you don't pass a full FTP URL as the remote parameter to retrieveFile. You only pass the remote path of the file.

Try changing:
Code:
   ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
to:
Code:
   ftpDownload("rooms.txt", f);

If rooms.txt isn't in the current working directory of the FTP session, you need to call changeWorkingDirectory on FTPClient to change directory before calling retrieveFile.


Also many of the Apache Commons Net library methods won't raise an exception if there's a protocol error, only if there's a failure on the communications link.

You need to check if the methods you're using are completing successfully and are returning true. If it returns false you need get the reply code and look up the meaning of the reply code in section 4.2 of RFC 959.
 
With Apache Commons, you don't pass a full FTP URL as the remote parameter to retrieveFile. You only pass the remote path of the file.

Try changing:
Code:
   ftpDownload("ftp://user@portalftp.?????.com/rooms.txt", f);
to:
Code:
   ftpDownload("rooms.txt", f);
If rooms.txt isn't in the current working directory of the FTP session, you need to call changeWorkingDirectory on FTPClient to change directory before calling retrieveFile.


Also many of the Apache Commons Net library methods won't raise an exception if there's a protocol error, only if there's a failure on the communications link.

You need to check if the methods you're using are completing successfully and are returning true. If it returns false you need get the reply code and look up the meaning of the reply code in section 4.2 of RFC 959.


Thats was it.......good god, that was it. So obvious I didnt consider to try it. Thank you very much :)
 
Back
Top Bottom