FTP Upload mit Android


Einbinden der benötigten Bibliothek von Apache mit Eclipse: Anleitung.

Code:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

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

import android.util.Log;

import de.egraveyard.android.view.ItemUploader;

public class FtpUpload {
    private static final String TAG = ItemUploader.class.getSimpleName();

    String serverAdress = "ftp.lala.de";
    String userName = "ftpUser";
    String password = "lala";
    String serverDirectory = "/android";
    //String localFilePath = "/data/anr/traces.txt";
    //String remoteFileName = "traces2.txt";

    public boolean uploadImage(String localFilePath, String remoteFileName) {
        boolean result = false;

        FTPClient ftpClient = new FTPClient();

        try {

            ftpClient.connect(this.serverAdress);
            ftpClient.login(this.userName, this.password);
            ftpClient.changeWorkingDirectory(this.serverDirectory);
            ftpClient
                    .setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
        } catch (IOException e) {
            Log.d(TAG,
                    "IOException ftp Client could not be established.controll Login, Server, Pw.");
        }
        Log.d(TAG, "FTP Server Response: " + ftpClient.getReplyString());

        BufferedInputStream buffIn = null;
        try {
            buffIn = new BufferedInputStream(new FileInputStream(localFilePath));
        } catch (FileNotFoundException e) {
            Log.d(TAG,
                    "FileNotFoundException: local File to be uploaded not Found: " + localFilePath);
        }
        ftpClient.enterLocalPassiveMode();

        try {
            result = ftpClient.storeFile(remoteFileName, buffIn);
        } catch (IOException e) {
            Log.d(TAG, "IOException: remote File could not be accessed");
        }
        try {
            buffIn.close();
        } catch (IOException e) {
            Log.d(TAG, "IOException: buffIn.close()");
        }

        try {
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (IOException e) {
            Log.d(TAG, "IOException: ftpClient close/logout");
        }

        return result;
    }
}

Hinterlasse eine Antwort

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  1. Code super! Habe diesen bei mir getestet, funktioniert aber leider nicht.

    result = ftpClient.storeFile(remoteFileName, buffIn); hier kommt der Fehler !

    hier springt Eclipse immer in “catch” und dort bekomm ich dann

    Log.d(TAG, “IOException: remote File could not be accessed”); –> null

    Was mache ich falsch??? Hilfe