java Socket 檔案傳輸
阿新 • • 發佈:2019-02-15
客戶端:
import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; /** * 檔案傳送客戶端主程式 * @author admin_Hzw * */ public class BxClient { /** * 程式main方法 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { int length = 0; double sumL = 0 ; byte[] sendBytes = null; Socket socket = null; DataOutputStream dos = null; FileInputStream fis = null; boolean bool = false; try { File file = new File("D:/天啊.zip"); //要傳輸的檔案路徑 long l = file.length(); socket = new Socket(); socket.connect(new InetSocketAddress("127.0.0.1", 48123)); dos = new DataOutputStream(socket.getOutputStream()); fis = new FileInputStream(file); sendBytes = new byte[1024]; while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) { sumL += length; System.out.println("已傳輸:"+((sumL/l)*100)+"%"); dos.write(sendBytes, 0, length); dos.flush(); } //雖然資料型別不同,但JAVA會自動轉換成相同資料型別後在做比較 if(sumL==l){ bool = true; } }catch (Exception e) { System.out.println("客戶端檔案傳輸異常"); bool = false; e.printStackTrace(); } finally{ if (dos != null) dos.close(); if (fis != null) fis.close(); if (socket != null) socket.close(); } System.out.println(bool?"成功":"失敗"); } }
服務端:
import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; import com.boxun.util.GetDate; /** * 接收檔案服務 * @author admin_Hzw * */ public class BxServerSocket { /** * 工程main方法 * @param args */ public static void main(String[] args) { try { final ServerSocket server = new ServerSocket(48123); 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) { System.out.println("伺服器異常"); e.printStackTrace(); } } } }); th.run(); //啟動執行緒執行 } catch (Exception e) { e.printStackTrace(); } } public void run() { } /** * 接收檔案方法 * @param socket * @throws IOException */ public static void receiveFile(Socket socket) throws IOException { byte[] inputByte = null; int length = 0; DataInputStream dis = null; FileOutputStream fos = null; String filePath = "D:/temp/"+GetDate.getDate()+"SJ"+new Random().nextInt(10000)+".zip"; try { try { dis = new DataInputStream(socket.getInputStream()); File f = new File("D:/temp"); if(!f.exists()){ f.mkdir(); } /* * 檔案儲存位置 */ fos = new FileOutputStream(new File(filePath)); inputByte = new byte[1024]; System.out.println("開始接收資料..."); while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) { fos.write(inputByte, 0, length); fos.flush(); } System.out.println("完成接收:"+filePath); } finally { if (fos != null) fos.close(); if (dis != null) dis.close(); if (socket != null) socket.close(); } } catch (Exception e) { e.printStackTrace(); } } }
時間工具類:
import java.text.SimpleDateFormat; import java.util.Date; /** * 時間工具類 * @author admin_Hzw * */ public class GetDate { /** * 時間格式到毫秒 */ private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); public static String getDate(){ return df.format(new Date()); } }