1

我想连接 FTP 并从那里获取文件列表。这是我的代码:

public class MainActivity extends Activity {

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


        task getson = new task();
        getson.execute();




    }

    FTPClient client;

    public FTPClient connectWithFTP() throws IOException{
         client = new FTPClient();  
         try {

             client.connect(server, 21);  
             System.out.println("status :: " + client.getReplyString());
             int reply = client.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    throw new Exception("Connect failed: " + client.getReplyString());
                }
                try {
                    client.enterLocalPassiveMode();
                    if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
                        Log.v(getClass().toString(), "Setting binary file type failed.");
                    }
                } catch(Exception e) {
                    e.printStackTrace();
                } 
             checkFiles(client);
        } catch (Exception e) {
            System.out.println("status :: " + client.getReplyString());
        }
          return client;
    }

    private class task extends AsyncTask <Void, Void, FTPClient> {

        @Override
        protected void onPreExecute() {
            // Do stuff before the operation
        }


        @Override
        protected FTPClient doInBackground(Void... params) {
            FTPClient ftp = null;
            try {
                ftp = connectWithFTP();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ftp;
        }
    }

    public void disconnectWithFTP() throws IOException{
        client.logout();  
        client.disconnect();  
    }

    private void checkFiles(FTPClient clients){
          try {  
              FTPFile[] ftpFiles = clients.listFiles();  
              int length = ftpFiles.length;  
              for (int i = 0; i < length; i++) {  
                String name = ftpFiles[i].getName();  
                Calendar date = ftpFiles[i].getTimestamp();
                Log.v("aasd", name+", "+date);

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

连接工作,但是当我想获取文件列表时,我得到:

10-29 12:25:25.113: I/System.out(1980): status :: 220 (vsFTPd 2.2.2)
10-29 12:25:25.542: V/class com.example.ftp.MainActivity(1980): Setting binary file type failed.
10-29 12:25:36.332: W/System.err(1980): java.io.IOException: Unable to determine system type - response: 530 Please login with USER and PASS.
10-29 12:25:36.343: W/System.err(1980):     at org.apache.commons.net.ftp.FTPClient.getSystemType(FTPClient.java:2721)
10-29 12:25:36.347: W/System.err(1980):     at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3256)
10-29 12:25:36.355: W/System.err(1980):     at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)
10-29 12:25:36.363: W/System.err(1980):     at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2977)
10-29 12:25:36.371: W/System.err(1980):     at com.example.ftp.MainActivity.checkFiles(MainActivity.java:87)
10-29 12:25:36.378: W/System.err(1980):     at com.example.ftp.MainActivity.connectWithFTP(MainActivity.java:53)
10-29 12:25:36.382: W/System.err(1980):     at com.example.ftp.MainActivity$task.doInBackground(MainActivity.java:72)
10-29 12:25:36.390: W/System.err(1980):     at com.example.ftp.MainActivity$task.doInBackground(MainActivity.java:1)
10-29 12:25:36.398: W/System.err(1980):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-29 12:25:36.398: W/System.err(1980):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-29 12:25:36.398: W/System.err(1980):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-29 12:25:36.402: W/System.err(1980):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-29 12:25:36.402: W/System.err(1980):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-29 12:25:36.402: W/System.err(1980):     at java.lang.Thread.run(Thread.java:1019)

此服务器已打开,无需登录名和密码。为什么我无法获取文件列表?

4

1 回答 1

2

我解决了我的问题。这是解决方案:

client.login("anonymous", "anonymous");
于 2013-10-29T11:45:23.987 回答