The following code is tested on a PC, but it does not work on the Android emulator, but can also be linked to the server but cannot get the file tangled half a day, the original is the problem of the pattern, specifically please Google simulator in the use of suggested passive mode
Public voidGetworkmessage () {ftpclient ftp=Newftpclient (); Try{ftp.connect (server); System.out.println ("Connected to" +server); System.out.print (Ftp.getreplystring ()); Reply=Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); System.err.println ("FTP server refused connection."); System.exit (1); } ftp.login (username, password); ftpfile[] Files= Ftp.listfiles ("/"); if(files!=NULL&&files.length>0){ for(Ftpfile f:files) {System.out.println ("User:>>>" +f.getuser () + "name:>>>" +f.getname () + "size:>>>" +f.getsize () + "link:>> > "+F.getlink ()); } } } Catch(SocketException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }
After adding the passive mode, the code is as follows:
Public voidGetworkmessage () {ftpclient ftp=Newftpclient (); Try{ftp.connect (server); System.out.println ("Connected to" +server); System.out.print (Ftp.getreplystring ()); Reply=Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); System.err.println ("FTP server refused connection."); System.exit (1); } ftp.login (username, password); //set to passive modeFtp.enterlocalpassivemode (); ftpfile[] Files= Ftp.listfiles ("/"); if(files!=NULL&&files.length>0){ for(Ftpfile f:files) {System.out.println ("User:>>>" +f.getuser () + "name:>>>" +f.getname () + "size:>>>" +f.getsize () + "link:>> > "+F.getlink ()); } } } Catch(SocketException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } }
OK, that's it.
Problems with FTP using the emulator under the Android platform