zoukankan      html  css  js  c++  java
  • 利用socket传递图片

    package com.company.s3;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
        public static void main(String[] args)  throws Exception{
            byte[] byteArray=new byte[2048];
            ServerSocket serverSocket=new ServerSocket(8088);
            Socket socket=serverSocket.accept();
    
            InputStream inputStream=socket.getInputStream();
            int readLength=inputStream.read(byteArray);
    
            FileOutputStream pngoutputstream=new FileOutputStream(new File("e:\2.jpg"));
            while (readLength!=-1){
                pngoutputstream.write(byteArray,0,readLength);
                readLength=inputStream.read(byteArray);
            }
            pngoutputstream.close();
            inputStream.close();
            socket.close();
            serverSocket.close();
        }
    }
    package com.company.s3;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class Client {
        public static void main(String[] args)  throws Exception{
           String pngFile="e:\1.jpg";
            FileInputStream pngstream=new FileInputStream(new File(pngFile));
            byte[] byteArray=new byte[2048];
    
            System.out.println("socket begin "+System.currentTimeMillis());
            Socket socket=new Socket("localhost",8088);
            System.out.println("socket end"+System.currentTimeMillis());
    
            OutputStream outputStream=socket.getOutputStream();
    
            int readLength=pngstream.read(byteArray);
            while (readLength!=-1){
                outputStream.write(byteArray,0,readLength);
                readLength=pngstream.read(byteArray);
            }
            outputStream.close();
            pngstream.close();
            socket.close();
    
        }
    }
  • 相关阅读:
    BufferedImage学习记录一
    response总结一
    Externalizable接口
    request 总结一
    处理jsp显示文字过长问题
    验证码设计
    ORA01461: 仅能绑定要插入 LONG 列的 LONG 值
    MAP平台在单据中填写好部门后,关闭后重新打开,部门就没有了
    MAP平台设置节点选取范围
    MAP平台java.lang.StackOverflowError
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/10977444.html
Copyright © 2011-2022 走看看