zoukankan      html  css  js  c++  java
  • Java基础知识强化之网络编程笔记13:TCP之TCP协议上传图片并给出反馈

    1. TCP协议上传图片并给出反馈:

    (1)客户端:

     1 package cn.itcast_13;
     2 
     3 import java.io.BufferedInputStream;
     4 import java.io.BufferedOutputStream;
     5 import java.io.FileInputStream;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.net.Socket;
     9 
    10 public class UploadClient {
    11     public static void main(String[] args) throws IOException {
    12         // 创建客户端Socket对象
    13         Socket s = new Socket("192.168.12.92", 19191);
    14 
    15         // 封装图片文件
    16         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    17                 "林青霞.jpg"));
    18         // 封装通道内的流
    19         BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
    20 
    21         byte[] bys = new byte[1024];
    22         int len = 0;
    23         while ((len = bis.read(bys)) != -1) {
    24             bos.write(bys, 0, len);
    25             bos.flush();
    26         }
    27         
    28         s.shutdownOutput();
    29 
    30         // 读取反馈
    31         InputStream is = s.getInputStream();
    32         byte[] bys2 = new byte[1024];
    33         int len2 = is.read(bys2);
    34         String client = new String(bys2, 0, len2);
    35         System.out.println(client);
    36 
    37         // 释放资源
    38         bis.close();
    39         s.close();
    40     }
    41 }

    (2)服务端:

     1 package cn.itcast_13;
     2 
     3 import java.io.BufferedInputStream;
     4 import java.io.BufferedOutputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.io.OutputStream;
     8 import java.net.ServerSocket;
     9 import java.net.Socket;
    10 
    11 public class UploadServer {
    12     public static void main(String[] args) throws IOException {
    13         // 创建服务器Socket对象
    14         ServerSocket ss = new ServerSocket(19191);
    15 
    16         // 监听客户端连接
    17         Socket s = ss.accept();
    18 
    19         // 封装通道内流
    20         BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
    21         // 封装图片文件
    22         BufferedOutputStream bos = new BufferedOutputStream(
    23                 new FileOutputStream("mn.jpg"));
    24 
    25         byte[] bys = new byte[1024];
    26         int len = 0;
    27         while ((len = bis.read(bys)) != -1) {
    28             bos.write(bys, 0, len);
    29             bos.flush();
    30         }
    31 
    32         // 给一个反馈
    33         OutputStream os = s.getOutputStream();
    34         os.write("图片上传成功".getBytes());
    35 
    36         bos.close();
    37         s.close();
    38     }
    39 }
  • 相关阅读:
    对react的context的研究
    react组件通信props属性传递
    react的组件的两种形式
    对react构造函数研究--------引用
    ReactRouter基本使用2
    ReactRouter基本使用1
    react生命周期
    对react进行研究----入门
    Nginx源码安装
    Vmware克隆linux6虚拟机后网络问题
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4872587.html
Copyright © 2011-2022 走看看