Question

I searched here for this topic and I tried all I found, but it still does not work.

    import java.io.FileInputStream;
    import java.io.IOException;

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

    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;


    public class MainActivity extends Activity {

        public static final String TAG = "Contacts";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

I made a Thread, because it wasn't allowed to run it on the main thread;

            Thread t = new Thread(new Runnable(){
                @Override
                public void run(){
                    Versuch();
                }
            });
            t.start();
    }

Here I try to upload the data and it does not show me an error in Log Cat.

public void Versuch(){
        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {
            client.connect("ftp-web.example");
            client.login("ftpuser", "ftppassword");


            String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM";
            fis = new FileInputStream(filename);

            // Store file to server
            //
            client.storeFile(filename, fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }
}
Was it helpful?

Solution

Since you didnt get any error try followed example that worked for me.

Before,

  • check that FTP is enable for your server.
  • be sure that device has the same sub net with Server
  • (optimal) on running FTP upload, check on server side access_log / error_log with tail. Verify that server listens and gets something

I use commons-net-3.1.jar

Here is the method where we upload bin file:

public void uploadFileToServer(
                            String serverIP,
                            String binFileToStore,
                            String workingRemoteFolder,
                            String localFilePath,
                            String timeout
                            ){
Log.d("test", "init FTP client ...");


// set FTP client
FTPClient client = new FTPClient();
client.setConnectTimeout(Integer.parseInt(timeout)*1000);
client.setDefaultTimeout(Integer.parseInt(timeout)*1000);
client.setControlKeepAliveTimeout(Long.parseLong(timeout) );


try{
    int reply;

    Log.d("test", "connecting ...");


    client.connect(serverIP);
    // After connection attempt, you should check the reply code to verify
    // success.
    reply = client.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply))
    {
        client.disconnect();

        //error

        return;
    }
}
catch (IOException e){

    return;
}

/** set setup configuration. We upload bin file */ 
FileInputStream fis = null;

try{

    client.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();

    client.login("automation", "automation");

    //
    // Create an InputStream of the file to be uploaded
    //
    fis = new FileInputStream(localFilePath);


    if (!client.changeWorkingDirectory(workingRemoteFolder)){
        client.makeDirectory(workingRemoteFolder);
        client.changeWorkingDirectory(workingRemoteFolder);
    }

    Log.d("test", "store file ...");

    boolean result = client.storeFile(binFileToStore, fis);

    // done

    client.logout();

} catch (IOException e) {

} finally {
    try {
        if (fis != null) {
            fis.close();
        }
        client.disconnect();
    } catch (IOException e) {

        return;
    }
  }     
}

Hope this example will help you

OTHER TIPS

Here's the solution to my question: I forgot to say the correct FileType and TransferMode. And important: set FileType and TransferMode after logging in.

Here's the whole corrected code:

package com.example.upload_contacts;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

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

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;


public class MainActivity extends Activity {

    public static final String TAG = "Contacts";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread t = new Thread(new Runnable(){
            @Override
            public void run(){
                jetzt();
            }
        });
        t.start();
        Log.i(TAG, "thread started");

    }

    public void jetzt(){
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect("YourHostHere");

            ftpClient.setSoTimeout(10000);
            ftpClient.enterLocalPassiveMode();
            if(ftpClient.login("YourUserHere", "YourPassHere"))
            {
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
                FileInputStream fs= new FileInputStream(sFile);
                String fileName = sFile.getName();
                Boolean result = ftpClient.storeFile(fileName, fs);
                fs.close();
                Log.i(TAG, "sent");
                String has = "";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top