欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现文件上传服务器和客户端

程序员文章站 2023-12-17 13:07:52
本文实例为大家分享了java实现文件上传服务器和客户端的具体代码,供大家参考,具体内容如下 文件上传服务器端: /** * 使用tcp协议实现上传功能...

本文实例为大家分享了java实现文件上传服务器和客户端的具体代码,供大家参考,具体内容如下

文件上传服务器端:

/** 
 * 使用tcp协议实现上传功能的服务器端 
 * 思路: 
 * 新建serversocket 
 * 等待客户端连接 
 * 连接上后开启子线程,把连接获取的socket传给子线程 
 * 循环进行 
 * @author yajun 
 * 
 */ 
public class uploadserver { 
  
 public static void main(string[] args) { 
  uploadserver server=new uploadserver(); 
  uploadthread command=new uploadthread(); 
  server.start(command); 
 } 
  
 /** 
  * 功能:接受连接,开启子线程,循环 
  * @param command 用于下载的子线程对象,该对象实现了runnable接口 
  */ 
 private void start(uploadthread command){ 
  //局部变量 
  serversocket serversocket = null; 
  socket transsocket; 
  //业务逻辑 
  try { 
   serversocket=new serversocket(55555); 
   while(true){ 
    system.out.println("等待连接……"); 
    transsocket=serversocket.accept(); 
    int i=0; 
    i++; 
    system.out.println("第"+i+"个连接"); 
    //用不用在下载完后关闭线程呢??? 
    command.setsocket(transsocket); 
    executors.newfixedthreadpool(5).execute(command); 
   } 
  //异常捕获 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  //关闭资源 
  } finally{ 
   try { 
    serversocket.close(); 
   } catch (ioexception e) { 
    e.printstacktrace(); 
   } 
  }//end of try 
 }//end of connect 
 @test 
 public void testconnect() { 
  //测试任务:先运行服务器端,然后多次运行客户端,服务器段可以不断创建子线程,则测试成功 
  //测试准备:构造一个线程,用于模拟下载线程 
  uploadthread command=new uploadthread(); 
  start(command); 
   
 } 
 
 @test 
 public void testdown() throws ioexception { 
  byte[] buf; 
  bytearrayinputstream bis; 
  string str="canglaoshi.avi\ncontent,content,content"; 
  buf=str.getbytes(); 
  bis=new bytearrayinputstream(buf); 
  uploadthread ut=new uploadthread(); 
  ut.down(bis); 
 } 
} 
//完成各个传输任务的子线程 
class uploadthread implements runnable{ 
  
 socket socket; 
 public uploadthread(){} 
 public uploadthread(socket socket){ 
  this.socket=socket; 
 } 
 @override 
 public void run() { 
  inputstream in; 
  try { 
    
   in = socket.getinputstream(); 
   down(in); 
    
  //异常处理 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } finally{ 
   try { 
    socket.close(); 
   } catch (ioexception e) { 
    e.printstacktrace(); 
   } 
  } 
  //测试代码 
  /*try { 
   thread.sleep(5000); 
   system.out.println(thread.currentthread().getname()+",复制完毕"); 
  } catch (interruptedexception e) { 
   e.printstacktrace(); 
  }*/ 
 }//end of run 
 public void setsocket(socket socket){ 
  this.socket=socket; 
 } 
 //下载方法 
 /** 
  * 目标:把inputstream中的数据写入到本地 
  * 思路: 
  * 1.获取输入流,最好传入输入流,而不是直接从socket获取,传入有利用单元测试 
  * 2.从输入流中读到文件名 
  * 3.新建文件和文件输出流 
  * 4.从输入流中读到文件内容到文件输出流 
  * 5. 
  * @throws ioexception 
  */ 
 public void down(inputstream in) throws ioexception{ 
  //局部变量 
  char ch; 
  char[] namearr=new char[256]; 
  byte[] buf=new byte[1024]; 
  string name=""; 
  outputstream out = null; 
  //业务逻辑 
  try { 
   //第一步:获取文件名,构造文件输出流 
   int i=0; 
   while((ch=(char) in.read())!='\n'){ 
    namearr[i++]= ch; 
   } 
   //name=namearr.tostring();//这句话无法将字符数组转换为字符串,需用下面的语句 
   name=new string(namearr); 
   system.out.println("要下载的文件为:"+name); 
   out=new fileoutputstream("src\\down\\"+name); 
   //第二步:将输入流中的其他内容写入到文件 
   int len; 
   while((len=in.read(buf))!=-1){ 
    out.write(buf,0,len); 
   } 
   out.flush(); 
  //异常捕获 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  //关闭资源 
  }finally{ 
   //疑问:两个捕获可不可以放到一块呢,怎样处理关闭流时的异常最好呢? 
   in.close(); 
   out.close(); 
  } 
  //调试 
  system.out.println(name); 
 } 
  
}//end of uploadthread 

文件上传客户端:

/** 
 * 使用tcp协议实现上传功能的客户端 
 * @author yajun 
 */ 
public class uploadclient { 
  
 public static void main(string[] args) { 
  uploadclient client=new uploadclient(); 
  client.upload("src\\thursday\\aslisttest.java"); 
 } 
 
 /** 
  * 作用:上传文件到服务器 
  * 1.建立到服务器的连接 
  * 2.获取输出流 
  * 3.将文件内容写入到输出流 
  * 4.获取服务器的响应 
  */ 
 private void upload(string name){ 
  socket socket=null; 
  outputstream out; 
  try { 
   socket=new socket("127.0.0.1", 55555); 
   out=socket.getoutputstream(); 
   write2outputstream(name, out); 
  //异常捕获 
  } catch (unknownhostexception e) { 
   e.printstacktrace(); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } 
 } 
 @test 
 public void testupload() { 
  upload("src\\status.xml"); 
 } 
 /** 
  * 作用:传入文件名和输出流,将文件写入到输出流 
  * @param path 
  * @throws ioexception 
  */ 
 private void write2outputstream(string path,outputstream out) throws ioexception{ 
   
  fileinputstream fis = null; 
  byte[] buf=new byte[1024]; 
  string filename=""; 
  //业务逻辑 
  try { 
    
   //1.写入文件名 
   filename=path.substring(path.lastindexof('\\')+1); 
   system.out.println("您要上传的文件名为:"+filename); 
   out.write(filename.getbytes()); 
   out.write('\n'); 
   //2.写入文件内容 
   fis=new fileinputstream(path); 
   int len; 
   while((len=fis.read(buf))!=-1){ 
    out.write(buf, 0, len); 
   } 
   out.flush(); 
  //异常处理 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  //关闭资源 
  } finally{ 
   fis.close(); 
   out.close(); 
  } 
 }//end of upload 
 @test 
 public void testwrite2outputstream() throws ioexception { 
  bytearrayoutputstream out = null; 
  try { 
   out=new bytearrayoutputstream(); 
   write2outputstream("src\\status.xml", out); 
   system.out.println(out.tostring("utf-8")); 
  } catch (ioexception e) { 
   e.printstacktrace(); 
  } finally{ 
   out.close(); 
  } 
   
 } 
  
}

本文已被整理到了《java上传操作技巧汇总》,欢迎大家学习阅读。

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: