⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatclientframe.java~501~

📁 此软件为简单的实时通信软件,带文件传输与语音功能,类似于QQ
💻 JAVA~501~
📖 第 1 页 / 共 3 页
字号:
              ss.close();
               jTextArea1.append("文件传输完成!\n");
               setsp();
              jLabel6.setText("文件状态:没有文件传输");
              jButton11.setEnabled(false);
              jButton5.setEnabled(true);
             this.stop();
            }
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }

      }
  }

     class filesave{
       private Socket socket= null;
       DataOutputStream out = null;
       DataInputStream getMessageStream = null;
       RandomAccessFile fileOut;
       Thread a;
       private String ip;// 设置成服务器IP
       private String file;
       private int port = 8821;
       long filelen;

       public filesave(String file,String ip,long filelen) {
           this.file=file;
           this.ip=ip;
           this.filelen=filelen;
            a=new watch();
            a.start();
       }

       void stopsf(){
         try{
           a.stop();
           socket.close();
           fileOut.close();
          }
         catch (Exception e){}
       }

       class watch extends Thread{
         public void run(){
               try {
                   socket = new Socket(ip, port);
               if (socket != null) {
              jTextArea1.append("连接文件成功!" + "\n");
              setsp();
               sendMessage();
               getMessage();
               }

           } catch (Exception ex) {
               ex.printStackTrace();
               jTextArea1.append("连接文件失败!" + "\n");
               setsp();
           }
            }

           private void sendMessage() {
           if (socket == null)
               return;
           try {
               out = new DataOutputStream(socket.getOutputStream());
               if (socket != null) {
                   out.writeByte(0x1);
                   out.flush();
                   return;
               } else {
               }
           } catch (Exception e) {
           } finally {
           }
       }

           private void getMessage() {
           if (socket == null)
               return;
           DataInputStream inputStream = null;
           try {
               getMessageStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
               inputStream=getMessageStream;
           } catch (Exception e) {
                jTextArea1.append("接收消息缓存错误!\n");
                setsp();
               return;
           } finally {
           }

           try {
               //本地保存路径,文件名会自动从服务器端继承而来。
                 String savePath = file;
               int bufferSize = 8192;
               byte[] buf = new byte[bufferSize];
               long passedlen = filelen/bufferSize;
               long len=0;

               savePath += inputStream.readUTF();
               fileOut = new RandomAccessFile(savePath,"rw");
               len = inputStream.readLong();
               long lenl = len/bufferSize;

               jTextArea1.append("文件的长度为:" + len + "\n");
               jTextArea1.append("开始接收文件!" + "\n");
               setsp();

                fileOut.seek(filelen);
               while (true) {
                   int read = 0;
                   if (inputStream != null) {
                       read = inputStream.read(buf);
                   }
                   passedlen += 1;
                   if (read == -1) {
                       break;
                   }
                   //下面为进度条
                   jLabel6.setText("文件状态:文件接收了" +  (passedlen*100/ lenl) + "%");
                   fileOut.write(buf, 0, read);
               }
               jTextArea1.append("文件状态:接收完成,文件保存为" + savePath + "\n");
               setsp();
               jLabel6.setText("文件状态:没有文件传输");
               fileOut.close();
               jButton11.setEnabled(false);
               jButton5.setEnabled(true);
               this.stop();
           } catch (Exception e) {
                jTextArea1.append("接收错误!" + "\n");
                setsp();
               return;
           }
        }
       }
    }

}

class Capture implements Runnable {
       TargetDataLine line;
       Thread thread;
       Socket s;
       BufferedOutputStream captrueOutputStream;
       Capture(Socket s){//构造器 取得socket以获得网络输出流
         this.s=s;
       }
       public void start() {
           thread = new Thread(this);
           thread.setName("Capture");
           thread.start();
       }
       public void stop() {
           thread = null;
       }
       public void run() {
           try {
             captrueOutputStream=new BufferedOutputStream(s.getOutputStream());//建立输出流 此处可以加套压缩流用来压缩数据
           }
           catch (IOException ex) {
               return;
           }
           AudioFormat format =new AudioFormat(8000,16,2,true,true);
           DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);
           try {
               line = (TargetDataLine) AudioSystem.getLine(info);
               line.open(format, line.getBufferSize());
           } catch (Exception ex) {
               return;
           }
           byte[] data = new byte[1024];//此处的1024可以情况进行调整,应跟下面的1024应保持一致
           int numBytesRead=0;
           line.start();
           while (thread != null) {
               numBytesRead = line.read(data, 0,128);//取数据(1024)的大小直接关系到传输的速度,一般越小越快,
               try {
                 captrueOutputStream.write(data, 0, numBytesRead);//写入网络流
               }
               catch (Exception ex) {
                   break;
               }
           }
           line.stop();
           line.close();
           line = null;
           try {
               captrueOutputStream.flush();
               captrueOutputStream.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }

}

class Playback implements Runnable {
final int bufSize = 16384;
SourceDataLine line;
Thread thread;
ServerSocket serSockp;
Socket s;
Playback(int i){//构造器 取得socket以获得网络输入流
 try{
serSockp=new ServerSocket(i);
 }
catch(IOException e){
return;
}
}
public void start() {
thread = new Thread(this);
thread.setName("Playback");
thread.start();
}
public void stop() {
thread = null;
}
public void run() {
AudioFormat format =new AudioFormat(8000,16,2,true,true);
BufferedInputStream playbackInputStream;
try {
Socket ser=serSockp.accept();
this.s=ser;
playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,2147483647));//封装成音频输出流,如果网络流是经过压缩的需在此加套解压流
}
catch (IOException ex) {
return;
}
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufSize);
} catch (LineUnavailableException ex) {
return;
}
byte[] data = new byte[1024];//此处数组的大小跟实时性关系不大,可根据情况进行调整
int numBytesRead = 0;
line.start();
while (thread != null) {
try{
numBytesRead = playbackInputStream.read(data);
line.write(data, 0,numBytesRead);
} catch (IOException e) {
break;
}
}
if (thread != null) {
line.drain();
}
line.stop();
line.close();
line = null;
}
}

class chatClientFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton1_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class chatClientFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton2_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class chatClientFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton3_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class chatClientFrame_this_windowAdapter extends java.awt.event.WindowAdapter {
  chatClientFrame adaptee;

  chatClientFrame_this_windowAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void windowClosing(WindowEvent e) {
    adaptee.this_windowClosing(e);
  }
}

class chatClientFrame_jButton4_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton4_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton4_actionPerformed(e);
  }
}

class chatClientFrame_jButton6_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton6_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton6_actionPerformed(e);
  }
}

class chatClientFrame_jButton7_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton7_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton7_actionPerformed(e);
  }
}

class chatClientFrame_jButton8_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton8_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton8_actionPerformed(e);
  }
}

class chatClientFrame_jButton5_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton5_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton5_actionPerformed(e);
  }
}

class chatClientFrame_jButton9_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton9_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton9_actionPerformed(e);
  }
}

class chatClientFrame_jButton10_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton10_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton10_actionPerformed(e);
  }
}

class chatClientFrame_jButton11_actionAdapter implements java.awt.event.ActionListener {
  chatClientFrame adaptee;

  chatClientFrame_jButton11_actionAdapter(chatClientFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton11_actionPerformed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -