日期:2014-05-20 浏览次数:20737 次
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(); } } } }