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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

实现服务器能同时接收多个客户端访问
  1. import java.io.*;
  2. import java.net.*;
  3. class NtClient//客户端
  4. {
  5.      public static void sop(Object obj)//自定义输出方法
  6.      {
  7.           System.out.println(obj);
  8.      }
  9.      public static void main(String[] args)throws Exception
  10.      {
  11.           if(args.length!=1)//判断是否传入参数
  12.           {
  13.                sop("请选择一个jpg格式的图片");
  14.                return;
  15.           }
  16.           File file=new File(args[0]);//将图片地址转换为File对象
  17.           if(!(file.exists()&&file.isFile()))
  18.           {
  19.                sop("文件有问题");
  20.                return;
  21.           }
  22.           if(!file.getName().endsWith(".jpg"))
  23.           {
  24.                sop("图片格式错误");
  25.                return;
  26.           }
  27.           if(file.length()>1024*1024*5)
  28.           {
  29.                sop("文件过大");
  30.                return;
  31.           }
  32.           Socket s=new Socket("127.0.0.1",10005);//连接服务器及端口
  33.           FileInputStream fis=new FileInputStream(file);//创建流输入对象
  34.           OutputStream out=s.getOutputStream();//创建流输出对象
  35.           byte[] buf=new byte[1024];
  36.           int len=0;
  37.           while((len=fis.read(buf))!=-1)
  38.           {
  39.                out.write(buf,0,len);
  40.           }
  41.           s.shutdownOutput();//网络传输结束
  42.           InputStream in=s.getInputStream();//接收服务器端返回信息
  43.           byte[] bufIn=new byte[1024];
  44.           int num=in.read(bufIn);
  45.           System.out.println(new String(bufIn,0,num));
  46.           fis.close();
  47.           s.close();
  48.      }
  49. }
  50. class NtThread implements Runnable//实现多线程上传
  51. {
  52.      private Socket s;
  53.      NtThread(Socket s)
  54.      {
  55.           this.s=s;
  56.      }
  57.      public void run()
  58.      {
  59.           String ip=s.getInetAddress().getHostAddress();//客户端地址
  60.           try
  61.           {              
  62.                int count=1;
  63.                System.out.println(ip+"...connected");
  64.                InputStream in=s.getInputStream();//接收数据
  65.                File file=new File(ip+"("+(count)+").jpg");//创建服务器端文件
  66.                while(file.exists())
  67.                     file=new File(ip+"("+(count++)+").jpg");
  68.                FileOutputStream out=new FileOutputStream(file);
  69.                byte[] buf=new byte[1024];
  70.                int len=0;
  71.                while((len=in.read(buf))!=-1)//将接收数据写入服务器端文件中
  72.                {
  73.                     out.write(buf,0,len);
  74.                }
  75.                OutputStream op=s.getOutputStream();//往客户端返回信息
  76.                op.write("上传完成".getBytes());
  77.                out.close();
  78.                s.close();         
  79.           }
  80.           catch(Exception e)
  81.           {
  82.                throw new RuntimeException(ip+"上传失败");
  83.    
  84.           }
  85.      }
  86. }
  87. class NtServer//服务器端
  88. {
  89.      public static void main(String[] args)throws Exception
  90.      {
  91.           ServerSocket ss=new ServerSocket(10005);
  92.           while(true)
  93.           {
  94.                Socket s=ss.accept();
  95.                new Thread(new NtThread(s)).start();
  96.           }              
  97.      }
  98. }
复制代码

0 个回复

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