本帖最后由 周兴中 于 2012-6-28 00:17 编辑
各功能使用正常,就是CPU使用率偏高,请大侠指点,该如何优化.
由于代码冗长,分段成3段
以下为分段1:- package filetransdemo;
- import java.io.*;
- import java.net.*;
- import java.awt.*;
- import java.awt.event.*;
- *class PicClient implements Runnable //客户端
- {
- private Frame f;
- private TextArea infoTa;
- private TextField ipTa,fileTa;
- private Button fileBut,sendBut,enter;
- private FileDialog fileDia;
- private File file;
- private Label ipLab,fileLab,logLab,info;
- private Dialog infoDia;
- private DatagramSocket ds;
- private DatagramPacket dp;
- private List lst;
- private String ip;
- PicClient(Frame f,TextField ipTa,TextField fileTa,TextArea infoTa,List lst,String ip)
- {
- this.f = f;
- this.ipTa = ipTa;
- this.fileTa = fileTa;
- this.infoTa = infoTa;
- this.lst =lst;
- this.ip = ip;
- }
- public void run()
- {
- try
- {
- init();
- }
- catch (Exception e)
- {
- throw new RuntimeException("客户端初始化失败");
- }
- }
- public void init() //客户端初始化
- {
- try
- {
- infoDia = new Dialog(f,"提示信息",true);
- infoDia.setBounds(600,300,300,80);
- infoDia.setLayout(new FlowLayout());
- //f.setLayout(new GridBagLayout()); //网格包布局
- infoDia.setResizable(false);
- //fileTa = new TextField("",40);
- //infoTa = new TextArea(5,40);
- //ipTa = new TextField("",40);
- fileBut = new Button("浏览");
- sendBut = new Button("发送");
- enter = new Button("确定");
- ipLab = new Label("发送目的地(192.168.1.*):");
- fileLab = new Label("请选择一个文件:");
- logLab = new Label("日志");
- info = new Label("连接超时,可能是目标不在线");
- //infoTa = new TextArea(5,40);
- //ipTa = new TextField("",40);
- fileDia = new FileDialog(f,"打开",FileDialog.LOAD);
- infoDia.add(info);
- infoDia.add(enter);
- //f.add(fileTa);
- //f.add(fileBut);
- //f.add(sendBut);
- //f.add(infoTa);
- GridBagConstraints c=new GridBagConstraints();
- c.fill=GridBagConstraints.BOTH; //组
- c.insets=new Insets(5,5,5,5); //每个边上都有5个像素的边距
- c.gridx=0;c.gridy=0;c.gridwidth=1;c.gridheight=1;
- f.add(ipLab,c);
- c.gridx=0;c.gridy=1;c.gridwidth=GridBagConstraints.REMAINDER;c.gridheight=1;
- f.add(ipTa,c);
- c.gridx=0;c.gridy=2;c.gridwidth=1;c.gridheight=1;
- f.add(fileLab,c);
- c.gridx=0;c.gridy=3;c.gridwidth=1;c.gridheight=1;
- f.add(fileTa,c);
- c.gridx=1;c.gridy=3;c.gridwidth=1;c.gridheight=1;
- f.add(fileBut,c);
- c.gridx=2;c.gridy=3;c.gridwidth=1;c.gridheight=1;
- f.add(sendBut,c);
- c.gridx=0;c.gridy=4;c.gridwidth=1;c.gridheight=1;
- f.add(logLab,c);
- c.gridx=0;c.gridy=5;c.gridwidth=GridBagConstraints.REMAINDER;c.gridheight=1;
- f.add(infoTa,c);
- myEvent();
- /*ds = new DatagramSocket();
- byte[] infobuf = "收到请回答".getBytes();
- dp = new DatagramPacket(infobuf,infobuf.length,InetAddress.getByName("192.168.1.255"),10000); //向局域网发出广播,等待回应,以获取对方IP
- ds.send(dp);
- ds.close();
- DatagramSocket dsrece = new DatagramSocket(10000);
- byte[] buf = new byte[1024];
- DatagramPacket dprece = new DatagramPacket(buf,buf.length);
- dsrece.receive(dprece);
- ip = dprece.getAddress().getHostAddress();
- lst.add(ip);
- ipTa.setText(ip);*/
- //f.setVisible(true);
- }
- catch (Exception e)
- {
- throw new RuntimeException("客户端初始化失败");
- }
- }
- public void myEvent()
- {
- f.addWindowListener(new WindowAdapter()//关闭窗口
- {
- public void windowClosing(WindowEvent e)
- {
- f.setVisible(false);
- }
- });
- infoDia.addWindowListener(new WindowAdapter()//关闭提示信息
- {
- public void windowClosing(WindowEvent e)
- {
- infoDia.setVisible(false);
- }
- });
- enter.addActionListener(new ActionListener()//通过回车键关闭窗口
- {
- public void actionPerformed(ActionEvent e)
- {
- infoDia.setVisible(false);
- }
- });
- fileBut.addActionListener(new ActionListener()//文件浏览按键事件
- {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- fileDia.setVisible(true);
- String dirPath = fileDia.getDirectory(); //打开文件浏览对话框并获取文件目录
- String fileName = fileDia.getFile();//获取文件名
- if(dirPath==null || fileName==null)
- return ;
- fileTa.setText(dirPath+fileName);//将获得的文件名和路径显示在文本框中
- file = new File(dirPath,fileName);//创建文件对象.
- }
- catch (Exception ex2)
- {
- if(ex2.getStackTrace().toString().contains("NullPointerException"))//判断IP是否输入正确
- {
- info.setText("请输入一个正确的IP");
- infoDia.setVisible(true);
- }
- }
- }
- });
- sendBut.addActionListener(new ActionListener() //发送文件按键监听事件
- {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- /*if(ipTa.getText().equals(""))
- {
- info.setText("请输入一个正确的IP!");
- infoDia.setVisible(true);
- return;
- }*/
- ip = ipTa.getText();//获取文本框输入的IP
- //ipTa.setText(ip);
- Socket s = new Socket();//创建客户端套接
- InetSocketAddress addr = new InetSocketAddress(ip,10007);
- try
- {
- s.connect(addr,5000);//连接
- }
- catch (Exception ex1)
- {
- if(ex1.getMessage().contains("connect timed out")||ex1.getMessage().contains("Socket is not connected"))
- {
- infoDia.setVisible(true);
- }
- if(ex1.getStackTrace().toString().contains("NullPointerException"))
- {
- info.setText("请输入一个正确的IP");
- infoDia.setVisible(true);
- }
- }
- InputStream is = s.getInputStream(); //获取接收端输入流
- //byte[] bufip = new byte[256];
- //int num1 = is.read(bufip);
- //String infoIp = new String(bufip,0,num1);
- //System.out.println(infoIp);
- BufferedInputStream bufin =
- new BufferedInputStream(new FileInputStream(file));//创建文件读取字节流(带缓冲区)
- byte[] info = new byte[256];
- byte[] b = file.getName().getBytes();
- for(int i=0;i<b.length;i++)
- {
- info[i] = b[i];
- }
- ByteArrayInputStream bais = new ByteArrayInputStream(info); //字节数组流用于存储文件名
- SequenceInputStream sis = new SequenceInputStream(bais,bufin); //将文件名字节流和文件流合并
- BufferedOutputStream bufout =
- new BufferedOutputStream(s.getOutputStream());
- infoTa.append("文件传送中.....\r\n");
- int len = 0;
- while((len=sis.read())!=-1)
- {
- bufout.write(len);
- }
- bufout.flush();
- s.shutdownOutput();
- //InputStream is = s.getInputStream();
- byte[] buf1 = new byte[1023];
- int num = is.read(buf1);
- String info1 = new String(buf1,0,num);
- System.out.println(info1);//打印接收端的反馈信息
- infoTa.append(file.getName()+info1+"\r\n");//将信息加入到文本显示区域
- bufin.close();//关闭资源
- bufout.close();//关闭资源
- s.close();
- System.out.println("发送端已结束");
- }
- catch (Exception ex)
- {
- StackTraceElement[] info1 = ex.getStackTrace();
- if(ex.fillInStackTrace().toString().contains("NullPointerException"))
- {
- info.setText("请输入一个正确的IP");
- infoDia.setVisible(true);
- }
- System.out.println(ex.fillInStackTrace());
- System.out.println("----");
- if(ex.getMessage().contains("Socket is not connected"))
- {
- info.setText("无法连接此IP,请检查IP输入是否正确");
- infoDia.setVisible(true);
- }
- ///System.out.println(ex.getMessage());
- //ex.printStackTrace().toString();
- //throw new RuntimeException("111");
- }
- }
- });
- }
- }
复制代码 |
|