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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java通過套接字發送和接收文件(字節[])

Java通過套接字發送和接收文件(字節[])

海綿寶寶撒 2019-10-16 11:09:12
我正在嘗試開發一個非常簡單的客戶端/服務器,其中客戶端將文件轉換為字節,將其發送到服務器,然后將字節轉換回文件。當前,程序僅創建一個空文件。我不是一名出色的Java開發人員,因此不勝感激。這是接收客戶端發送的內容的服務器部分。ServerSocket serverSocket = null;    serverSocket = new ServerSocket(4444);    Socket socket = null;    socket = serverSocket.accept();    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));    DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));    byte[] bytes = new byte[1024];    in.read(bytes);    System.out.println(bytes);    FileOutputStream fos = new FileOutputStream("C:\\test2.xml");    fos.write(bytes);這是客戶端部分Socket socket = null;    DataOutputStream out = null;    DataInputStream in = null;    String host = "127.0.0.1";         socket = new Socket(host, 4444);    out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));    in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));    File file = new File("C:\\test.xml");    //InputStream is = new FileInputStream(file);    // Get the size of the file    long length = file.length();    if (length > Integer.MAX_VALUE) {        System.out.println("File is too large.");    }    byte[] bytes = new byte[(int) length];    //out.write(bytes);    System.out.println(bytes);    out.close();    in.close();    socket.close();
查看完整描述

3 回答

  • 3 回答
  • 0 關注
  • 686 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

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

公眾號

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