Connection to FTP server works from Filezilla but returns 530 from ftpClient Apache Commons in Android

501 Views Asked by At

I want to send some files to an FTP server from Android. I have the server IP address, username, and password. I tried to connect to it from Filezilla and it works, however, if I try to connect from Android it fails. I get status code 530 from ftpClient.getReplyCode().

According to https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes the status code means that the login didn't work. ftpClient.login returns false. So I tried mounting an FTP server with node.js and could connect and upload files perfectly. Then I tried to connect to another test server ftp://test.rebex.net/ using username: demo and password: password and the login works too (uploading files fails because its a test account).

But why do I fail to log in to that specific server from Android but not from Filezilla?

My code:

    uploading_to_local_server = true;
    FTPClient ftpClient = new FTPClient();
    try {
        if(port == 0){
            ftpClient.connect(ftpserver);
        }else {
            ftpClient.connect(ftpserver, port);
        }

        ftpClient.setSoTimeout(10000);
        ftpClient.enterLocalPassiveMode();
        Log.d(TAG,"FTP. TRYING TO LOGIN ");
        Boolean login_response = ftpClient.login(ftp_usr,ftp_psw);
        mreplyCode = ftpClient.getReplyCode();
        Log.d(TAG,"FTP. RESPONSE " + mreplyCode);
        if (login_response) {
            Log.d(TAG, "FTP. VIDEOS TO UPLOAD: LOGGED SUCCESFULLY");
            //Logged.

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

            //iterating through the videos
            String video_upload_result = "Uknown Error";
            ArrayList<HashMap<String, String>> list_videos_to_upload = dataBaseHelper.getVideosLocalserverH();
            for (int counter = 0; counter < list_videos_to_upload.size(); counter++) {
                video_upload_result = "Uknown Error";
                //list_videos_to_upload.get(counter).put("imei", IMEI);
                String video_path = list_videos_to_upload.get(counter).get("dir_route");
                String video_name = list_videos_to_upload.get(counter).get("video_name");
                String vid_id = list_videos_to_upload.get(counter).get("id");
                String sync_status = list_videos_to_upload.get(counter).get("sync");

                Log.d(TAG, "VIDEOS TO UPLOAD: " + video_path + " ID: " + vid_id);
                //Log.d(TAG, "WIFI STATUS: " + mWifi.isConnected() + " DATA STATUS: " + wData.isConnected());

                video_upload_result = try_upload_video_to_ftpserver(ftpClient, video_name, remote_path, video_path);

                Log.d(TAG, "VIDEOS TO UPLOAD: " + video_path + " RESULT: " + video_upload_result);


                if(video_upload_result.equals("Succes")) {
                    Log.d(TAG, "TESTING UPLOAD: VIDEO ID: "+ vid_id);
                    dataBaseHelper.modVideos_Localserver(Collections.singletonList(vid_id));
                }else if(video_upload_result.equals(video_path + ": open failed: ENOENT (No such file or directory)")){
                    Log.d(TAG, "TESTING UPLOAD: ERROR CATCHED. FILE NOT FOUND: "+ vid_id);
                    dataBaseHelper.modVideos_Localserver(Collections.singletonList(vid_id));
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    Log.d(TAG, "Error Sleeping thread");
                    e.printStackTrace();
                }



            }

        }

        ftpClient.logout();
        ftpClient.disconnect();
    }catch (IOException e) {
        e.printStackTrace();
    }
    Log.d(TAG, "UPLOADING LOOP ENDED: ");
    uploading_to_local_server = false;

EDIT:

Finally Managed to make it work.

Turns out the server only accepted connections through ftps. So I changed: FTPClient ftpClient = new FTPClient(); for FTPSClient ftpClient = new FTPSClient("TLS", false);

and I managed to connect to the server but still coudn´t upload videos because the server also only accepts encrypted data, so I added: ftpClient.execPROT("P"); // encrypt data channel

And now I can finally upload videos to that server.

1

There are 1 best solutions below

0
AngelDev0329 On

After connect to FTP, and then login to FTP. If to login is success, then you need to let localPassiveMode:

ftpClient.enterLocalPassiveMode();

Then you can upload file to FTP server.

Thank you