vulnhub靶機writeup之WestWild:1.1
阿新 • • 發佈:2020-10-08
package com.herley; /** * * 客戶端 */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; import java.util.TimeZone; public class Send { public static void main(String[] args) { File filesrc; //需要傳送的檔案 Socket socket; //套接字 FileInputStream open; //讀取檔案 FileOutputStream out; //傳送檔案 Scanner sc;//鍵盤錄入 Date start; Date end; try { System.out.println("[歡迎使用區域網傳送系統,僅支援單檔案傳送]"); System.out.print("請輸入你要傳送的檔案路徑!"); System.out.println("(注意格式:檔案路徑+字尾名!)"); //需要傳輸的檔案 sc = new Scanner(System.in); String src = sc.nextLine(); filesrc = new File(src);//鍵盤錄入需要傳輸的檔案路徑 open = new FileInputStream(filesrc);//建立檔案輸入流 //連線伺服器 System.out.println("請輸入伺服器的ip地址"); sc = new Scanner(System.in); String url = sc.nextLine(); // System.out.println("請輸入伺服器的埠號"); // sc = new Scanner(System.in); // int port = sc.nextInt(); socket = new Socket(url, 2017);//建立socket out = (FileOutputStream)socket.getOutputStream();//建立檔案輸出流 //開始傳送 byte[] b = new byte[1024]; int n = open.read(b);//首次傳送 // int start = (int)System.currentTimeMillis(); start = new Date();//首次時間 while (n != -1) { out.write(b, 0, n); n = open.read(b); } // int end = (int)System.currentTimeMillis(); end = new Date(); long l = Send.printSplitTime(start, end); SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00")); String hms = formatter.format(l); System.out.println( "傳送成功,耗時:" + hms); System.out.println("謝謝使用"); //關閉流 out.close(); socket.close(); open.close(); } catch (Exception e) { System.out.println("檔案路徑或者ip有誤"); } } public static long printSplitTime(Date start, Date end) { long interval = (end.getTime() - start.getTime()); //System.out.println("介面耗時:" + interval + "毫秒"); return interval; } } package com.herley; /** * * 服務端 */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.Socket; import java.net.ServerSocket; import java.util.Scanner; public class Receive { public static void main(String[] args) { File target; //接收到的檔案儲存的位置 FileOutputStream save; //將接收到的檔案寫入電腦 FileInputStream in; //讀取穿送過來的資料檔案 ServerSocket server; //伺服器 Socket socket; //套接字 //處理客戶端的請求 try { //接受前檔案的準備 System.out.print("請輸入你將要儲存檔案的地址"); System.out.println("(注意格式:檔案路徑+字尾名!)"); Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); target = new File(s); save = new FileOutputStream(target); server = new ServerSocket(2017); //服務埠 //等待客戶端的呼叫 System.out.println("正在等待客戶端的呼叫........"); socket = server.accept(); //阻塞 in = (FileInputStream)socket.getInputStream(); //接收資料 byte[] b = new byte[64]; int n = in.read(b); int start = (int)System.currentTimeMillis(); while (n != -1) { save.write(b, 0, n); //寫入指定地方 n = in.read(b); } int end = (int)System.currentTimeMillis(); System.out.println("接收成功,耗時:" + (end-start)+"毫秒"); socket.close(); server.close(); in.close(); save.close(); } catch (Exception e) { System.out.println(e); } } }