ServerThreadst=(ServerThread)threads.elementAt(i);
st.write("<"+getID()+">:
"+receive);
}
}
//发送指定消息
else{
inttemp=receive.indexOf("<");
inttemp1=receive.indexOf(">");
//获得接收者ID号
StringtempS=receive.substring(temp+1,temp1);
inti=Integer.parseInt(tempS);
//指定接收者输出
ServerThreadst=(ServerThread)threads.elementAt(i);
st.write("<"+getID()+">:
"+receive);
//发送者自己也输出
st=(ServerThread)threads.elementAt(getID());
st.write("<"+getID()+">:
"+receive);
}
}
else{
ServerThreadst=(ServerThread)threads.elementAt(getID());
st.write("***"+receive+"***");
}
}
}catch(Exceptionex){
threads.removeElement(this);
ex.printStackTrace();
}
try{
socket.close();
}catch(Exceptionex){
ex.printStackTrace();
}
}
publicvoidwrite(Stringmsg){
synchronized(out){
try{
out.writeUTF(msg);
}catch(Exceptionex){
}
}
}
publicintgetID(){
returnthis.nid;
}
publicvoidsetID(intnid){
this.nid=nid;
}
}
服务器端的文件传送:
packagechat;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.PrintStream;
import.ServerSocket;
import.Socket;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjavax.swing.JOptionPane;
publicclassfilesendServer{
publicvoidfilereceive()throwsException{
//1.创建ServerSocket,在循环中接收客户端请求,每接收到一个请求就开启新线程处理
ServerSocketserverSocket=newServerSocket(1234);
JOptionPane.showMessageDialog(null,"服务已启动,绑定1234端口!
");
while(true){
Socketsocket=serverSocket.accept();
newfileServerThread(socket).start();
}
}
}
classfileServerThreadextendsThread{
Socketsocket;
publicfileServerThread(Socketsocket){
this.socket=socket;
}
publicvoidrun(){
FileOutputStreamfos=null;
try{
//3.获取输入输出流
BufferedReaderbr=newBufferedReader(newInputStreamReader(socket.getInputStream()));
PrintStreamps=newPrintStream(socket.getOutputStream());
//7.接收文件,查找是否存在.如果存在,给写一个消息给客户端,服务端线程结束.如果不存在,写消息给客户端,准备开始上传.
StringfileName=br.readLine();
longfileLen=Long.parseLong(br.readLine());
Filedir=newFile("upload");
dir.mkdir();
Filefile=newFile(dir,fileName);
if(file.exists()&&file.length()==fileLen){//文件已存在,length()即为文件大小,文件不存在length()为0
ps.println("已存在");
return;
}
else{
ps.println(file.length());//文件已存在,length()即为文件大小,文件不存在length()为0
}
//10.从Socket的输入流中读取数据,创建FileOutputStream写出到文件中
Stringtime=newSimpleDateFormat("yyyy-MM-ddHH:
mm:
ss").format(newDate());
System.out.println(time+""+(file.exists()?
"开始断点续传:
":
"开始上传文件:
")+file.getName());
longstart=System.currentTimeMillis();
InputStreamin=socket.getInputStream();
fos=newFileOutputStream(file,true);//文件存在就追加,文件不存在则创建
byte[]buffer=newbyte[1024];
intlen;
while((len=in.read(buffer))!
=-1){//这个地方会堵塞,如果客服端没有关闭socket.服务器端读不到流末尾(-1)
fos.write(buffer,0,len);
if(file.length()==fileLen)//文件大小等于客户端文件大小时,代表上传完毕
break;
}
fos.close();
longend=System.currentTimeMillis();
time=newSimpleDateFormat("yyyy-MM-ddHH:
mm:
ss").format(newDate());
System.out.println(time+""+"上传文件结束:
"+file.getName()+",耗时:
"+(end-start)+"毫秒");
ps.println("上传成功");
socket.close();
}catch(IOExceptione){
if(fos!
=null)
try{
fos.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}
客户端的界面设计:
packagechat;
importjava.awt.*;
importjava.io.*;
import.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassClient
{
publicstaticvoidmain(Stringargs[])
{
newChatClient();
}
}
classChatClientextendsFrameimplementsActionListener,Runnable{
privatestaticfinallongserialVersionUID=-4149839042245330513L;
publicButtonlisten,connection,sendtoall,sendtoone,exit,filesend;
publicJComboBoxemote;
publicTextFieldinputName,inputContent;
publicTextAreachatResult;
publicSocketsocket=null;
publicDataInputStreamin=null;
publicDataOutputStreamout=null;
publicThreadthread;
ChatClient()
{
socket=newSocket();
Boxbox1=Box.createHorizontalBox();
listen=newButton("作为服务器监听");
connection=newButton("连接服务器");
filesend=newButton("发送文件");
exit=newButton("退出");
sendtoall=newButton("群聊");
sendtoone=newButton("私聊");
listen.setEnabled(false);
filesend.setEnabled(false);
sendtoall.setEnabled(false);
sendtoone.setEnabled(false);
inputName=newTextField(6);
inputName.setBackground(newColor(162,231,250));
inputContent=newTextField(22);
inputContent.setBackground(newColor(162,231,250));
chatResult=newTextArea("",17,20,TextArea.SCROLLBARS_VERTICAL_ONLY);
chatResult.setBackground(newColor(162,231,250));
JLabeljlname=newJLabel("输入昵称");
box1.add(jlname);
box1.add(inputName);
box1.add(listen);
box1.add(connection);
box1.add(filesend);
box1.add(exit);
Boxbox2=Box.createHorizontalBox();
emote=newJComboBox();
emote.setModel(newDefaultComboBoxModel(newString[]{"表情","微笑",
"甜笑","惊喜","嘻嘻","扮酷","嘿嘿","傻笑","好奇","媚眼","鬼脸","陶醉",
"害羞","生气","嚷嚷","发怒","伤心","高明","菜鸟","问号","狂笑","大哭",
"示爱","呻吟","想想"}));
emote.setE