淺析Java基於Socket的文件傳輸案例。本站提示廣大學習愛好者:(淺析Java基於Socket的文件傳輸案例)文章只能為提供參考,不一定能成為您想要的結果。以下是淺析Java基於Socket的文件傳輸案例正文
本文實例引見了Java基於Socket的文件傳輸案例,分享給年夜家供年夜家參考,詳細內容以下
1、Java代碼
package com.wf.demo.socket.socketfile; import java.net.*; import java.io.*; /** * 2.socket的Util幫助類 * * @author willson * */ public class ClientSocket { private String ip; private int port; private Socket socket = null; DataOutputStream out = null; DataInputStream getMessageStream = null; public ClientSocket(String ip, int port) { this.ip = ip; this.port = port; } /** * 創立socket銜接 * * @throws Exception * exception */ public void CreateConnection() throws Exception { try { socket = new Socket(ip, port); } catch (Exception e) { e.printStackTrace(); if (socket != null) socket.close(); throw e; } finally { } } // 發送新聞 public void sendMessage(String sendMessage) throws Exception { try { out = new DataOutputStream(socket.getOutputStream()); if (sendMessage.equals("Windows")) { out.writeByte(0x1); out.flush(); return; } if (sendMessage.equals("Unix")) { out.writeByte(0x2); out.flush(); return; } if (sendMessage.equals("Linux")) { out.writeByte(0x3); out.flush(); } else { out.writeUTF(sendMessage); out.flush(); } } catch (Exception e) { e.printStackTrace(); if (out != null) out.close(); throw e; } finally { } } // 接收新聞 public DataInputStream getMessageStream() throws Exception { try { getMessageStream = new DataInputStream(new BufferedInputStream( socket.getInputStream())); return getMessageStream; } catch (Exception e) { e.printStackTrace(); if (getMessageStream != null) getMessageStream.close(); throw e; } finally { } } // 封閉銜接 public void shutDownConnection() { try { if (out != null) out.close(); if (getMessageStream != null) getMessageStream.close(); if (socket != null) socket.close(); } catch (Exception e) { } } }
2、Java代碼
package com.wf.demo.socket.socketfile; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.ServerSocket; import java.net.Socket; /** * 1.辦事器端 * * @author willson * */ public class ServerTest { int port = 8821; void start() { Socket socket = null; try { ServerSocket serverSocket = new ServerSocket(port); while (true) { // 選擇停止傳輸的文件 String filePath = "E:\\lib.zip"; File fi = new File(filePath); System.out.println("File Name:" + fi.getName() + ";\tFile Size():" + (int) fi.length() + "bytes"); // public Socket accept() throws // IOException偵聽並接收到此套接字的銜接。此辦法在停止銜接之前一向壅塞。 System.out.println("期待客戶端銜接,銜接端口:" + port); socket = serverSocket.accept(); System.out.println("樹立socket鏈接"); DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); dis.readByte(); DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); DataOutputStream ps = new DataOutputStream(socket.getOutputStream()); // 將文件名及長度傳給客戶端。這裡要真正實用一切平台,例如中文名的處置,還須要加工,詳細可以拜見Think In Java // 4th裡有現成的代碼。 ps.writeUTF(fi.getName()); ps.flush(); ps.writeLong((long) fi.length()); ps.flush(); int bufferSize = 8192; byte[] buf = new byte[bufferSize]; while (true) { int read = 0; if (fis != null) { read = fis.read(buf); } if (read == -1) { break; } ps.write(buf, 0, read); } ps.flush(); // 留意封閉socket鏈接哦,否則客戶端會期待server的數據過去, // 直到socket超時,招致數據不完全。 fis.close(); socket.close(); System.out.println("文件傳輸完成\n"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String arg[]) { new ServerTest().start(); } }
3、客戶端
package com.wf.demo.socket.socketfile; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; /** * 3.客戶端 * * @author willson * */ public class ClientTest { private ClientSocket cs = null; private String ip = "localhost";// 設置成辦事器IP private int port = 8821; private String sendMessage = "Windwos"; public ClientTest() { try { if (createConnection()) { sendMessage(); getMessage("F:\\"); } } catch (Exception ex) { ex.printStackTrace(); } } private boolean createConnection() { cs = new ClientSocket(ip, port); try { cs.CreateConnection(); System.out.print("銜接辦事器勝利!" + "\n"); return true; } catch (Exception e) { System.out.print("銜接辦事器掉敗!" + "\n"); return false; } } private void sendMessage() { if (cs == null) return; try { cs.sendMessage(sendMessage); } catch (Exception e) { System.out.print("發送新聞掉敗!" + "\n"); } } private void getMessage(String savePath) { if (cs == null) return; DataInputStream inputStream = null; try { inputStream = cs.getMessageStream(); } catch (Exception e) { System.out.print("吸收新聞緩存毛病\n"); return; } try { // 當地保留途徑,文件名會主動從辦事器端繼續而來。 int bufferSize = 8192; byte[] buf = new byte[bufferSize]; int passedlen = 0; long len = 0; savePath += inputStream.readUTF(); DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)))); len = inputStream.readLong(); System.out.println("File Size():" + len + "bytes"); System.out.println("開端吸收文件!" + "\n"); while (true) { int read = 0; if (inputStream != null) { read = inputStream.read(buf); } passedlen += read; if (read == -1) { break; } // 上面進度條本為圖形界面的prograssBar做的,這裡假如是打文件,能夠會反復打印出一些雷同的百分比 System.out.println("文件吸收了" + (passedlen * 100 / len) + "%\n"); fileOut.write(buf, 0, read); } System.out.println("吸收完成,文件存為" + savePath + "\n"); fileOut.close(); } catch (Exception e) { System.out.println("吸收新聞毛病" + "\n"); return; } } public static void main(String arg[]) { new ClientTest(); } }
願望本文所述對年夜家進修java法式設計有所贊助。