awareeye
awareeye

Reputation: 3061

unable to read file from ftp in android?

I am trying to read data from a file from ftp server. This piece of code works perfectly in java when i run from my desktop computer. I copied over the same code to android and i am getting an exception. The Exception is:

java.io.IOException: Unable to connect to server: Unable to retrieve file: 550

I have no idea why it is occurring when the same code is working perfecty in java. The java code is:

String s = "ftp://username:[email protected]:21/sg1996text.txt;type=i";
    URL u;
    String f="";
    try {
        u = new URL(s);
        URLConnection uc=u.openConnection();
        BufferedInputStream bis=new  BufferedInputStream(uc.getInputStream()); //This is where exception i raised.
        System.out.println("IS opened");
        int i;
        while((i=bis.read())!=-1)
            f=f+(char)i;
        System.out.println("File Read");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

Upvotes: 0

Views: 3154

Answers (2)

LSerni
LSerni

Reputation: 57388

Error 550 usually means "permission error", so the most likely cause is username/password mismatch (but see case #3).

Yet, if the same code works on your desktop, username and password ought to be correct. The possibilities I see are:

  • The package you are using is not the same between desktop and Android, and the Android version does not parse correctly username/password. Try sniffing the FTP traffic, or change FTP server address to a FTP server that you control (you can temporarily deploy one on your desktop), and verify whether username and password are transmitted correctly.
  • There is a DNS error and your desktop (or your Android) is not connecting to ftp.mysite.x10.mx but to somewhere else, e.g. a development installation on localhost (I did this once; took me some time to figure it out). Try changing the server name to its IP address.
  • As case (1), but the parsing error is caused by the TYPE I specification (";type=i" at end of URL) that the Android package does not recognize while the desktop one does. Should give a 500 Error, not 550; but several servers use error 550 to indicate file not found, and the package is believing that the file you want is 'sg1996text.txt;type=i' instead of 'sig1996text.txt'. Remove ';type=i' and see what happens.

Upvotes: 0

reynev
reynev

Reputation: 336

Remove:

;type=i

from your URL:

 String s = "ftp://username:[email protected]:21/sg1996text.txt;type=i";

It works in my application.

Upvotes: 1

Related Questions