2013-10-29 65 views
0

我想用FTP連接,並得到弗羅姆有文件列表獲取文件列表。 這是我的代碼:無法從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) 

這serwer是開放的,沒有登錄名和密碼。爲什麼我無法獲取文件列表?

回答

1

我解決我的問題。 這是解決方案:

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