码迷,mamicode.com
首页 > 其他好文 > 详细

利用socket传递图片

时间:2019-06-05 09:52:47      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:class   length   inpu   ack   string   cep   system   get   except   

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传递图片

标签:class   length   inpu   ack   string   cep   system   get   except   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!