将文件从SD卡上传到FileZilla Server

时间:2014-06-24 11:20:13

标签: android android-asynctask filezilla ftp-server

我的应用程序是将文件从SD卡上传到FileZilla FTP Server上的目录。在运行我的appliaction之后,它给了我一些异常,在经过这么多搜索后我无法解决。

这是log cat输出:

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)

这是我的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 个答案:

答案 0 :(得分:1)

经过这么多天我再次研究这件事,成功地把事情搞定了。所以,我发布了我的答案。希望它可以帮助一些人。

这是我的主要活动代码:

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

}

请在应用程序运行时检查日志cat,您将了解它是如何工作的。 感谢。

答案 1 :(得分:0)

似乎SimpleFTP库实现不符合FTP规范(RFC 959)。根据找到的源代码here,它希望FTP服务器的初始回复以"220 "开头,而RFC指定 [an] FTP回复由三位数字组成(传输为三个)字母数字字符后跟一些文字。因此允许以"220-"开头的回复。

我建议使用更好的FTP库,例如apache commons ftpftp4j