开发者

How to get last created file from an FTP directory?

I first want to get a list of files stored in an FTP directory and then get the name of last created file using timestamp. And I'm getting an alert box: Activity is not responding. After checking logcat entry, I notice that the code never reach line :

Log.e("FTP", "number of filenames: " + count);

But I get to Log.e("FTP", "Connexion successful "); So connexion to the server seems ok.

It Seems like something going wrong out there. Can someone help me deal with it. Or show me a simple way to get the last created file from an the FTP server director?

FTPClient ftpClient = new FTPClient();
try 
{
    ftpClient.connect(InetAddress.getByName(Fonctions.address), Integer.parseInt(Fonctions.port));              
    if (ftpClient.login(Fonctions.login, Fonctions.pass))
    {                           
    Log.e("FTP", "Connexion successful ");
    String workDir = ftpClient.printWorkingDirectory();
    //Log.e("FTP", "workdir:" + workDir);

    int count = ftpClient.listNames().length;   
    Log.e("FTP", "number of filenames: " + count);
    FTPFile [] dossier = new FTPFile[count];                                
    FTPFile back = new FTPFile();

    dossier = ftpClient.listDirectories("Sarelo_FTP");
    back = dossier[0];
    Log.e("FTP", "Avant boucle " + back);
    int buf = 0;
    for (int i=0;i<(dossier.length) - 1;i++)
      {
       for (int j=1;j<dossier.length;j++)
         {
          buf = back.getTimestamp().compareTo(dossier[j].getTimestamp());                                   
          if (buf == -1)
            back = dossier[j];
         }                          
      } 
    Log.e("FTP", "fichier final le plus récent: " + back.getName());
    }
    else{
        Log.e("Restore FTP", "Error while connecting to FTP server");
    }
} 
catch(IOException e)
{
String title = "Error connecting to FTP server";
String msg = "Please check your parameters and connexion info: login, password,port number";
f.alert(c, title, msg).show();
Log.e("Restore FTP", "Error while connecting to FTP server", e);
}

P.S: I can't get the list of files in the directory so, I don't know if my code to retrieve the last created file is working. Any help on that would also be appreciated.

[Edit] This is my AsyncTask to retrieve the list of files in the directory. But it's still not working. I'm not getting Application Not Responding an开发者_运维百科ymore, but It not seems to do anything else. Execution get stuck at the same point (can't reach Log.e("FTP", "number of filenames: " + count); )

class getFilesFromFtp extends AsyncTask
{
    @Override
    protected String doInBackground(Object... params) 
    {
        int count = 0;
        try 
        {
            Log.e("FTP", "avant names: " + count);
            count = ftpClient.listNames().length;
            Log.e("FTP", "names: " + count);
            handler.sendMessage(handler.obtainMessage());
        } 
        catch (IOException e) {                                 
            Log.e("FTP", "Error getting number of files ", e);
        }           
    return null;
   }
}

Thanks for help.


You must not execute long running code on UI thread. Thia blocks UI redraw and event handling. It also produces ANR.

You should run it in the background thread, preferably via 'AsyncTask'.


First problem solved. I just needed to activate FTP data connection passive mode like that:

ftpClient.enterLocalPassiveMode();

before line int count = ftpClient.listNames().length;.

Hope this will help other people. Thanks to @Peter Knego driving me to AsynkTask. I learned something new. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