如何将ProgressMonitor InputStream添加到ftp上传?

时间:2012-04-05 14:55:00

标签: java ftp progress-bar inputstream fileinputstream

任何人都可以看到此代码有什么问题。它没有显示进度条,但是上传所有文件。 我也检查了太阳教程和摇摆工作者,但我还没有解决它。

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;
}

1 个答案:

答案 0 :(得分:0)

我认为你的代码很好 也许这个任务花了足够长的时间才能需要进度条?

这是您的代码的修改版本,它从本地文件读取并写入另一个本地文件 我还为写入添加了一个延迟,以便它给进度条提供时间。 这可以在我的系统上使用12MB PDF文件样本正常工作,并显示进度条 如果你有一个较小的文件,那么只需将睡眠从5毫秒增加到100或者其他东西 - 你需要进行实验。

我甚至不知道ProgressMonitorInputStream类存在,所以我自己学到了一些东西;]。

/**
 * 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;
}