为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

服务端

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();
            }
        }

}
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消