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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© haojingwei310 中级黑马   /  2016-6-16 22:45  /  335 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Demo05_Client {
        public static void main(String[] args) throws Exception, IOException {
                //2:创建客户端对象,连接服务器
                  Socket socket = new Socket("192.168.74.79", 6668);
                  //3:获取Socket的输出流 作用:用来把数据写到服务器中
                  OutputStream out = socket.getOutputStream();
                  //4:创建文件字节输入流   作用 :用来读取数据源的字节
                  BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream("D:\\test\\1.jpg"));
              //5:把图片数据写到 Socket的输出流中(数据要传给服务器)
                  byte[] buffer = new byte[1024];
                  int len = -1;
                  while((len=fileIn.read(buffer))!=-1){
                          //把数据写到Socket的输出流中
                          out.write(buffer, 0, len);
                  }
                  //6:客户端发送数据完毕,结果Socket输出流的 写的操作,告知服务器
                  socket.shutdownOutput();
                  
                  //---------------------反馈信息------------------
                  //12:获取Socket的输入流  作用:读反馈信息
                  InputStream in = socket.getInputStream();
                  //13:读反馈信息
                  byte[] info = new byte[1024];
                  //把反馈信息存储到info数组中,并记录字节数
                  int length = in.read(info);
                  //显示反馈信息
                  System.out.println(new String(info, 0, length));
                  
                  //关闭流
                  in.close();
                  fileIn.close();
                  out.close();
                  socket.close();       
        }
}

public class Demo05_Server {
        public static void main(String[] args) throws Exception {
                 //1:创建服务器对象 等待连接
                  ServerSocket serverSocket = new ServerSocket(6668);
                  Socket clientSocket = serverSocket.accept();
                  //显示 这个客户端 的一些信息
                  //显示那个客户端 连的服务器
                  InetAddress ipAddress = clientSocket.getInetAddress();
                  String ip = ipAddress.getHostAddress();
                  System.out.println("小样,抓到你了,连接我!!!!"+ip);
                  
                  //7:获取Socket输入流
                  InputStream in = clientSocket.getInputStream();
                  //8:创建目的地的文件字节输出流
                  BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream("D:\\"+System.currentTimeMillis()+".jpg"));
                  //9:把Socket输入流中的数据,写到目的地的字节输出流中
                  byte[] buffer = new byte[1024];
                  int len = -1;
                  while((len=in.read(buffer))!=-1){
                          //写入目的地的字节输出流中
                          fileOut.write(buffer, 0, len);
                  }
                  
                  //----------------反馈信息-------------------------
                  //10:获取Socket的输出流  作用:写反馈信息给客户端
                  OutputStream out = clientSocket.getOutputStream();
                  //11:写反馈信息给客户端
                  out.write("图片上传成功".getBytes());
                  //释放资源
                  
                  out.close();
                  fileOut.close();
                  in.close();
                  clientSocket.close();
        }
}

0 个回复

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