国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java socket 發送文件

2019-11-14 20:56:32
字體:
來源:轉載
供稿:網友
java socket 發送文件

客戶端:

package tt;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.net.InetSocketAddress;import java.net.Socket;public class ClientTcpSend {    public static void main(String[] args) {        int length = 0;        byte[] sendByte = null;        Socket socket = null;        DataOutputStream dout = null;        FileInputStream fin = null;        try {          try {            socket = new Socket();            socket.connect(new InetSocketAddress("127.0.0.1", 33456),10 * 1000);            dout = new DataOutputStream(socket.getOutputStream());            File file = new File("E://TU//DSCF0320.JPG");            fin = new FileInputStream(file);            sendByte = new byte[1024];            dout.writeUTF(file.getName());            while((length = fin.read(sendByte, 0, sendByte.length))>0){                dout.write(sendByte,0,length);                dout.flush();            }            } catch (Exception e) {            } finally{                if (dout != null)                    dout.close();                if (fin != null)                    fin.close();                if (socket != null)                    socket.close();        }        } catch (Exception e) {            e.PRintStackTrace();        }    }}

服務端:

package test;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ServerTcpListener implements Runnable {    @Override    public void run() {    }    public static void main(String[] args) {        try {            final ServerSocket server = new ServerSocket(33456);            Thread th = new Thread(new Runnable() {                @Override                public void run() {                    while (true) {                        try {                           System.out.println("開始監聽。。。");                           Socket socket = server.accept();                           System.out.println("有鏈接");                           receiveFile(socket);                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                }            });            th.run();        } catch (Exception ex) {            ex.printStackTrace();        }    }    public static void receiveFile(Socket socket) throws IOException {        byte[] inputByte = null;        int length = 0;        DataInputStream din = null;        FileOutputStream fout = null;        try {            din = new DataInputStream(socket.getInputStream());                        fout = new FileOutputStream(new File("E://"+din.readUTF()));            inputByte = new byte[1024];            System.out.println("開始接收數據...");            while (true) {                if (din != null) {                    length = din.read(inputByte, 0, inputByte.length);                }                if (length == -1) {                    break;                }                System.out.println(length);                fout.write(inputByte, 0, length);                fout.flush();            }            System.out.println("完成接收");        } catch (Exception ex) {            ex.printStackTrace();        } finally {            if (fout != null)                fout.close();            if (din != null)                din.close();            if (socket != null)                socket.close();        }    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平乡县| 金沙县| 富平县| 十堰市| 安远县| 新乡市| 康定县| 永登县| 平原县| 常州市| 秦安县| 双江| 阳西县| 广安市| 东辽县| 阜新市| 象山县| 迁西县| 治多县| 石嘴山市| 交城县| 昌宁县| 南阳市| 井陉县| 兴城市| 安宁市| 柏乡县| 沙坪坝区| 兴仁县| 平湖市| 隆化县| 新田县| 和政县| 平陆县| 白山市| 天津市| 珲春市| 铜梁县| 龙口市| 郯城县| 达州市|