【问题标题】:how to add ProgressMonitorInputStream to ftp upload?如何将 ProgressMonitorInputStream 添加到 ftp 上传?
【发布时间】:2012-04-05 14:54:59
【问题描述】:

谁能看到这段代码有什么问题。它不会显示进度条,但会上传所有文件。 我也检查了 sun tutorial 和 swingworkers,但我还不能修复它。

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

【问题讨论】:

    标签: java ftp progress-bar inputstream fileinputstream


    【解决方案1】:

    我认为你的代码很好。
    也许任务花费的时间不够长,以至于需要进度条?

    这是您的代码的修改版本,它从本地文件读取并写入另一个本地文件。
    我还为写入添加了延迟,以便让进度条有时间启动。 这在我的系统上运行良好,带有一个 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;
    }
    

    【讨论】:

    • 你是对的,任务花费的时间不够长,以至于需要进度条?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多