0

I am an amateur in android coding. I am trying to setup an android app with the ability to download a file from an ftp server. While running the code on the android 2.2 emulator, i am able to connect to the ftp server but the downloading part is showing an error. LogCat gives "download failed".

package com.ftconnect.down;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import org.apache.commons.net.ftp.*;

public class FTPConnectActivity extends Activity {
/** Called when the activity is first created. */

public FTPClient mFTPClient = null;
public boolean mConnect;
public boolean mDownload;
public boolean mDisconnected;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mConnect = ftpConnect("xxx.xxx.xxx.xxx", "admin",
            "123456", 21);

    mDownload = ftpDownload("xxx.xxx.xxx.xxx/ftp.mp3", "/sdcard");

    mDisconnected = ftpDisconnect();

}

public boolean ftpConnect(String host, String username, String password,
        int port) {
    try {
        mFTPClient = new FTPClient();
        // connecting to the host
        mFTPClient.connect(host, port);
        Log.d("ftpConnectApp", "Connecting to " + host);

        // 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);
            return status;
        }
    } catch (Exception e) {
        Log.d("ftpConnectApp", "Error: could not connect to host " + host);
    }

    return false;
}

public boolean ftpDownload(String srcFilePath, String desFilePath) {
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);
        ;
        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {
        Log.d("ftpConnectApp", "download failed");
    }

    return status;
}

public boolean ftpDisconnect() {
    try {
        mFTPClient.logout();
        mFTPClient.disconnect();
        return true;
    } catch (Exception e) {
        Log.d("ftpConnectApp",
                "Error occurred while disconnecting from ftp server.");
    }

    return false;
}

}

I have setup the internet and write external permission in the android manifest file. Should i include any other permissions?

Also, let me know if there is any changes to be made to the code above. Is the destination address as '/sdcard' correct?

Thanks in advance.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 1
    Please print the exception to your LogCat and post it here – powerMicha Jul 21 '11 at 06:31
  • http://stackoverflow.com/questions/6760331/download-image-from-url/6760473#6760473 – Nikunj Patel Jul 21 '11 at 06:42
  • @Jack: I've edited your code to remove the actual IP address - not sure if it's an actual address but you may not want to tell the world what it is (if you see what I mean). :D – Squonk Jul 21 '11 at 06:54

3 Answers3

0

You need to add Exception variable in your log message. You may also want to print full stack trace of the problem using: e.printStackTrace(); Generally /sdcard should work, however it is more reliable to request SD card location using Environment object. See more details about file storage on android in link

Dmitriy R
  • 613
  • 1
  • 5
  • 12
0

Also, let me know if there is any changes to be made to the code above. Is the destination address as '/sdcard' correct?

At the very least you should use /sdcard/filename.ext although this would only be OK for testing purposes if you are sure that /sdcard is a valid root directory.

To do things correctly, use getExternalFilesDir to find the correct path to the external storage 'files' directory that can be used for 'private' files for your own app. See the example code in that link for how to use it. You'll need to provide a filename for the output stream not just a path to a directory.

This may not be the answer to your problem but simply using...

FileOutputStream desFileStream = new FileOutputStream(desFilePath);

...when desFilePath is a directory, i.e., /sdcard, and not a file is guaranteed to fail.

Squonk
  • 48,735
  • 19
  • 103
  • 135
-1

Use mFTPClient.enterLocalActiveMode(); after login

Anup
  • 21
  • 1
  • 3