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

📄 clientsendfilethread.java~51~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~51~
字号:
package chat;
import java.net.*;
import java.io.*;
import java.util.*;
import java.io.RandomAccessFile;

/**
 *
 * <p>Title:发送文件线程类 </p>
 * <p>Description: 用于发送文件</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ClientSendFileThread extends Thread{

  File file=null;//要发送的文件
  String toPeople=null;//要发送的对象
  Socket socket=null;//发送文件套接
  DataInputStream dis;
  DataOutputStream dos;
  FileInputStream fis;
  RandomAccessFile raf;
  BufferedOutputStream bos;
  long readLength;
  static Hashtable saveThread=new Hashtable();
  FileCancelButton fileCancelButton;
  String key;
  String _message=null;
  /**
   *
   * @param socket Socket 发送文件套接
   */

  public ClientSendFileThread(Socket socket){
   // this.file=file;
    //this.toPeople=toPeople;
    this.socket=socket;
   // this.key=toPeople+file;
    //System.out.println(this.file+"  "+this.toPeople);
  }

  public void run(){
   // saveThread.put(key,this);
    //System.out.println(key+"clientsendfilethread");
    byte[] buffer=new byte[8291];//缓冲大小
    int readByte=0;
    try{
      dis=new DataInputStream(socket.getInputStream());
      dos=new DataOutputStream(socket.getOutputStream());
      //bos=new BufferedOutputStream(dos);
      //fis=new FileInputStream(file);
     // raf=new RandomAccessFile(file,"r");
     while (true) {//取得用户反馈信息
        _message = dis.readUTF();
        break;
      }
    }catch(IOException e){
    }
    if (_message.startsWith("AGREE_ACCEPT:")) {//同意接收
      this.toPeople = _message.substring(_message.indexOf(
          "AGREE_ACCEPT:") + 13, _message.indexOf("#TOSENDFILE#"));//发送对象
      this.file = new File(_message.substring(_message.indexOf(
          "#TOSENDFILE#") + 12));//发送文件对象
      this.key=toPeople+file;
      saveThread.put(key,this);
      try {
        //while((readByte=fis.read(buffer))!=-1){
        fis = new FileInputStream(file);
        raf = new RandomAccessFile(file, "r");
        fileCancelButton = (FileCancelButton) FileCancelButton.saveFileCancel.
            get(this.key);//保存取消按钮对象到hashtable中
        fileCancelButton.fileInfo.setText(Constant.getLang("GG_nx") + this.toPeople + Constant.getLang("GG_fsdwj") +
                                          this.file.getName() + Constant.getLang("GG_zzfsz"));//显示正在发送的提示信息
        fileCancelButton.cancelSendButton.setVisible(true);
        while ( (readByte = raf.read(buffer)) != -1) {//从文件中读入数据
          dos.write(buffer, 0, readByte);//写入套接流中
          this.readLength += readByte;
          //bos.write(buffer);
        }
        if (this.raf.length() == this.readLength) {//发送完毕
          fileCancelButton.fileInfo.setText(Constant.getLang("GG_nx") + this.toPeople + Constant.getLang("GG_fsdwj") +
                                            this.file.getName() + Constant.getLang("GG_fswb"));
          fileCancelButton.buttonJPanel.setVisible(false);
        }
        else {//对方取消发送
          fileCancelButton.fileInfo.setText(this.toPeople + Constant.getLang("GG_qxwj") +
                                            this.file.getName() + Constant.getLang("GG_dfs"));
        }
      }
      catch (IOException ioe) {
        try {
          this.dos.flush();
          this.dos.close();
          this.dis.close();
          this.raf.close();
          this.socket.close();
          this.saveThread.remove(this);
        }
        catch (Exception e) {
        }
      }
      finally {
        try {
          this.dos.flush();
          this.dos.close();
          this.dis.close();
          this.raf.close();
          this.socket.close();
        }
        catch (Exception e) {

        }
      }
    } else if(_message.startsWith("DISAGREE_ACCEPT:")){
      String _toPeople = _message.substring(_message.indexOf("DISAGREE_ACCEPT:") +
                                            16, _message.indexOf("#TOSENDFILE#"));
      String _toSendFile = _message.substring(_message.indexOf("#TOSENDFILE#") +
                                              12);
      File file = new File(_toSendFile);
      String key = _toPeople + _toSendFile;
      FileCancelButton fileCancelButton = (FileCancelButton) FileCancelButton.
          saveFileCancel.get(key);
      fileCancelButton.fileInfo.setText(_toPeople +Constant.getLang("GG_jjjswj") + file.getName() +
                                       Constant.getLang("GG_yzkh"));
      fileCancelButton.buttonJPanel.setVisible(false);
      //FileCancelButton.saveFileCancel.remove(key);
      //System.out.println(_message);
      try {
        this.dis.close();
        this.socket.close();
      }
      catch (Exception e) {
      }
    }

    }

  public static void main(String[] args) {
  }
}












⌨️ 快捷键说明

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