how to add ProgressMonitorInputStream to ftp upload?

Go To StackoverFlow.com

0

Can anybody see what is wrong with this code. it does not show up progress-bar but uploades all the files. I did checkout sun tutorial and swingworkers also but i couldn't fix it yet.

private static boolean putFile(String m_sLocalFile, FtpClient m_client) {
    boolean success = false;
    int BUFFER_SIZE = 10240;
    if (m_sLocalFile.length() == 0) {
        System.out.println("Please enter file name");
    }
    byte[] buffer = new byte[BUFFER_SIZE];
    try {
        File f = new File(m_sLocalFile);
        int size = (int) f.length();
        System.out.println("File " + m_sLocalFile + ": " + size + " bytes");
        System.out.println(size);
        FileInputStream in = new FileInputStream(m_sLocalFile);
        //test
        InputStream inputStream = new BufferedInputStream(
                      new ProgressMonitorInputStream(null,"Uploading " + f.getName(),in));

        //test
        OutputStream out = m_client.put(f.getName());

        int counter = 0;
        while (true) {
            int bytes = inputStream.read(buffer);  //in
            if (bytes < 0)
                break;
            out.write(buffer, 0, bytes);
            counter += bytes;
            System.out.println(counter);
        }

        out.close();
        in.close();
        inputStream.close();
        success =true;
    } catch (Exception ex) {
        System.out.println("Error: " + ex.toString());
    }
    return true;
}
2012-04-05 14:54
by itro


0

I think your code is fine.
Maybe the task isn't taking long enough for the progress bar to be needed?

Here's a modified version of your code which reads from a local file and writes to another local file.
I have also added a delay to the write so that it gives the progress bar time to kick in. This works fine on my system with a sample 12MB PDF file, and shows the progress bar.
If you have a smaller file then just increase the sleep from 5 milliseconds to 100 or something - you would need to experiment.

And I didn't even know that the ProgressMonitorInputStream class existed, so I've learnt something myself ;].

/**
 * main
 */
public static void main(String[] args) {
    try {
        System.out.println("start");

        final String inf = "d:/testfile.pdf";
        final String outf = "d:/testfile.tmp.pdf";
        final FileOutputStream out = new FileOutputStream(outf) {
            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                super.write(b, off, len);
                try {
                    // We delay the write by a few millis to give the progress bar time to kick in
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        putFile(inf, out);

        System.out.println("end");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

private static boolean putFile(String m_sLocalFile, OutputStream out /*FtpClient m_client*/) {
    boolean success = false;
    int BUFFER_SIZE = 10240;
    if (m_sLocalFile.length() == 0) {
        System.out.println("Please enter file name");
    }
    byte[] buffer = new byte[BUFFER_SIZE];
    try {
        File f = new File(m_sLocalFile);
        int size = (int) f.length();
        System.out.println("File " + m_sLocalFile + ": " + size + " bytes");
        System.out.println(size);
        FileInputStream in = new FileInputStream(m_sLocalFile);
        //test
        InputStream inputStream = new BufferedInputStream(
                      new ProgressMonitorInputStream(null,"Uploading " + f.getName(),in));

        //test
        //OutputStream out = m_client.put(f.getName());

        int counter = 0;
        while (true) {
            int bytes = inputStream.read(buffer);  //in
            if (bytes < 0)
                break;
            out.write(buffer, 0, bytes);
            counter += bytes;
            System.out.println(counter);
        }

        out.close();
        in.close();
        inputStream.close();
        success =true;
    } catch (Exception ex) {
        System.out.println("Error: " + ex.toString());
    }
    return true;
}
2012-04-05 15:44
by davidfrancis
You are right, the task isn't taking long enough for the progress bar to be needed - itro 2012-04-06 12:01
Ads