1

I want to upload a file via ftp. I found some information's about it and built my class like I found it here: http://www.sebastianviereck.de/ftp-upload-mit-android/

I also added the internet permission in the manifest and set the strictmode like I found it here: Error StrictMode$AndroidBlockGuardPolicy.onNetwork

The result of storeFile ist treu and the reply code of the ftpClient is 226 ( "Closing data connection. Requested file action successful (for example, file transfer or file abort)" ).

So it looks fine for me, but the files are not uploaded.

Does someone have an Idea?

Here is my code:

Main:

FtpDataHandler ftp = new FtpDataHandler("serverName", "userName", "password", "/folder");
                    ftp.uploadFile("/storage/emulated/0/pictures/test_sig.jpg", "test_sig.jpg");

FTP handler:

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;

public class FtpDataHandler {
    private static final String TAG = "FtpDataHandler";

    String serverAdress;
    String userName;
    String password;
    String serverDirectory; 
    FTPClient ftpClient;

    public FtpDataHandler(String serverAdress, String userName, String password, String serverDiretory) {
        this.serverAdress = serverAdress;
        this.userName = userName;
        this.password = password;
        //this was my problem. I set the serverAdress to the serverDirectory insted the serverDirectory
        //this.serverDirectory = serverAdress;
        this.serverDirectory = serverDirectory;

        connect();
    }  

    public String getServerAdress() {
        return serverAdress;
    }

    public String getUserName() {
        return userName;
    }

    public String getServerDirectory() {
        return serverDirectory;
    }

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

        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);
            Log.d(TAG, "Reply code: " + ftpClient.getReplyCode());
        } 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()");
        }

        return result;
    }

    public boolean connect(){
        boolean bool = false;
        ftpClient = new FTPClient();

        try {

            ftpClient.connect(this.serverAdress);
            bool = ftpClient.login(this.userName, this.password);
            if (!bool){
                Log.d(TAG, "Login Reply Code" + ftpClient.getReplyCode());
            }
            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());

        return bool;
    }

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

Here is also the ftpClient.getReplyString() after the ftpClient.login, this.ftpClient.changeWorkingDirectory(this.serverDirectory), this.ftpClient.setFileType(2);

FTP Server Response: 200 Type set to I

And the reply code after this.ftpClient.storeFile(remoteFileName, buffIn);

Reply code: 226

I can upload a file via a Ftp Client like FileZilla or Turbo FTP Client. Here you can see the log of FileZilla used on my laptop.

