Thursday, May 17, 2012

How to FTP file Upload and Download in android?



Requirement :
·      FTP hostname
·      Username and Password of FTP server
Features :
·      Android (local) and remote (FTP) file browsers.
·      Upload and download with resume support.
·      Folders selection support for upload, download and delete.
Code for Upload file to FTP Server :
FTPClient ftp = new FTPClient();
                System.out.println(ftp.connect(host)[0]);
        ftp.login("username of FTP", "password of FTP");
        Log.v("log_tag", "path : " + pat.getText().toString());
        ftp.upload(new java.io.File(pat.getText().toString()));
Code for Download from FTP Server :
               FTPClient ftp = new FTPClient();
        System.out.println(ftp.connect(host)[0]);
        ftp.login("username of FTP", "password of FTP");
        File fileDownload = new File("/sdcard/testnew.xml");
        fileDownload.createNewFile();
        // File.createTempFile("testnew", ".xml");
        ftp.changeDirectory("dhaval");
        // ftp.download("testnew.xml", fileDownload);
        ftp.download("testnew.xml", fileDownload,
                        new FTPDataTransferListener() {

                            public void transferred(int arg0) {
                                Log.v("log_tag", "This is for tranfer");

                            }

                            public void started() {
                                // TODO Auto-generated method stub
                                Log.v("log_tag", "This is for started");
                            }

                            public void failed() {
                                Log.v("log_tag", "This is for failed");
                            }

                            public void completed() {

                                Log.v("log_tag", "This is for completed");

                            }

                            public void aborted() {
                                Log.v("log_tag", "This is for aborted");

                            }
                        });
Reference link
FTP jar : http://sourceforge.net/projects/ftp4j/

3 comments:

  1. Hi can you upload a proyect please, it doesnt run on my eclipse but has no errorss!!

    thanks nice work that web

    ReplyDelete
  2. For load, error

    "Pat cannot be resolved"

    ReplyDelete