Java 如何将ProgressMonitorInputStream添加到ftp上载?

Java 如何将ProgressMonitorInputStream添加到ftp上载?,java,ftp,progress-bar,inputstream,fileinputstream,Java,Ftp,Progress Bar,Inputstream,Fileinputstream,有人能看出这个代码有什么问题吗。它不会显示进度条,但会上载所有文件。 我也检查了sun教程和swingworkers,但我还不能修复它 private static boolean putFile(String m_sLocalFile, FtpClient m_client) { boolean success = false; int BUFFER_SIZE = 10240; if (m_sLocalFile.length() == 0) { Syst

有人能看出这个代码有什么问题吗。它不会显示进度条,但会上载所有文件。 我也检查了sun教程和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;
}
private静态布尔putFile(字符串m_sLocalFile,FtpClient m_client){
布尔成功=假;
int BUFFER_SIZE=10240;
如果(m_sLocalFile.length()=0){
System.out.println(“请输入文件名”);
}
字节[]缓冲区=新字节[缓冲区大小];
试一试{
文件f=新文件(m_sLocalFile);
int size=(int)f.length();
System.out.println(“文件”+m_sLocalFile+”:“+size+”字节”);
系统输出打印项次(尺寸);
FileInputStream in=新的FileInputStream(m_sLocalFile);
//试验
InputStream InputStream=新的BufferedInputStream(
新的ProgressMonitorInputStream(null,“上载”+f.getName(),in));
//试验
OutputStream out=m_client.put(f.getName());
int计数器=0;
while(true){
int bytes=inputStream.read(缓冲区);//in
如果(字节<0)
打破
out.write(缓冲区,0,字节);
计数器+=字节;
系统输出打印项次(计数器);
}
out.close();
in.close();
inputStream.close();
成功=真实;
}捕获(例外情况除外){
System.out.println(“错误:+ex.toString());
}
返回true;
}

我认为您的代码很好。
也许任务没有花费足够长的时间来创建进度条

这是您的代码的修改版本,它从本地文件读取并写入另一个本地文件。
我还为写入添加了一个延迟,以便让进度条有时间启动。 这在我的系统上运行良好,带有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;
}
/**
*主要
*/
公共静态void main(字符串[]args){
试一试{
系统输出打印项次(“开始”);
最终字符串inf=“d:/testfile.pdf”;
最终字符串outp=“d:/testfile.tmp.pdf”;
final FileOutputStream out=新FileOutputStream(outp){
@凌驾
公共无效写入(字节[]b,int off,int len)引发IOException{
超级。注销(b、off、len);
试一试{
//我们将写操作延迟了几毫秒,以便让进度条有时间启动
睡眠(5);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
};
putFile(inf,out);
系统输出打印项次(“结束”);
}catch(filenotfounde异常){
e、 printStackTrace();
}
}
私有静态布尔putFile(字符串m_sLocalFile,OutputStream out/*FtpClient m_client*/){
布尔成功=假;
int BUFFER_SIZE=10240;
如果(m_sLocalFile.length()=0){
System.out.println(“请输入文件名”);
}
字节[]缓冲区=新字节[缓冲区大小];
试一试{
文件f=新文件(m_sLocalFile);
int size=(int)f.length();
System.out.println(“文件”+m_sLocalFile+”:“+size+”字节”);
系统输出打印项次(尺寸);
FileInputStream in=新的FileInputStream(m_sLocalFile);
//试验
InputStream InputStream=新的BufferedInputStream(
新的ProgressMonitorInputStream(null,“上载”+f.getName(),in));
//试验
//OutputStream out=m_client.put(f.getName());
int计数器=0;
while(true){
int bytes=inputStream.read(缓冲区);//in
如果(字节<0)
打破
out.write(缓冲区,0,字节);
计数器+=字节;
系统输出打印项次(计数器);
}
out.close();
in.close();
inputStream.close();
成功=真实;
}捕获(例外情况除外){
System.out.println(“错误:+ex.toString());
}
返回true;
}

你说得对,任务没有花足够长的时间来显示进度条?