利用socket传递图片
Posted guoyansi19900907
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用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();
以上是关于利用socket传递图片的主要内容,如果未能解决你的问题,请参考以下文章
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题