黑马程序员技术交流社区

标题: java文件传输 [打印本页]

作者: 庭院深深深几许    时间: 2019-4-17 13:45
标题: java文件传输
          文件接收部分
  FileReceiver.java
  packagecom.vista.Util;
  importjava.io.IOException;
  importjava.net.ServerSocket;
  publicclassFileReceiverextendsThread
  {
  privateServerSockets=null;
  privateintFILE_PORT;//文件传输的监听端口
  privateUseruser=null;
  publicFileReceiver(Userus)
  {
  this.FILE_PORT=us.getUdpPort();//设置文件端口
  this.user=us;
  }
  publicvoidrun()
  {
  try
  {
  s=newServerSocket(FILE_PORT);
  System.out.println("用户端开始监听:"+s);
  while(true)
  {
  java.net.Socketsocket=s.accept();
  System.out.println("连接接受"+socket);
  newFileReceiverThread(user,socket);
  }
  }
  catch(IOExceptione)
  {
  e.printStackTrace();
  }
  catch(java.lang.Exceptionex)
  {
  System.err.println(ex.getMessage().toString());
  ex.printStackTrace();
  }
  finally
  {
  try
  {
  s.close();
  }
  catch(IOExceptione)
  {
  //TODOAuto-generatedcatchblock
  e.printStackTrace();
  }
  }
  }
  }
  FileReceiverThread.java
  packagecom.vista.Util;
  importjava.io.BufferedWriter;
  importjava.io.File;
  importjava.io.FileOutputStream;
  importjava.io.IOException;
  importjava.io.OutputStreamWriter;
  importjava.io.PrintWriter;
  importjava.net.Socket;
  importjava.util.Date;
  importcom.vista.Form.SingleChatForm;
  publicclassFileReceiverThreadextendsThread
  {
  privateSingleChatFormchatForm=null;
  privatejava.net.Socketsocket=null;
  privatejava.io.InputStreamin=null;//从客户端读数据的
  privatejava.io.PrintWriterout=null;//向客户端写数据
  privateStringserverMsg;
  privateUsersender=null;
  privateUserreceiver=null;
  privatecom.vista.Util.SenderMsgSender=null;
  publicFileReceiverThread(Userus,Socketsocket)
  {
  this.receiver=us;
  try
  {
  this.socket=socket;
  this.in=this.socket.getInputStream();
  this.out=newPrintWriter(newBufferedWriter(newOutputStreamWriter(this.socket.getOutputStream())),true);
  }
  catch(IOExceptione)
  {//TODOAuto-generatedcatchblock
  e.printStackTrace();
  }
  this.start();//启动线程
  }
  publicvoidrun()
  {
  Stringinfo="";
  try
  {
  while(true)
  {
  //第一个参数为命令
  Datebefor=newDate();
  bytecmd[]=newbyte[128];
  intb=0;
  while(b<cmd.length) <="" cmd.length)
  {
  b+=in.read(cmd,b,cmd.length-b);
  }
  intends=0;
  for(inti=0;i<cmd.length;i++) <="" cmd.length;i++)
  {
  if(cmd==-0)
  {
  ends=i;
  break;
  }
  }
  Stringcmds=newString(cmd,0,ends);
  if("cp".equals(cmds))
  {
  byte[]filename=newbyte[256];
  b=0;
  while(b<cmd.length) <="" cmd.length)
  {
  b+=in.read(filename,b,filename.length-b);
  }
  ends=0;
  for(inti=0;i<cmd.length;i++) <="" cmd.length;i++)
  {
  if(filename==-0)
  {
  ends=i;
  break;
  }
  }
  Stringfilenames=newString(filename,0,ends);
  filenames="F://"+filenames;
  Filefileout=newFile(filenames);
  FileOutputStreamfos=newFileOutputStream(fileout);
  byte[]filesize=newbyte[64];
  b=0;
  while(b<filesize.length) <="" filesize.length)
  {
  b+=in.read(filesize,b,filesize.length-b);
  }
  ends=0;
  for(inti=0;i<filesize.length;i++) <="" filesize.length;i++)
  {
  if(filesize==-0)
  {
  ends=i;
  break;
  }
  }
  Stringfilesizes=newString(filesize,0,ends);
  System.out.println("filesize:"+filesizes);
  intta=Integer.parseInt(filesizes);
  byte[]buf=newbyte[1024*10];
  while(true)
  {
  if(ta==0)
  {
  break;
  }
  intlen=ta;
  if(len>buf.length)
  {
  len=buf.length;
  }
  intrlen=in.read(buf,0,len);
  ta-=rlen;
  if(rlen>0)
  {
  fos.write(buf,0,rlen);
  fos.flush();
  }
  else
  {
  break;
  }
  }
  Dateafter=newDate();
  longdiff=after.getTime()-befor.getTime();
  diff/=1000;
  info="传输完成!文件已经保存到:"+filenames+",文件大小:"+filesizes+"字节,"+"共用时间:"+diff+"秒";
  intresult=javax.swing.JOptionPane.showConfirmDialog(null,info,"文件传输",javax.swing.JOptionPane.YES_NO_OPTION,javax.swing.JOptionPane.INFORMATION_MESSAGE);
  fos.close();
  break;
  }
  }
  //socket.close();
  }
  catch(Exceptione)
  {
  e.printStackTrace();
  }
  try
  {
  this.socket.close();
  }
  catch(IOExceptionex1)
  {
  }
  }
  }
  文件发送线程
  FileSender.java
  packagecom.vista.Util;
  importjava.io.BufferedReader;
  importjava.io.BufferedWriter;
  importjava.io.File;
  importjava.io.FileInputStream;
  importjava.io.FileNotFoundException;
  importjava.io.IOException;
  importjava.io.InputStreamReader;
  importjava.io.OutputStream;
  importjava.io.OutputStreamWriter;
  importjava.io.PrintWriter;
  importjava.net.InetAddress;
  importjava.net.Socket;
  importjava.net.UnknownHostException;
  publicclassFileSenderextendsThread
  {
  privateSocketsocket=null;//客户端socket
  privateOutputStreamout=null;//向服务器写数据
  privateintRECEIVER_PORT=-1;//接收者端口
  privateFilefileToSend=null;//要传输的文件
  publicFileSender(Filefile,intReceiver_Port)
  {
  this.fileToSend=file;
  this.RECEIVER_PORT=Receiver_Port;//设置接收方端口
  try
  {
  this.socket=newSocket(InetAddress.getLocalHost(),RECEIVER_PORT);
  out=socket.getOutputStream();//向接收者发数据的
  }
  catch(UnknownHostExceptione)
  {
  //TODOAuto-generatedcatchblock
  e.printStackTrace();
  }
  catch(IOExceptione)
  {
  //TODOAuto-generatedcatchblock
  e.printStackTrace();
  }
  }
  publicvoidrun()
  {
  byte[]cmd=newbyte[128];
  byte[]tcmd="cp".getBytes();
  for(inti=0;i<tcmd.length;i++) <="" tcmd.length;i++)
  {
  cmd=tcmd;
  }
  cmd[tcmd.length]=-0;
  try
  {
  out.write(cmd,0,cmd.length);
  }
  catch(IOExceptione)
  {
  e.printStackTrace();
  }
  //文件名
  byte[]file=newbyte[256];
  byte[]tfile=this.fileToSend.getName().getBytes();
  for(inti=0;i<tfile.length;i++) <="" tfile.length;i++)
  {
  file=tfile;
  }
  file[tfile.length]=-0;
  try
  {
  out.write(file,0,file.length);
  }
  catch(IOExceptione)
  {
  e.printStackTrace();
  }
  //大小
  byte[]size=newbyte[64];
  byte[]tsize=(""+this.fileToSend.length()).getBytes();
  for(inti=0;i<tsize.length;i++) <="" tsize.length;i++)
  {
  size=tsize;
  }
  size[tsize.length]=-0;
  try
  {
  out.write(size,0,size.length);
  }
  catch(IOExceptione)
  {
  //TODOAuto-generatedcatchblock
  e.printStackTrace();
  }
  FileInputStreamfis=null;
  byte[]buf=newbyte[1024*10];
  try
  {
  fis=newFileInputStream(this.fileToSend);
  }
  catch(FileNotFoundExceptione)
  {
  e.printStackTrace();
  }
  intreadsize=0;
  try
  {
  while((readsize=fis.read(buf,0,buf.length))>0)
  {
  out.write(buf,0,readsize);
  out.flush();
  }
  }
  catch(IOExceptione)
  {
  e.printStackTrace();
  }
  }
  }






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2