zoukankan      html  css  js  c++  java
  • tcp图片上传

    1.client

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

    2.server

    public static void main(String[] args) throws IOException {
            // 创建服务器Socket对象
            ServerSocket ss = new ServerSocket(19191);
    
            // 监听客户端连接
            Socket s = ss.accept();
    
            // 封装通道内流
            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            // 封装图片文件
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream("mn.jpg"));
    
            byte[] bys = new byte[1024];
            int len = 0;
            while ((len = bis.read(bys)) != -1) {
                bos.write(bys, 0, len);
                bos.flush();
            }
    
            // 给一个反馈
            OutputStream os = s.getOutputStream();
            os.write("图片上传成功".getBytes());
    
            bos.close();
            s.close();
        }

    注意:IO流的操作流程不熟悉,查询api

  • 相关阅读:
    django修身大法之一阳指
    django修身大法之归天心法
    事务(转账功能)
    el技术,,,,,JSTL技术,,,,,javaEE的开发模式
    DBUtils
    Jquery的Ajax
    实例_一个CSS开门动画
    js_快速判断浏览器是否是IE9以下版本浏览器
    html5_通过网页添加QQ或者加入QQ群
    html5_video&audio的autoplay属性失效的解决方法
  • 原文地址:https://www.cnblogs.com/csslcww/p/9208352.html
Copyright © 2011-2022 走看看