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

📄 p2pconversation.java

📁 一个仿qq的程序源码 一个用纯java开发的
💻 JAVA
字号:
package qianqian.p2pchat.control;

import qianqian.p2pchat.constant.Const;
import qianqian.p2pchat.filetrans.RecvfileThread;
import qianqian.p2pchat.filetrans.SendfileThread;
import qianqian.p2pchat.gui.P2PConvsForm;
import qianqian.p2pchat.message.P2PMessage;
import qianqian.p2pchat.tools.Tools;

public class P2PConversation {
	private P2PConvsForm p2pRoom = null;
	private boolean shutup = false;
	private boolean fileSending = false;
	private boolean fileRecving = false;
	private Peer to = null;
	private String userIp;
	private RecvfileThread rThread;
	private SendfileThread sThread;
	private java.io.File fileToSend;
	private java.io.File fileToRecv;
	private java.net.Socket socket;

	public P2PConversation(Peer iTo) {
		to = iTo;
		p2pRoom = new P2PConvsForm(this);
	}

	public Peer getTo() {
		return to;
	}

	public void setShutUp(boolean shut) {
		shutup = shut;
	}

	public boolean isShutUp() {
		return shutup;
	}

	public void show() {
		p2pRoom.setVisible(true);
		p2pRoom.toFront();
	}

	public void setRecvThread(RecvfileThread rThread) {
		this.rThread = rThread;
	}

	public void startRecv() {
		if (rThread != null) {
			rThread.start();
		}
	}

	public SendfileThread getSendThread() {
		return sThread;
	}

	public RecvfileThread getRecvThread() {
		return rThread;
	}

	public void setUserIp(String ip) {
		userIp = ip;
	}

	public String getToAddr() {
		return userIp;
	}

	public void setSendFile(java.io.File fileToSend) {
		this.fileToSend = fileToSend;
	}

	public void setRecvFile(java.io.File fileToRecv) {
		this.fileToRecv = fileToRecv;
	}

	public java.io.File getSendFile() {
		return fileToSend;
	}

	public java.io.File getRecvFile() {
		return fileToRecv;
	}

	public void setSocket(java.net.Socket socket) {
		this.socket = socket;
	}

	public java.net.Socket getSocket() {
		return socket;
	}

	public void setRecving(boolean fileRecving) {
		this.fileRecving = fileRecving;
	}

	public void setSending(boolean fileSending) {
		this.fileSending = fileSending;
	}

	public boolean isRecving() {
		return fileRecving;
	}

	public boolean isSending() {
		return fileSending;
	}

	public void transOver() {
		sendMsg(Const.CODE_FILE_OVER, fileToRecv.getName());
		resetFileRecver();
		p2pRoom.setTitle(getTo().getName());
	}

	public void setProgress(long size) {
		p2pRoom.setTitle("完成[" + (size / 1024) + "]KB");
	}

	public void resetFileRecver() {
		setRecving(false);
		if (getRecvThread() != null) {
			getRecvThread().interrupt();
			setRecvThread(null);
		}
		if (getSocket() != null) {
			closeRecvSocket();
			setSocket(null);
		}
		setRecvFile(null);
	}

	public void resetFileSender() {
		setSendFile(null);
		setSending(false);
	}

	public void closeRecvSocket() {
		try {
			socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void msgArrival(P2PMessage iMsg, String ip) {
		char code = iMsg.getStyle();
		String content = iMsg.getMsgContent();
		switch (code) {
		case Const.CODE_TEXT_MSG:
			p2pRoom.addRecvLine(content.substring(0, content.indexOf('\n')),
					P2PConvsForm.SYS_STYLE);
			p2pRoom.addRecvLine(content.substring(content.indexOf('\n')),
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_SEND:
			java.io.File fileToRecv = new java.io.File(content);
			setUserIp(ip);
			setRecving(true);
			setRecvFile(fileToRecv);
			p2pRoom.addRecvLine("文件消息:是否接收文件?\n文件名:" + fileToRecv.getName()
					+ "\n\n", P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_RECV:
			p2pRoom.addRecvLine("文件消息:对方同意接收文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_CANS:
			p2pRoom.addRecvLine("文件消息:对方取消发送文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			resetFileRecver();
			return;
		case Const.CODE_FILE_CANR:
			p2pRoom.addRecvLine("文件消息:对方取消接收文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			resetFileSender();
			return;
		case Const.CODE_FILE_OVER:
			p2pRoom.addRecvLine("文件消息:文件发送完毕!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			resetFileSender();
			return;
		case Const.CODE_FACE_MSG:
			p2pRoom.addRecvFace(content, iMsg.getFaces());
			return;
		case Const.CODE_IMAGE_MSG:
			imgArrival(iMsg.getMsgContent(), iMsg.getImage(), iMsg.getImgType());
			return;
		}
	}

	public void imgArrival(String content, byte[] image, String imgType) {
		String fileName = "image/" + System.currentTimeMillis() + imgType;
		try {
			java.io.FileOutputStream fos = new java.io.FileOutputStream(
					fileName);
			fos.write(image);
			fos.close();
		} catch (Exception e) {
		}
		p2pRoom.addRecvImage(content, fileName);
	}

	public void sendMsg(char code, String iMsg) {
		String info = Controller.getInstance().getMe().getName() + "\t"
				+ Tools.getTimeInfo();
		switch (code) {
		case Const.CODE_TEXT_MSG:
			Controller.getInstance().sendP2PMsg(to, code,
					info + "\n" + iMsg + "\n\n");
			p2pRoom.addRecvLine(info + "\n", P2PConvsForm.SYS_STYLE);
			p2pRoom.addRecvLine(iMsg + "\n\n", P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_SEND:
			Controller.getInstance().sendP2PMsg(to, code, iMsg);
			p2pRoom.addRecvLine("文件消息:等待对方确认接收文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_RECV:
			Controller.getInstance().sendP2PMsg(to, code, iMsg);
			p2pRoom.addRecvLine("文件消息:您已经同意接收文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_CANS:
			Controller.getInstance().sendP2PMsg(to, code, iMsg);
			p2pRoom.addRecvLine("文件消息:您已经取消发送文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_CANR:
			Controller.getInstance().sendP2PMsg(to, code, iMsg);
			p2pRoom.addRecvLine("文件消息:您已经取消接收文件!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_FILE_OVER:
			Controller.getInstance().sendP2PMsg(to, code, iMsg);
			p2pRoom.addRecvLine("文件消息:文件接收完毕!\n文件名:" + iMsg + "\n\n",
					P2PConvsForm.SET_STYLE);
			return;
		case Const.CODE_IMAGE_MSG:
			byte[] imgByte = new byte[Const.RecvBufSize];
			String imgType = iMsg.substring(iMsg.lastIndexOf('.'));
			int len = 0;
			try {
				java.io.FileInputStream fis = new java.io.FileInputStream(iMsg);
				len = fis.read(imgByte);
				fis.close();
			} catch (Exception e) {
			}
			p2pRoom.addRecvImage(info, iMsg);
			Controller.getInstance().sendImage(to, code, imgByte, len, imgType,
					info);
			return;
		case Const.CODE_FACE_MSG:
			String face = iMsg.substring(iMsg.indexOf('-') + 1);
			p2pRoom.addRecvFace(info, face);
			Controller.getInstance().sendFace(to, code, info, face);
			return;
		}
	}

	public void exit() {
		p2pRoom.dispose();
	}
}

⌨️ 快捷键说明

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