继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JAVA 图像传输 我是名字的小尾巴

尘T陌
关注TA
已关注
手记 17
粉丝 10
获赞 81

服务端

package cqupt.DNN.Lin;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SerVer {

     public static void main(String[] args) {

            try {
                final ServerSocket server = new ServerSocket(33456);
                Thread th = new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            try {
                                System.out.println("开端监听...");
                                Socket socket = server.accept();
                                System.out.println("有链接");
                                receiveFile(socket);
                            } catch (Exception e) {
                            }
                        }
                    }

                });

                th.run(); //启动线程运行
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void run() {
        }

        public static void receiveFile(Socket socket) {

            byte[] inputByte = null;
            int length = 0;
            DataInputStream dis = null;
            FileOutputStream fos = null;
            try {
                try {

                    dis = new DataInputStream(socket.getInputStream());
                    fos = new FileOutputStream(new File("C:/Users/China/Desktop/cc.jpg"));
                    inputByte = new byte[1024];
                    System.out.println("开端接管数据...");
                    while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
                        System.out.println(length);
                        fos.write(inputByte, 0, length);
                        fos.flush();
                    }
                    System.out.println("完成接管");
                } finally {
                    if (fos != null)
                        fos.close();
                    if (dis != null)
                        dis.close();
                    if (socket != null)
                        socket.close();
                }
            } catch (Exception e) {

            }

        }

}

客服端

package cqupt.DNN.Lin;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {

     public static void main(String[] args) {
            int length = 0;
            byte[] sendBytes = null;
            Socket socket = null;
            DataOutputStream dos = null;
            FileInputStream fis = null;
            try {
                try {
                    socket = new Socket();
                    socket.connect(new InetSocketAddress("127.0.0.1", 33456),
                            10 * 1000);
                    dos = new DataOutputStream(socket.getOutputStream());
//                  File tempFile = new File("/sdcard/temp.jpg");//  Android  图片路径
                    File file = new File("显示界面.jpg");
                    fis = new FileInputStream(file);
                    sendBytes = new byte[1024];
                    while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
                        dos.write(sendBytes, 0, length);
                        dos.flush();
                    }
                } finally {
                    if (dos != null)
                        dos.close();
                    if (fis != null)
                        fis.close();
                    if (socket != null)
                        socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP