Search This Blog

Monday, 17 February 2014

Android FTP client tutorial with example of uploading, downloading and authentication with FTP server

FTP protocol is standard network protocol for transferring file. It works on peer to peer base network. In recent days I had written few articles about file transfer protocol and comparison among them to choose best. After research, research shows that FTP outperformed everything in android. So I decide to write about FTP client implementation. This tutorial will teach you
  • Android FTP client Authentication - How to connect with FTP server?
  • Android FTP client Download - How to download a file from FTP server?
  • Android FTP client Upload -How to upload a file to FTP server?

Basic requirement for this tutorial is one simple FTP server setup at your desktop. You can use file-Zilla or IIS server for this requirement.

  • Android FTP client authentication and listing file from FTP server

      /**  
       *   
       * @param ip  
       * @param userName  
       * @param pass  
       */  
      public void connnectingwithFTP(String ip, String userName, String pass) {  
           boolean status = false;  
           try {  
                FTPClient mFtpClient = new FTPClient();  
                mFtpClient.setConnectTimeout(10 * 1000);  
                mFtpClient.connect(InetAddress.getByName(ip));  
                status = mFtpClient.login(userName, pass);  
                Log.e("isFTPConnected", String.valueOf(status));  
                if (FTPReply.isPositiveCompletion(mFtpClient.getReplyCode())) {  
                     mFtpClient.setFileType(FTP.ASCII_FILE_TYPE);  
                     mFtpClient.enterLocalPassiveMode();  
                     FTPFile[] mFileArray = mFtpClient.listFiles();  
                     Log.e("Size", String.valueOf(mFileArray.length));  
                }  
           } catch (SocketException e) {  
                e.printStackTrace();  
           } catch (UnknownHostException e) {  
                e.printStackTrace();  
           } catch (IOException e) {  
                e.printStackTrace();  
           }  
      }  

  • Android FTP client download a file from FTP server – you can browse through directory saved on FTP server and can download desired file or directory. I just write code here for downloading a single file and writing it to sdcard/phone memory.

      /**  
       * @param ftpClient FTPclient object  
       * @param remoteFilePath  FTP server file path  
       * @param downloadFile   local file path where you want to save after download  
       * @return status of downloaded file  
       */  
      public boolean downloadSingleFile(FTPClient ftpClient,  
                String remoteFilePath, File downloadFile) {  
           File parentDir = downloadFile.getParentFile();  
           if (!parentDir.exists())  
                parentDir.mkdir();  
           OutputStream outputStream = null;  
           try {  
                outputStream = new BufferedOutputStream(new FileOutputStream(  
                          downloadFile));  
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);  
                return ftpClient.retrieveFile(remoteFilePath, outputStream);  
           } catch (Exception ex) {  
                ex.printStackTrace();  
           } finally {  
                if (outputStream != null) {  
                     try {  
                          outputStream.close();  
                     } catch (IOException e) {  
                          e.printStackTrace();  
                     }  
                }  
           }  
           return false;  
      }  

For browsing through directory see this Browsing Nested Directory

  • Android FTP client uploading a file to FTP server – You can upload a file to server with object of FTPClient at desired path which you need to define.

      /**  
       *   
       * @param ftpClient FTPclient object  
       * @param downloadFile local file which need to be uploaded.  
       */  
      public void uploadFile(FTPClient ftpClient, File downloadFile,String serverfilePath) {  
           try {  
                FileInputStream srcFileStream = new FileInputStream(downloadFile);  
                boolean status = ftpClient.storeFile("remote ftp path",  
                          srcFileStream);  
                Log.e("Status", String.valueOf(status));  
                srcFileStream.close();  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  

Download Jar  

 

Note : Keep all methods inside background thread (i.e Asynchronous Task, Service)

 See Sambha file sharing client for android

Wednesday, 12 February 2014

Best file transfer protocol : Which to use among SMB, Socket (TCP/UDP) and FTP?



I had gone through the requirement of file transfer using peer to peer in a same network. I tried the case of server and client too because I had not any issue to run server script (in case of Socket Connection). Main purpose was to attain maximum transfer speed of data. So I tried with three most popular protocols for file transfers which are –
  • SMB – Server Message Protocols known also as sambha file sharing
  • Socket- Work on the base of client server concept
  • FTP- File transfer protocol

Comparison for performance –


Phone
Protocol
Data
Distance
Time
Max Speed
XOLO
Socket
1.3 GB
5 Meter
16 Min
1.3 MB/S
XOLO
SMB
0.7 GB
5 Meter
25 Min
0.5 MB/S
XOLO
FTP
1.3 GB
5 Meter
14 Min
1.6 MB/S

Notable Point about file transfer protocols –

  • Socket – Server scripting required for file transfer. This connection cannot be named as peer to peer
  • SMB – No scripting required for file transfer. It can read any file structure of peer computer
  • FTP – FileZilla can do the trick for file transfer. No Server script required

Term and Condition – These all testing has been done between an android and a low configuration desktop (Windows 7). Both were connected to one dedicated wifi local network



Android News and source code