首页 > 其他 > 详细

利用socket传递图片

时间:2019-06-05 10:15:33      阅读:78      评论:0      收藏:0      [点我收藏+]
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();

    }
}

 

利用socket传递图片

原文:https://www.cnblogs.com/guoyansi19900907/p/10977444.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!