日期:2014-05-20  浏览次数:20701 次

进度条无法更新,只在完成的时候更新一次
我写了一个客户端和服务器端能相互发送信息和文件的一个程序,现在碰到的问题是发送端发送文件时不能正常更新进度条,只在最后更新一次,下面是客户端的代码,另外一个服务器的代码差不多。。。求帮忙看一下
Java code
package Fourth;
public class TestClinet extends JFrame implements Runnable, ActionListener{
    JTextField jts = new JTextField(15);
    JButton send = new JButton("send");
    JButton file=new JButton("file");
    JProgressBar pro = new JProgressBar(); 
    ScrollPane scp = new ScrollPane();
    JTextArea swmag = new JTextArea(10, 20);
    Socket sc = null;
    DataInputStream dis = null;
    DataOutputStream dos = null;

    TestClinet() {
        this.setLayout(new FlowLayout());
        this.add(send);
        send.setMnemonic(KeyEvent.VK_ENTER);
        this.add(jts);
        this.add(file);
        this.add(pro);
        scp.add(swmag);
        scp.setBounds(0, 0, 200, 200);
        this.add(scp);
        send.addActionListener(this);
        file.addActionListener(this);
        pro.setOrientation(JProgressBar.HORIZONTAL);
        pro.setMinimum(0);
        pro.setMaximum(100);
        pro.setValue(0);
        pro.setStringPainted(true);
        pro.setBackground(Color.red);
        pro.setForeground(Color.GREEN);
        try {
            sc = new Socket("localhost", 5000);
            dos = new DataOutputStream(sc.getOutputStream());
            dis = new DataInputStream(sc.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Thread th = new Thread(this);
        th.start();
    }

    public static void main(String[] args) {
        TestClinet mt = new TestClinet();
        mt.setTitle("客户端");
        mt.setVisible(true);
        mt.setBounds(200, 200, 300, 300);
        mt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == send) {
            String str = jts.getText();
            try {
                dos.writeUTF(str);// 客户端发送信息
                swmag.setText(swmag.getText() + "我:" + str+"  " +new Date().toLocaleString()+"\n");
                jts.setText("");
                dos.flush();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }// send按钮结束
        if(e.getSource()==file){
            FileDialog fd=new FileDialog(this,"客户端");
            fd.setVisible(true);
            String filepath=fd.getDirectory()+fd.getFile();
            File file=new File(filepath);
            System.out.println(file);
            FileInputStream fis=null;
            System.out.println(file.length());
            try{
            dos.writeUTF("start");
            dos.flush();
            dos.writeLong(file.length());
            dos.flush();
            fis = new FileInputStream(file);
            
            double d=(double) file.length();
            pro.invalidate();
            for (long i = 0; i < file.length(); i++) {
                int k = (int) (((i+1)/d)*100);
                pro.setValue(k);
                dos.write(fis.read());
                System.out.println("客户端发送"+k);
            }
            fis.close();
            dos.flush();
            }catch(Exception e1){
                e1.printStackTrace();
            }
        }
        
    }

    public void run() {
        while(true){
        try {
            String str = dis.readUTF();
            if(!("start".equals(str))){
                swmag.setText(swmag.getText() + "他:" + str + "  "
                        + new Date().toLocaleString() + "\n");
                }
                else{
                    long length=dis.readLong();
                    System.out.println(length);
                    double d=(double) length;
                    FileDialog fd=new FileDialog(this,"客户端");
                    fd.setVisible(true);
                    String filepath=fd.getDirectory()+fd.getFile();
                    if(filepath.equals("nullnull"))return;
                    File file=new File(filepath);
                    FileOutputStream fos=new FileOutputStream(file);
                    for(long i=0;i<length;i++){
                        int k=(int) (((i+1)/d)*100);
                        fos.write(dis.read());
                        System.out.println("客户端接收"+k);
                        pro.setValue(k);
                        pro.invalidate();
                    }
                    fos.close();
                }//if循环结束
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }
}