Angular, AWS und Symfony Freelancer

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;
    }
}

Veröffentlicht

in

von

Schlagwörter:

Kommentare

3 Kommentare zu „FTP Upload mit Android“

  1. Avatar von christian
    christian

    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

    1. Avatar von Sebastian Viereck
      Sebastian Viereck

      Das liegt an den schreibrechten des ftp ordners, in den gespeichert werden soll…

  2. Avatar von Maxi
    Maxi

    Bei mir wird „ItemUploader“ als Fehler markiert 🙁 Was mach ich falsch ??

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert