Java基於Socket的文件傳輸完成辦法。本站提示廣大學習愛好者:(Java基於Socket的文件傳輸完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java基於Socket的文件傳輸完成辦法正文
本文實例講述了Java基於Socket的文件傳輸完成辦法。分享給年夜家供年夜家參考,詳細以下:
1. Java代碼以下:
package sterning; 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; public class ServerTest { int port = 8821; void start() { Socket s = null; try { ServerSocket ss = new ServerSocket(port); while (true) { // 選擇停止傳輸的文件 String filePath = "D:\\lib.rar"; File fi = new File(filePath); System.out.println("文件長度:" + (int) fi.length()); // public Socket accept() throws // IOException偵聽並接收到此套接字的銜接。此辦法在停止銜接之前一向壅塞。 s = ss.accept(); System.out.println("樹立socket鏈接"); DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream())); dis.readByte(); DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); DataOutputStream ps = new DataOutputStream(s.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(); s.close(); System.out.println("文件傳輸完成"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String arg[]) { new ServerTest().start(); } }
2.socket的Util幫助類
package sterning; import java.net.*; import java.io.*; 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) { } } }
3.客戶端
package sterning; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; 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(); } } 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() { if (cs == null) return; DataInputStream inputStream = null; try { inputStream = cs.getMessageStream(); } catch (Exception e) { System.out.print("吸收新聞緩存毛病\n"); return; } try { //當地保留途徑,文件名會主動從辦事器端繼續而來。 String savePath = "E:\\"; 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("文件的長度為:" + len + "\n"); 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法式設計有所贊助。