亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消