Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.60 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class FileBrowserClient {
  5.     public static void main(String[] args) throws IOException {
  6.         String host = args[0];
  7.  
  8.        
  9.         Socket socket = null;
  10.         PrintWriter out = null;
  11.         BufferedReader in = null;
  12.  
  13.         try {
  14.             socket = new Socket(host, port);
  15.             out = new PrintWriter(socket.getOutputStream(), true);
  16.             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  17.         } catch(UnknownHostException e) {
  18.             System.err.println("Cannot find the host: " + host);
  19.             System.err.println("Error: " + e);
  20.         } catch(IOException e) {
  21.             System.err.println("Couldn't get I/O for the connection to: " + host + ".");
  22.             System.err.println("Error: " + e);
  23.         }
  24.  
  25.         InetAddress i = InetAddress.getLocalHost();
  26.         String user = System.getProperty("user.name");
  27.         out.println(user);
  28.         out.println(i.getHostAddress()); // IP address only
  29.  
  30.         BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
  31.         String password = "", inputLine = "";
  32.         boolean loggedIn = false;
  33.  
  34.         System.out.println(in.readLine());
  35.         while(loggedIn == false) {
  36.             password = userIn.readLine();
  37.             out.println(password);
  38.             if(password.equalsIgnoreCase("logout")) {
  39.                 break;
  40.             } else {
  41.                 while(!(inputLine = in.readLine()).equals("")) {
  42.                     if(inputLine.equalsIgnoreCase("Password accepted.")) {
  43.                         loggedIn = true;
  44.                     }
  45.                     if(inputLine.equalsIgnoreCase("C:\\>")) {
  46.                         System.out.print("\n" + inputLine);
  47.                         break;
  48.                     } else {
  49.                         System.out.println(inputLine);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.  
  55.         if(!password.equalsIgnoreCase("logout")) {
  56.             String outputLine, fileInfo;
  57.             while(!(outputLine = userIn.readLine()).equalsIgnoreCase("logout")) {
  58.                 out.println(outputLine);
  59.                 if(outputLine.startsWith("scp ")) {
  60.                     if(outputLine.equalsIgnoreCase("scp dir")) {
  61.                         int numFiles = Integer.parseInt(in.readLine());
  62.                         for(int j=0; j<numFiles; j++) {
  63.                             System.out.println("Starting file " + (j + 1));
  64.                             fileInfo = in.readLine();
  65.                             System.out.println(fileInfo);
  66.                             String fileName = fileInfo.substring(7, fileInfo.lastIndexOf(">"));
  67.                             long fileSize = Integer.parseInt(fileInfo.substring(fileInfo.lastIndexOf("<") + 1, fileInfo.length()));
  68.                             System.out.println("File: <" + fileName + "> Size: <" + fileSize + ">");
  69.                             receiveFile(fileName, fileSize, socket);
  70.                         }
  71.                     } else {
  72.                         String fileSize = in.readLine();
  73.                         if(fileSize.startsWith("Error 404")) {
  74.                             System.out.println(fileSize);
  75.                         } else {
  76.                             long filesize = Integer.parseInt(fileSize.substring(fileSize.indexOf(": ")+2));
  77.                             receiveFile(outputLine.substring(outputLine.indexOf(" ") + 1), filesize, socket);
  78.                         }
  79.                     }
  80.                 }
  81.                 while(!(inputLine = in.readLine()).equals("")) {
  82.                     System.out.print("\n" + inputLine);
  83.                 }
  84.             }
  85.             out.println("logout");
  86.         }
  87.  
  88.         out.close();
  89.         in.close();
  90.         userIn.close();
  91.         socket.close();
  92.     }
  93.  
  94.     public static void receiveFile(String fileName, long fileSize, Socket sock) throws IOException {
  95.         BufferedInputStream is = new BufferedInputStream(sock.getInputStream());
  96.         is.mark(0);
  97.  
  98.         System.out.println("File '" + fileName + "' has been found on the server and is " + fileSize + " bytes.");
  99.  
  100.         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName)); // BOS writes to a file created using FOS
  101.  
  102.         int arrayLength = 5000000, total = 0, bytesRead = 0, current = 0, difference = 0;
  103.         if(fileSize < arrayLength) {
  104.             arrayLength = (int)fileSize;
  105.         }
  106.         byte[] myByteArray = new byte[arrayLength];
  107.         byte[] finalArray;
  108.  
  109.         long start = System.currentTimeMillis();
  110.         while(total < fileSize) {
  111.             is.reset();
  112.  
  113.             difference = (int)fileSize - total;
  114.             total = total + arrayLength;
  115.  
  116.             if(difference < arrayLength) {
  117.                 total = (int)fileSize;
  118.                 finalArray = new byte[difference];
  119.                 bytesRead = is.read(finalArray, 0, difference);
  120.                 current = bytesRead;
  121.                 do {
  122.                     bytesRead = is.read(finalArray, current, (difference-current));
  123.                     if(bytesRead >= 0)
  124.                         current += bytesRead;
  125.                 } while(bytesRead != 0);
  126.  
  127.                 bos.write(finalArray, 0, difference);
  128.             } else {
  129.                 bytesRead = is.read(myByteArray, 0, arrayLength);
  130.                 current = bytesRead;
  131.                 do {
  132.                     bytesRead = is.read(myByteArray, current, (arrayLength-current));
  133.                     if(bytesRead >= 0)
  134.                         current += bytesRead;
  135.                 } while(bytesRead != 0);
  136.  
  137.                 bos.write(myByteArray, 0, arrayLength);
  138.                 is.mark(total);
  139.             }
  140.         }
  141.  
  142.         bos.flush();
  143.         bos.close();
  144.  
  145.         long end = System.currentTimeMillis();
  146.         System.out.println("Transfer complete in " + ((end - start) / 1000) + " seconds.");
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement