Uploading file from SD Card to FileZilla Server

2019-06-22 16:07发布

My application is uploading the file from SD Card to the directory on FileZilla FTP Server. After running my appliaction it gives me exception which I am unable to resolve after so many searches.

here is the log cat output:

06-24 11:06:53.715: W/System.err(1304): java.io.IOException: SimpleFTP received an unknown response when connecting to the FTP server: 220-FileZilla Server version 0.9.41 beta
06-24 11:06:54.055: W/System.err(1304):     at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:74)
06-24 11:06:54.087: W/System.err(1304):     at com.example.upload1.MainActivity$UploadVideo.doInBackground(MainActivity.java:63)
06-24 11:06:54.167: W/System.err(1304):     at com.example.upload1.MainActivity$UploadVideo.doInBackground(MainActivity.java:1)
06-24 11:06:54.167: W/System.err(1304):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-24 11:06:54.167: W/System.err(1304):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-24 11:06:54.167: W/System.err(1304):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-24 11:06:54.167: W/System.err(1304):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-24 11:06:54.167: W/System.err(1304):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-24 11:06:54.403: W/System.err(1304):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-24 11:06:54.403: W/System.err(1304):     at java.lang.Thread.run(Thread.java:856)

and this is my code for the MainActivity.java

import java.io.File;

    import org.jibble.simpleftp.SimpleFTP;

    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    //import com.kpbird.ftpdemo.R;




    public class MainActivity extends Activity implements OnClickListener {
        //FTPClient client;


    /*********  work only for Dedicated IP ***********/
    static final String FTP_HOST= "203.199.134.131";

    /*********  FTP USERNAME ***********/
    static final String FTP_USER = "a_gupta";

    /*********  FTP PASSWORD ***********/
    static final String FTP_PASS  ="AditI123";

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

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }
    public void onClick(View v) {
        UploadFile async = new UploadFile();
        async.execute();

        }

        class UploadFile extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
        // ftpClient=uploadingFilestoFtp();
        try {
        SimpleFTP ftp = new SimpleFTP();

        ftp.connect(FTP_HOST, 21, FTP_USER, FTP_PASS);

        ftp.bin();

        // Change to a new working directory on the FTP server.
        ftp.cwd("callrecording");

        // Upload some files.
        ftp.stor(new File(Environment.getExternalStorageDirectory().getParent() + "/invite_json.txt"));
        // ftp.stor(new File("comicbot-latest.png"));

        // You can also upload from an InputStream, e.g.
        // ftp.stor(new FileInputStream(new File("test.png")),
        // "test.png");
        // ftp.stor(someSocket.getInputStream(), "blah.dat");

        // Quit from the FTP server.
        ftp.disconnect();

        } catch (Exception e) {
        e.printStackTrace();
        }

        return null;
        }

        @Override
        protected void onPreExecute() {
        super.onPreExecute();
        // dialog.show();
        }

        @Override
        protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Toast.makeText(MainActivity.this, "sent", Toast.LENGTH_LONG).show();
        }
        }

2条回答
对你真心纯属浪费
2楼-- · 2019-06-22 16:30

It seems that the SimpleFTP library implementation is not conforming to the FTP specification (RFC 959). According to the source found here it expects the initial reply of the FTP server to start with "220 " while the RFC specifies that [an] FTP reply consists of a three digit number (transmitted as three alphanumeric characters) followed by some text. So a reply starting with "220-" is allowed.

I suggest using a better FTP library such as apache commons ftp or ftp4j.

查看更多
唯我独甜
3楼-- · 2019-06-22 16:36

After so many days i worked on this thing again and successfuly got the things working. So, I am posting my answer.Hope it would help some one.

This is my Main Activity Code:

package com.example.ftpup;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {


    /*********  work only for Dedicated IP ***********/
    static final String FTP_HOST= "203.199.134.131";

    /*********  FTP USERNAME ***********/
    static final String FTP_USER = "a_gupta";

    /*********  FTP PASSWORD ***********/
    static final String FTP_PASS  ="AditI123";
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    Button btn;
    FTPClient client = new FTPClient();
    private ProgressDialog mProgressDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.uploadButton);
        btn.setOnClickListener(this);

    }

    public void onClick(View v) {


         startDownload();
    }
   // /*******  Used to file upload and show progress  **********/




        private void startDownload() {
            //Pick file
            //File f = new File("/sdcard/invite_json.txt");
           // Async task

            new DownloadFileAsync().execute();
        }
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                mProgressDialog = new ProgressDialog(this);
                mProgressDialog.setMessage("Downloading file..");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();
                return mProgressDialog;
            default:
                return null;
            }
        }
        // Upload sdcard file


    //public void uploadFile(File fileName){

    class DownloadFileAsync extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);


        }@Override
        protected String doInBackground(String... filename) {
            String sdcard = Environment.getExternalStorageDirectory().toString();
            File file = new File(sdcard+"/androrec/");
            //File file2 = new File("/mnt/sdcard/CallRecorderBackup");

            File[] listofFiles = file.listFiles();
            Log.d("No. of files" ,fileArrayToString(listofFiles));
            try{
                client.connect(FTP_HOST,21);



            for (int i = 0; i <=listofFiles.length ; i++) {


                    Log.d("Tag 4", "inside of ftp connect");

                Log.d("Tag 1", "inside of for loop");

                if (listofFiles == null || listofFiles[i].isFile()) {
                    Log.d("Tag 2", "inside of if");
                    String files = listofFiles[i].getName();
                    Log.d("Tag 3", "inside of files");

                    File filetoload = new File(sdcard+"/androrec/"+files);
                    String uploadingFiles = filetoload.toString();
                    Log.d("Files uploading to the server", uploadingFiles);
                    client.login(FTP_USER, FTP_PASS);
                            //client.connect(FTP_HOST,21);
                            Log.d("Tag 4", "inside of ftp connect");

                        Log.d("Tag 5", "inside of for ftp pass");
                        //client.setType(FTPClient.TYPE_BINARY);
                        //client.changeDirectory("/");
                        Log.d("Tag 6", "inside of for dir");

                    client.upload(filetoload);
                    Log.d("Tag 7", "inside of download");
                    //out.write(buf, 0, len);
                //}
                //} 






           // client.upload(in, new MyTransferListener());

        } 
                else{

                }
                //client.disconnect(true);
                }
            }

                 catch (IllegalStateException | IOException
                        | FTPIllegalReplyException | FTPException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FTPDataTransferException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FTPAbortedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            try {
                client.disconnect(true);   
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            return null;
            }



            /*}
            }*/

        @Override
        protected void onPostExecute(String unused) {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

    }

   // }
    String fileArrayToString(File[] f){
        String output = "";
        String delimiter = "\n" ;// Can be new line \n tab \t etc...
        for (int i=0; i<f.length; i++)
        {
            output = output + f[i].getPath() + delimiter;
        }

        return output;
    }

}

Please check the log cat while your application is running you will get to know how this is working. Thanks.

查看更多
登录 后发表回答