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

📄 clientacceptfile.java~59~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~59~
字号:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/**
 *
 * <p>Title:用户接收文件线程 </p>
 * <p>Description: 用户从套接中取得流并且保存到相应的文件中</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ClientAcceptFile implements Runnable {
  Constant constant;
  Socket acceptFileSocket=null;//接收该文件的套接
  DataInputStream dis=null;//接收该文件的输入流
  DataOutputStream dos=null;//接收该文件的输出流
  RandomAccessFile raf=null;//保存文件的randomaccessfile对象
  FileOutputStream fos;
  File file=null;//新的保存文件对象
  String key=null;
  String ip=null;
  String action=null;
  String oldFileName=null;//旧的文件对象(从套接中取得)
  String requestPeople;
  String userName;
  long readLength;
  long fileLength;
  FileDisposeButton fileDisposeButton;
  static  Hashtable saveThread=new Hashtable();
  /**
   *
   * @param requestPeople String 请求发送文件的对象
   * @param file File 新选择的要保存的文件对象
   * @param ip String 请求者的IP地址
   * @param port int 请求者的端口
   * @param action String 动作标识
   * @param oldFileName String 从请求者中取得的文件对象名
   * @param fileLength String 文件大小
   */
  public   ClientAcceptFile(String requestPeople,File file,
                            String ip,int port,String action,String oldFileName,String fileLength)  {
    constant=new Constant();
    this.file=file;
    this.ip=ip.substring(1);
    this.requestPeople=requestPeople;
    this.action=action;
    this.oldFileName=oldFileName;
    this.fileLength=(Long.valueOf(fileLength)).longValue();
    userName=ChatArea.loginDialog.getName();
    //System.out.println(this.fileLength);
    key=new String(requestPeople+this.oldFileName);
    saveThread.put(key,this);
    try{
      acceptFileSocket = new Socket(this.ip, port);
    }catch(IOException e){
      e.printStackTrace();
    }

  }
  public void run() {//接收文件线程
    byte[] buffer = new byte[8291];//缓冲大小
    int readBuffer = 0;
    saveThread.put(key, this);//保存当前线程到 hashtable中
    //System.out.println(key+"接受文件线程");
    try {
      dis = new DataInputStream(acceptFileSocket.getInputStream());
      dos = new DataOutputStream(acceptFileSocket.getOutputStream());
      raf = new RandomAccessFile(this.file, "rw");
      //fos=new FileOutputStream(this.file);
      if (action.equals("AGREE")) {//保存和另存为事件
        dos.writeUTF("AGREE_ACCEPT:" + this.userName + "#TOSENDFILE#" +
                     this.oldFileName);//发送同意接收的通知给发送方
        fileDisposeButton = (FileDisposeButton) FileDisposeButton.
            saveFileDisposeButton.get(this.requestPeople + this.oldFileName);//取得该对象相对应的按钮处理对象

        fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_xnfsdfj") +
                                           this.file.getName() +constant.getLang("GG_zzfsz"));//设置相应的提示信息
        while ( (readBuffer = dis.read(buffer)) != -1) {//从流中取得数据
          raf.write(buffer, 0, readBuffer);//写入文件中
          readLength += readBuffer;
        }
        fileDisposeButton.buttonJPanel.setVisible(false);//文件保存完毕,按钮对象为不可见
        if (this.raf.length() == this.readLength) {//写入的文件大小和原文件大小一样
          fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_xnfsdfj") +
                                             this.file.getName() + constant.getLang("GG_fswb")+constant.getLang("GG_wjbcz")+this.file.getAbsolutePath());//显示发送完毕的提示信息
        }else {
          fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_qxwj")+
                                             this.file.getName() + constant.getLang("GG_dfs"));
        }
      }
      else if (action.equals("DISAGREE")) {
        dos.writeUTF("DISAGREE_ACCEPT:" + this.userName + "#TOSENDFILE#" +
                     this.oldFileName);
      }
    }
    catch (IOException ioe) {
      try {
        fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_xnfsdfj") +
                                           this.file.getName() + constant.getLang("GG_yywlgzezz"));
        this.dis.close();
        this.dos.flush();
        this.dos.close();
        this.raf.close();
        this.acceptFileSocket.close();
        saveThread.remove(this);
      }
      catch (Exception e) {

      }
    }
    finally {
      try {
        this.dis.close();
        this.dos.flush();
        this.dos.close();
        this.raf.close();
        this.acceptFileSocket.close();
        saveThread.remove(this);
        //System.out.println("jieshowenjianexcpeiont");
      }
      catch (IOException e) {
        //e.printStackTrace();

      }

    }
  }


  public static void main(String[] args) {
   // File f=new File()
   //Thread thread=new Thread(new ClientAcceptFile("","","",0));
  }
}










⌨️ 快捷键说明

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