A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xgm 中级黑马   /  2016-3-14 23:04  /  376 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //服务器
  2. public class Server {

  3.         public static void main(String[] args) {
  4.                
  5.                 ServerSocket ss = null;
  6.                 Socket s = null;
  7.                 try {
  8.                        
  9.                         ss = new ServerSocket(6565);
  10.                         BufferedInputStream bis = null;
  11.                         BufferedOutputStream bos = null;
  12.                         while(true){
  13.                                 try{
  14.                                         s = ss.accept();
  15.                                         bis = new BufferedInputStream(s.getInputStream());
  16.                                         bos = new BufferedOutputStream(new FileOutputStream("F:\\java带吗 "));
  17.                                        
  18.                                         byte[] buff = new byte[1024];
  19.                                         int len = 0;
  20.                                         while((len=bis.read(buff))>0){
  21.                                                 bos.write(buff, 0, len);
  22.                                         }
  23.                                 }finally{
  24.                                         if(bis!=null){
  25.                                                 bis.close();
  26.                                         }
  27.                                         if(bos!=null){
  28.                                                 bis.close();
  29.                                         }
  30.                                         if(s!=null){
  31.                                                 s.close();
  32.                                         }
  33.                                 }
  34.                         }
  35.                 } catch (IOException e) {
  36.                         e.printStackTrace();
  37.                 }finally{
  38.                         try {
  39.                                 if(ss!=null){
  40.                                         ss.close();
  41.                                 }
  42.                         } catch (IOException e) {
  43.                                 e.printStackTrace();
  44.                         }
  45.                 }
  46.         }
  47. }
复制代码
  1. //客户端
  2. public class Client {

  3.         public static void main(String[] args) {
  4.                 Socket s = null;
  5.                 BufferedOutputStream bos = null;
  6.                 BufferedInputStream bis = null;
  7.                 String filepath = "F:\\eclipse\\java代码";
  8.                 try {
  9.                         s = new Socket("127.0.0.1",6565);
  10.                         bis = new BufferedInputStream(new FileInputStream(filepath));
  11.                         bos = new BufferedOutputStream(s.getOutputStream());
  12.                         byte[] buff = new byte[1024];
  13.                         int len = 0;
  14.                         while((len=bis.read(buff))>0){
  15.                                 bos.write(buff, 0, len);
  16.                         }
  17.                 } catch (UnknownHostException e) {
  18.                         e.printStackTrace();
  19.                 } catch (IOException e) {
  20.                         e.printStackTrace();
  21.                 }  finally{
  22.                         try {
  23.                                 if(bis!=null){
  24.                                         bis.close();
  25.                                 }
  26.                         } catch (IOException e2) {
  27.                                 e2.printStackTrace();
  28.                         }
  29.                         try {
  30.                                 if(bos!=null){
  31.                                         bos.close();
  32.                                 }
  33.                         } catch (IOException e1) {
  34.                                 e1.printStackTrace();
  35.                         }
  36.                         try {
  37.                                 if(s!=null){
  38.                                         s.close();
  39.                                 }
  40.                         } catch (IOException e) {
  41.                                 e.printStackTrace();
  42.                         }
  43.                 }
  44.         }
  45. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马