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

📄 clientsendfilethread.java~46~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~46~
字号:

import java.net.*;
import java.io.*;
import java.util.*;
import java.io.RandomAccessFile;

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;

  public ClientSendFileThread(File file,String toPeople,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");
    }catch(IOException e){
      e.printStackTrace();
    }
    try{
    //while((readByte=fis.read(buffer))!=-1){
    fileCancelButton=(FileCancelButton)FileCancelButton.saveFileCancel.get(this.key);
    fileCancelButton.fileInfo.setText("您向"+this.toPeople+"发送的文件"+this.file+"正在传送中...");
    fileCancelButton.cancelSendButton.setVisible(true);
    while((readByte=raf.read(buffer))!=-1){
      dos.write(buffer,0,readByte);
      this.readLength+=readByte;
      //bos.write(buffer);
      }
      //this.dos.flush();
      //this.dos.close();
      //this.dis.close();
      //this.raf.close();
      //this.socket.close();
      if(this.file.length()==this.readLength){
        fileCancelButton.fileInfo.setText("您向"+this.toPeople+"发送的文件"+this.file+"发送完毕");
        fileCancelButton.buttonJPanel.setVisible(false);
      }else{
        fileCancelButton.fileInfo.setText(this.toPeople+"取消了文件"+this.file+"的发送");
      }

      System.out.println("读取完毕!");
    }catch(IOException ioe){
      try{
        fileCancelButton.fileInfo.setText(this.toPeople+"取消文件"+this.file.getName()+"的发送");
         fileCancelButton.buttonJPanel.setVisible(false);
        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){

      }
    }
    }
















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












⌨️ 快捷键说明

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