首页 > 代码库 > [JAVA]Socket 图片流的传输
[JAVA]Socket 图片流的传输
import java.io.DataInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class Server { public static void main(String[] args) { System.out.println("--->server"); try { ServerSocket server = new ServerSocket(4700); Socket socket = server.accept(); DataInputStream dis = new DataInputStream(socket.getInputStream()); String type = dis.readUTF(); System.out.println("type: " + type); // long len = dis.readLong(); // System.out.println("len: " + len); byte[] buff = new byte[1024]; OutputStream os = new FileOutputStream("C:/Users/Administrator/Desktop/dts.jpg"); int len = -1; while ((len = dis.read(buff)) != -1) { os.write(buff, 0, len); } os.flush(); os.close(); dis.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("server--->"); }}
import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.net.Socket;public class Client { public static void main(String args[]) { try { Socket socket = new Socket("127.0.0.1", 4700); if (socket.isConnected()) { System.out.println("isConnected"); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF("STREAM"); File file = new File("C:/Users/Administrator/Desktop/src.jpg"); // dos.writeLong(file.length()); byte[] buff = new byte[1024]; InputStream inputStream = new FileInputStream(file); int len = -1; while ((len = inputStream.read(buff)) != -1) { dos.write(buff, 0, len); } inputStream.close(); dos.flush(); dos.close(); } // socket.close(); // 关闭Socket } catch (Exception e) { e.printStackTrace(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。