long战941 long战941
关注数: 0 粉丝数: 112 发帖数: 868 关注贴吧数: 7
文件传输与进度条 问题,求大家帮忙看一看 我要做一个服务器端与客户端能相互发送信息和文件的程序,同时在传送和接收文件的同时能通过进度条显示文件传送的进度。我现在碰到的问题是当我发送端发送文件时,不能正确显示进度条而接收的那一端能正常显示进度,大家帮忙看看拿出了问题,先谢谢了~~ public class TestClinet extends JFrame implements Runnable, ActionListener,ChangeListener { 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); 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); pro.addChangeListener(this); 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(); for (long i = 0; i < file.length(); i++) { //System.out.println(i); dos.write(fis.read()); pro.setValue((int) (((i+1)/d)*100)); System.out.println((int)(((i+1)/d)*100)); } dos.flush(); Thread.sleep(5*1000); pro.setValue(0); }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++){ fos.write(dis.read()); System.out.println(i); //pro.setValue((int) (((i+1)/d)*100)); } Thread.sleep(5*1000); pro.setValue(0); } Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } @Override public void stateChanged(ChangeEvent e) { int value=pro.getValue(); if(value==0){ pro.setString(value+"%"); } if(value<100&&value>0){ pro.setString("已完成"+value+"%"); } if(value==100){ pro.setString("已完成!"); } } } 还有一个服务器端 代码基本一样
1 下一页