Track FTP upload data in android?

后端 未结 3 1744
粉色の甜心
粉色の甜心 2021-02-06 15:36

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 possibl

相关标签:
3条回答
  • 2021-02-06 15:43

    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

    0 讨论(0)
  • 2021-02-06 15:44

    Download this .jar file

    httpmime-4.1.1.jar and commons-net.jar

            try {
    
                FTPClient ftpClient = new FTPClient();
    
                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 
                            File file = new File("/sdcard/my pictures/image.png");  
                BufferedInputStream buffIn = new BufferedInputStream(
                        new FileInputStream(myImageFile));
                ftpClient.enterLocalPassiveMode();
                ProgressInputStream progressInput = new ProgressInputStream(
                        buffIn);
    
                boolean result = 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;
    
    public class ProgressInputStream extends InputStream {
    
        /* Key to retrieve progress value from message bundle passed to handler */
        public static final String PROGRESS_UPDATE = "progress_update";
    
        private static final int TEN_KILOBYTES = 1024 * 40;
    
        private InputStream inputStream;
        //private Handler handler;
    
        private long progress;
        private long lastUpdate;
    
        private boolean closed;
    
        public ProgressInputStream(InputStream inputStream) {
            this.inputStream = inputStream;
    
            this.progress = 0;
            this.lastUpdate = 0;
    
            this.closed = false;
        }
    
        @Override
        public int read() throws IOException {
            int count = inputStream.read();
            return incrementCounterAndUpdateDisplay(count);
        }
    
        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            int count = inputStream.read(b, off, len);
            return incrementCounterAndUpdateDisplay(count);
        }
    
        @Override
        public void close() throws IOException {
            super.close();
            if (closed)
                throw new IOException("already closed");
            closed = true;
        }
    
        private int incrementCounterAndUpdateDisplay(int count) {
            if (count < 0)
                progress += count;
            lastUpdate = maybeUpdateDisplay(progress, lastUpdate);
            return count;
        }
    
        private long maybeUpdateDisplay(long progress, long lastUpdate) {
            if (progress - lastUpdate < TEN_KILOBYTES) {
                lastUpdate = progress;
                sendLong(PROGRESS_UPDATE, progress);
            }
            return lastUpdate;
        }
    
        public void sendLong(String key, long value) {
            Bundle data = new Bundle();
            data.putLong(key, value);
    
            Message message = Message.obtain();
            message.setData(data);
            //handler.sendMessage(message);
        }
    }
    
    0 讨论(0)
  • 2021-02-06 15:45

    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.

    0 讨论(0)
提交回复
热议问题