数周以来,我一直在尝试解决以下问题:( ..在我的Java小程序(MyApplet)中,我有一种方法称为btnSendToPC,该按钮应该通过打开的套接字将服务器中的选定文件内容发送到本地PC。当我尝试实现一个JProgressBar时会出现问题,该错误将向用户显示该文件的多少已下载到本地PC。我在该站点上阅读了许多JProgressBar示例,但老实说我还是想不通。您帮助了我,以下是我的代码的简短版本,其中包括所有主要部分。

//My Network class that opens a socket and reads bytes from the socket
public class TcpIp {

    protected Socket s = null;
    public DataInputStream dis = null;

    public TcpIp(InetAddress ipa, int port) {

        try {
            s = new Socket(ipa.getHostAddress(), port);
        } catch (IOException ex) {
            System.out.println("Error opening socket!");
            return;
        }

        try { //Create an input stream.
            dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
        } catch (Exception ex) {
            System.out.println("Error creating input stream!");
        }

    }

    public synchronized byte[] readBytes() throws IOException {

        ByteArrayOutputStream getBytes = new ByteArrayOutputStream();

        int oneByte;

        while ((oneByte = dis.read()) != 124) {//Reads 1 byte from the InputStream and breaks on | character
            getBytes.write(oneByte);
        }

        return (getBytes.toByteArray());

    }
}

//My main Applet class that has the method for Send to PC button
public class MyApplet extends javax.swing.JApplet implements Runnable {

    public TcpIp gtp = null;
    private static String inGet;

    @Override
    public void run() {

        int i;

        byte[] in = new byte[10000024];

        try {
            Thread.sleep(1000);
            //pause gtp.readBytes so that server has enough time to receive name of the file to read,
            //to read its contents and send its contents back through socket
        } catch (InterruptedException ex) {
        }

        if ((gtp != null)) {

            try {
                in = gtp.readBytes();
            } catch (IOException ex) {
            }

            //Remove non-printing bytes.
            for (i = 0; i < in.length; i++) {
                if (in[i] < 0x20) {
                    in[i] = 0x20;
                }
            }

            inGet = new String(in);
        }
    }

    public void btnSendToPC() {

        //In here are commands that send name of the file to read.
        //Server reads that file and sends its contents back through socket.

        try {

            FileOutputStream fout = new FileOutputStream(fn);
            BufferedWriter myOutput = new BufferedWriter(new OutputStreamWriter(fout));

            timer = new Thread(this);
            timer.start();

            try {
                timer.join();
            } catch (InterruptedException ex) {
            }

            myOutput.write(inGet)

            myOutput.close();
            fout.close();
        }

    }
}

最佳答案

只需使用javax.swing.ProgressMonitorInputStream。为您完成所有艰苦的工作。

关于java - Java-如何从套接字更新JProgressBar?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8525364/

10-11 03:47