Skip to content Skip to sidebar Skip to footer

Track Ftp Upload Data In Android?

I have a working FTP system with android, but I want to be able to track the bytes as they get uploaded, so I can update a progress bar as the upload progresses. Is this possible w

Solution 1:

This question has an implementation of an InputStream that includes a progress callback. Use that InputStream and call publishProgress from that callback for incremental updates during the file upload.

Solution 2:

Download this .jar file

httpmime-4.1.1.jar and commons-net.jar

try {

            FTPClientftpClient=newFTPClient();

            ftpClient.connect(InetAddress
                    .getByName(Your host Url));
            ftpClient.login(loginName, password);
            System.out.println("status :: " + ftpClient.getStatus());

            ftpClient.changeWorkingDirectory(your directory name);

            ftpClient.setFileType(FTP.IMAGE_FILE_TYPE);
                        //Your File path set here Filefile=newFile("/sdcard/my pictures/image.png");  
            BufferedInputStreambuffIn=newBufferedInputStream(
                    newFileInputStream(myImageFile));
            ftpClient.enterLocalPassiveMode();
            ProgressInputStreamprogressInput=newProgressInputStream(
                    buffIn);

            booleanresult= ftpClient.storeFile(UPLOADFILENAME + ".png",
                    progressInput);

            System.out.println("result is  :: " + result);
            buffIn.close();
            ftpClient.logout();
            ftpClient.disconnect();

        } catch (Exception e) {
            e.printStackTrace();

        }

//ProgressInputStream

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

publicclassProgressInputStreamextendsInputStream {

    /* Key to retrieve progress value from message bundle passed to handler */publicstaticfinalStringPROGRESS_UPDATE="progress_update";

    privatestaticfinalintTEN_KILOBYTES=1024 * 40;

    private InputStream inputStream;
    //private Handler handler;privatelong progress;
    privatelong lastUpdate;

    privateboolean closed;

    publicProgressInputStream(InputStream inputStream) {
        this.inputStream = inputStream;

        this.progress = 0;
        this.lastUpdate = 0;

        this.closed = false;
    }

    @Overridepublicintread()throws IOException {
        intcount= inputStream.read();
        return incrementCounterAndUpdateDisplay(count);
    }

    @Overridepublicintread(byte[] b, int off, int len)throws IOException {
        intcount= inputStream.read(b, off, len);
        return incrementCounterAndUpdateDisplay(count);
    }

    @Overridepublicvoidclose()throws IOException {
        super.close();
        if (closed)
            thrownewIOException("already closed");
        closed = true;
    }

    privateintincrementCounterAndUpdateDisplay(int count) {
        if (count < 0)
            progress += count;
        lastUpdate = maybeUpdateDisplay(progress, lastUpdate);
        return count;
    }

    privatelongmaybeUpdateDisplay(long progress, long lastUpdate) {
        if (progress - lastUpdate < TEN_KILOBYTES) {
            lastUpdate = progress;
            sendLong(PROGRESS_UPDATE, progress);
        }
        return lastUpdate;
    }

    publicvoidsendLong(String key, long value) {
        Bundledata=newBundle();
        data.putLong(key, value);

        Messagemessage= Message.obtain();
        message.setData(data);
        //handler.sendMessage(message);
    }
}

Solution 3:

This can be done using Secure FTP Factory library.

You just need to implement an instance of the com.jscape.inet.ftp.FtpListener interface, register the instance with the Ftp class and overload the progress(FtpProgressEvent event) method to capture progress information.

JavaDoc: Overview (secure FTP Factory API)

Download: Java FTP, Java FTPS and Java SFTP Components

Post a Comment for "Track Ftp Upload Data In Android?"