简体   繁体   English

ProgressMonitorInputStream不会弹出

[英]ProgressMonitorInputStream doesn't pop up

This is a sever and its receiving a file from a client. 这是一个服务器,它从客户端接收文件。 It receives the complete file but the progress bar doesn't show up. 它收到完整的文件,但进度条不显示。 I am using ProgressInputStream. 我正在使用ProgressInputStream。 Here is the piece of code where I am using SwingWorker to create a new thread for progress bar. 这是我使用SwingWorker为进度条创建新线程的代码段。

SwingWorker<Void,Void> worker = new SwingWorker<Void,Void>()
        {
            protected Void doInBackground()
            {
                try {
                    ss = new ServerSocket(port);
                    s = ss.accept();
                    bytes = new byte[1024];
                    FileOutputStream fos = new FileOutputStream(new File(FileName));
                    DataInputStream dis = new DataInputStream(s.getInputStream());
                    pmis = new ProgressMonitorInputStream(frame,"Receiving",dis);
                    pmis.getProgressMonitor().setMillisToPopup(10);

                    while(pmis.read(bytes) > 0)
                    {
                        fos.write(bytes);
                    }


                    ss.close();
                    fos.close();




                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }
        };
        worker.execute();

Any help would be appreciated. 任何帮助,将不胜感激。 Sorry for my bad English. 对不起,我的英语不好。 Thanks in anticipation. 谢谢您的期待。

It only pops up 'if it's taking a while'. 它只会在“需要一段时间”时弹出。

NB: 注意:

  1. Your copy loop is wrong. 您的复制循环是错误的。 It should be: 它应该是:

     while ((count = pmis.read(bytes)) > 0) { fos.write(bytes, 0, count); } 

where count is obviously an int . 这里count显然是一个int

  1. You don't need the DataInputStream . 您不需要DataInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM