简体   繁体   中英

ProgressMonitor and SwingWorker aren't working

I've got a problem with a SwingWorker. The application sends a file between client and server but the progressmonitor will not be shown me progress during transmission. Could you tell me what i'm doing wrong and what should i do?

package main;

import java.awt.EventQueue;

public class Main {

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                Test2 test2=new Test2();

            }
        });

    }

}

package main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Test2 {

    public Test2() {

        hostName="localhost";

        try {
            clientSocket = new Socket(hostName, 1234);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        File file=new File("E:/test/123.mp4");

        try {
            fileInputStream=new FileInputStream(file);
            bis=new BufferedInputStream(fileInputStream);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            bos=new BufferedOutputStream(clientSocket.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        bit=new byte[512];
        int len;

        System.out.println("Send..."); //test

        try {
            while ((len = bis.read(bit,0,511)) != -1) {
                bos.write(bit, 0, len);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       

        try {
            bis.close();
            bos.close();
            fileInputStream.close();
            //fileOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

        System.out.println("Finish"); //test

    }

    private String hostName;
    private Socket clientSocket;

    private BufferedInputStream bis;
    private BufferedOutputStream bos;
    private FileInputStream fileInputStream;
    private byte bit[];
}

package main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

public class Test2 {

    public Test2() {

        pm=new ProgressMonitor(null, "Download...", null, 0, 1850297);
        pm.setMillisToDecideToPopup(1);

        test4=new Test4();
        test4.execute();                            

    }

    private class Test4 extends SwingWorker<Boolean, Void> {

        public Test4() {

            try {
                welcomeSocket = new ServerSocket(1234);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Server works");

            try {
                connectionSocket = welcomeSocket.accept();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        protected Boolean doInBackground() throws Exception {

            try {

                bis=new BufferedInputStream(connectionSocket.getInputStream());

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                fileOutputStream=new FileOutputStream("E:/test2/123.mp4");
                bos=new BufferedOutputStream(fileOutputStream);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                                           

            bit=new byte[512];
            int len;

            System.out.println("Download..."); //test

            try {
                while ((len = bis.read(bit,0,511)) != -1) {

                    bos.write(bit, 0, len);

                    publish();

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

            try {
                bis.close();
                bos.close();
                fileOutputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           

        return true;

        }

        @Override
        protected void process(List<Void> chunks) {

            number++;
            pm.setProgress(number);

        }

        @Override
        protected void done() {
            System.out.println("DONE");
        }

    }

    private ServerSocket welcomeSocket; 
    private Socket connectionSocket;

    private FileOutputStream fileOutputStream;
    private BufferedInputStream bis;
    private BufferedOutputStream bos;
    private byte bit[];

    private ProgressMonitor pm;
    private Test4 test4;
    private int number;

}

Apart from SwingWorker, I've got a problem with a code below. This is second version my server's application without a SwingWorker. Here, the progressmonitor is shown me progress during transmission, but not ever. I used invokeLater in run() method but sometimes the progressmonitor isn't work. Could you tell me what i'm doing wrong?

package main;

import java.awt.EventQueue;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

public class Test2 {

    public Test2() {

        pm=new ProgressMonitor(null, "Download...", null, 0, 1850297);
        pm.setMillisToDecideToPopup(1);

        test3=new Test3();
        new Thread(test3).start();                          

    }

    private class Test3 implements Runnable {

        public Test3() {        

        }

        @Override
        public void run() {

            try {
                welcomeSocket = new ServerSocket(1234);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Server works"); //test

            while(true){

                try {
                    connectionSocket = welcomeSocket.accept();

                    try {

                        bis=new BufferedInputStream(connectionSocket.getInputStream());

                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    try {
                        fileOutputStream=new FileOutputStream("E:/test2/123.mp4");
                        bos=new BufferedOutputStream(fileOutputStream);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }                                   

                    bit=new byte[512];
                    int len;

                    System.out.println("Download..."); //test

                    try {
                        while ((len = bis.read(bit,0,511)) != -1) {

                            bos.write(bit, 0, len);

                            EventQueue.invokeLater(new Runnable() {

                                @Override
                                public void run() {
                                    number++;
                                    pm.setProgress(number);

                                }
                            });

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

                    try {
                        bis.close();
                        bos.close();
                        fileOutputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    System.out.println("DONE"); //test

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

            }

        }

    }

    private ServerSocket welcomeSocket; 
    private Socket connectionSocket;

    private FileOutputStream fileOutputStream;
    private BufferedInputStream bis;
    private BufferedOutputStream bos;
    private byte bit[];

    private ProgressMonitor pm;
    private Test3 test3;
    private int number;

}

You're waiting on the socket connection and accepting it in the Test4 constructor which will be called on the Swing event thread . It's OK to create the socket there, but don't wait for the connection there as this blocks. Instead wait for the connection it in the doInBackground method.

Also consider changing your SwingWorker so it passes the bytes read:

// change generic parameter to Integer
private class Test4 extends SwingWorker<Boolean, Integer> {


    @Override
    protected Boolean doInBackground() throws Exception {

        // .....                                          

        bit=new byte[512];
        int len;

        try {
            while ((len = bis.read(bit,0,511)) != -1) {

                bos.write(bit, 0, len);

                publish(len);

            }

and elsewhere:

    @Override
    protected void process(List<Integer> chunks) {
        // number++;
        for (Integer chunk : chunks) {
           pm.setProgress(chunk);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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