2017-10-05 19:02:28 8160 1 Status: Resolving address of server.com
2017-10-05 19:02:28 8160 1 Status: Connecting to serverIp...
2017-10-05 19:02:28 8160 1 Status: Connection established, waiting for welcome message...
2017-10-05 19:02:28 8160 1 Response: 220 FTP on server.com ready
2017-10-05 19:02:28 8160 1 Command: AUTH TLS
2017-10-05 19:02:28 8160 1 Response: 234 AUTH TLS successful
2017-10-05 19:02:28 8160 1 Status: Initializing TLS...
2017-10-05 19:02:28 8160 1 Status: Verifying certificate...
2017-10-05 19:02:28 8160 1 Status: TLS connection established.
2017-10-05 19:02:28 8160 1 Command: USER user
2017-10-05 19:02:28 8160 1 Response: 331 Password required for user
2017-10-05 19:02:28 8160 1 Command: PASS ************
2017-10-05 19:02:28 8160 1 Response: 230 User user logged in
2017-10-05 19:02:28 8160 1 Command: OPTS UTF8 ON
2017-10-05 19:02:28 8160 1 Response: 200 UTF8 set to on
2017-10-05 19:02:28 8160 1 Command: PBSZ 0
2017-10-05 19:02:28 8160 1 Response: 200 PBSZ 0 successful
2017-10-05 19:02:28 8160 1 Command: PROT P
2017-10-05 19:02:28 8160 1 Response: 200 Protection set to Private
2017-10-05 19:02:28 8160 1 Status: Logged in
2017-10-05 19:02:28 8160 1 Status: Retrieving directory listing...
2017-10-05 19:02:28 8160 1 Command: PWD
2017-10-05 19:02:28 8160 1 Response: 257 "/" is the current directory
2017-10-05 19:02:28 8160 1 Command: TYPE I
2017-10-05 19:02:28 8160 1 Response: 200 Type set to I
2017-10-05 19:02:28 8160 1 Command: PASV
2017-10-05 19:02:28 8160 1 Response: 227 Entering Passive Mode (xx,xx,xx,xx,xx,xx).
2017-10-05 19:02:28 8160 1 Command: MLSD
2017-10-05 19:02:28 8160 1 Response: 150 Opening BINARY mode data connection for MLSD
2017-10-05 19:02:29 8160 1 Response: 226 Transfer complete
2017-10-05 19:02:29 8160 1 Status: Directory listing of "/" successful
2017-10-05 19:02:36 8160 3 Status: Resolving address of server.com
2017-10-05 19:02:36 8160 3 Status: Connecting to serverIp...
2017-10-05 19:02:36 8160 3 Status: Connection established, waiting for welcome message...
2017-10-05 19:02:36 8160 3 Response: 220 FTP on server.com ready
2017-10-05 19:02:36 8160 3 Command: AUTH TLS
2017-10-05 19:02:36 8160 3 Response: 234 AUTH TLS successful
2017-10-05 19:02:36 8160 3 Status: Initializing TLS...
2017-10-05 19:02:36 8160 3 Status: Verifying certificate...
2017-10-05 19:02:36 8160 3 Status: TLS connection established.
2017-10-05 19:02:36 8160 3 Command: USER user
2017-10-05 19:02:36 8160 3 Response: 331 Password required for user
2017-10-05 19:02:36 8160 3 Command: PASS ************
2017-10-05 19:02:36 8160 3 Response: 230 User user logged in
2017-10-05 19:02:36 8160 3 Command: OPTS UTF8 ON
2017-10-05 19:02:36 8160 3 Response: 200 UTF8 set to on
2017-10-05 19:02:36 8160 3 Command: PBSZ 0
2017-10-05 19:02:36 8160 3 Response: 200 PBSZ 0 successful
2017-10-05 19:02:36 8160 3 Command: PROT P
2017-10-05 19:02:36 8160 3 Response: 200 Protection set to Private
2017-10-05 19:02:36 8160 3 Status: Logged in
2017-10-05 19:02:36 8160 3 Status: Starting upload of C:\Users\User1\Pictures\test_sig.jpg
2017-10-05 19:02:36 8160 3 Command: CWD /folder
2017-10-05 19:02:36 8160 3 Response: 250 CWD command successful
2017-10-05 19:02:36 8160 3 Command: PWD
2017-10-05 19:02:36 8160 3 Response: 257 "/folder" is the current directory
2017-10-05 19:02:36 8160 3 Command: TYPE I
2017-10-05 19:02:36 8160 3 Response: 200 Type set to I
2017-10-05 19:02:36 8160 3 Command: PASV
2017-10-05 19:02:36 8160 3 Response: 227 Entering Passive Mode (xx,xx,xx,xx,xx,xx).
2017-10-05 19:02:36 8160 3 Command: STOR test_sig.jpg
2017-10-05 19:02:37 8160 3 Response: 150 Opening BINARY mode data connection for test_sig.jpg
2017-10-05 19:02:38 8160 3 Response: 226 Transfer complete
2017-10-05 19:02:38 8160 3 Status: File transfer successful, transferred 1.728.438 bytes in 1 second
2017-10-05 19:02:38 8160 3 Status: Retrieving directory listing of "/folder"...
2017-10-05 19:02:38 8160 3 Command: PASV
2017-10-05 19:02:38 8160 3 Response: 227 Entering Passive Mode (xx,xx,xx,xx,xx,xx).
2017-10-05 19:02:38 8160 3 Command: MLSD
2017-10-05 19:02:38 8160 3 Response: 150 Opening BINARY mode data connection for MLSD
2017-10-05 19:02:38 8160 3 Response: 226 Transfer complete
2017-10-05 19:02:38 8160 3 Status: Directory listing of "/folder" successful
Fabian
  • 177
  • 1
  • 14
  • Possible duplicate of [Unable to Upload file on FTP Server in android](https://stackoverflow.com/questions/11427346/unable-to-upload-file-on-ftp-server-in-android) – Dima Kozhevin Sep 28 '17 at 17:15
  • @DimaKozhevin it's not a duplicate to this question. I got true back from the storeFile function – Fabian Sep 29 '17 at 08:34
  • Show us [mcve]. Show us what is the actual value of `remoteFileName`. Tell us how to you check that the *"files are not uploaded"*. Show us a log file. – Martin Prikryl Oct 02 '17 at 05:59
  • @MartinPrikryl I deleted the unnecessary code from the Main and added the filename and path. So the RemoteFileName and the localFilePath are `remoteFileName = "com_777777_sig.jpg" ` `localFilePath = "/storage/emulated/0/comuniverseAppData/pictures/com_777777_sig.jpg"` – Fabian Oct 05 '17 at 15:15
  • @MartinPrikryl To check the files are not uploaded, look into the folder of my ftp server. – Fabian Oct 05 '17 at 15:18
  • Can you upload the file using a standalone FTP client, like WinSCP? Show us its log file. – Martin Prikryl Oct 05 '17 at 15:35
  • I can upload Files via filezilla on my laptop. I will try it on the android device. Btw. the android version I'm using is 4.3. – Fabian Oct 05 '17 at 16:03
  • @MartinPrikryl I used Turbo Ftp Client to test the upload of the file. It works fine. Which log do you mean? The LogCat do not write any other information than I already provide in the question. Thanks for you help – Fabian Oct 05 '17 at 16:17
  • Log of the FTP client. – Martin Prikryl Oct 05 '17 at 16:19
  • Ok I added the log of FileZilla in the question – Fabian Oct 05 '17 at 17:17
  • OK, looks good. Do you have access to server-side log? – Martin Prikryl Oct 05 '17 at 18:52
  • I had no access to the server log. So I installed a FTP on my Laptop and tried to connect with the android device. Now I got an errro. `(000003)06.10.2017 15:39:27 - test (192.168.0.138)> 550 CWD failed. "/192.168.0.113": directory not found.` So it was a copy and past fail of myself. In the constructor of the FtpDataHandler I set the serverDirectory = serverAdress. Thanks for you help. But it's very weird that there was no error thrown in the client, that the directory does not exist. – Fabian Oct 06 '17 at 13:59

1 Answers1

0

I fixed the Problem. It was a copy and past fail in the FtpDataHandler. I set the this.serverDirectory equals serverAdress. So the Ftp Client could not find the directory on the server.

Here is the code snippit:

public FtpDataHandler(String serverAdress, String userName, String password, String serverDiretory) {
    this.serverAdress = serverAdress;
    this.userName = userName;
    this.password = password;
    //this was my problem. I set the serverAdress to the serverDirectory insted the serverDirectory
    //this.serverDirectory = serverAdress;
    this.serverDirectory = serverDirectory;

    connect();
}  

I also fixed it in the question, so that it's easier to find the full solution.

Thanks to @Martin Prikryl for the support.

Fabian
  • 177
  • 1
  • 14