Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class FileBrowserClient {
- public static void main(String[] args) throws IOException {
- String host = args[0];
- Socket socket = null;
- PrintWriter out = null;
- BufferedReader in = null;
- try {
- socket = new Socket(host, port);
- out = new PrintWriter(socket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- } catch(UnknownHostException e) {
- System.err.println("Cannot find the host: " + host);
- System.err.println("Error: " + e);
- } catch(IOException e) {
- System.err.println("Couldn't get I/O for the connection to: " + host + ".");
- System.err.println("Error: " + e);
- }
- InetAddress i = InetAddress.getLocalHost();
- String user = System.getProperty("user.name");
- out.println(user);
- out.println(i.getHostAddress()); // IP address only
- BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
- String password = "", inputLine = "";
- boolean loggedIn = false;
- System.out.println(in.readLine());
- while(loggedIn == false) {
- password = userIn.readLine();
- out.println(password);
- if(password.equalsIgnoreCase("logout")) {
- break;
- } else {
- while(!(inputLine = in.readLine()).equals("")) {
- if(inputLine.equalsIgnoreCase("Password accepted.")) {
- loggedIn = true;
- }
- if(inputLine.equalsIgnoreCase("C:\\>")) {
- System.out.print("\n" + inputLine);
- break;
- } else {
- System.out.println(inputLine);
- }
- }
- }
- }
- if(!password.equalsIgnoreCase("logout")) {
- String outputLine, fileInfo;
- while(!(outputLine = userIn.readLine()).equalsIgnoreCase("logout")) {
- out.println(outputLine);
- if(outputLine.startsWith("scp ")) {
- if(outputLine.equalsIgnoreCase("scp dir")) {
- int numFiles = Integer.parseInt(in.readLine());
- for(int j=0; j<numFiles; j++) {
- System.out.println("Starting file " + (j + 1));
- fileInfo = in.readLine();
- System.out.println(fileInfo);
- String fileName = fileInfo.substring(7, fileInfo.lastIndexOf(">"));
- long fileSize = Integer.parseInt(fileInfo.substring(fileInfo.lastIndexOf("<") + 1, fileInfo.length()));
- System.out.println("File: <" + fileName + "> Size: <" + fileSize + ">");
- receiveFile(fileName, fileSize, socket);
- }
- } else {
- String fileSize = in.readLine();
- if(fileSize.startsWith("Error 404")) {
- System.out.println(fileSize);
- } else {
- long filesize = Integer.parseInt(fileSize.substring(fileSize.indexOf(": ")+2));
- receiveFile(outputLine.substring(outputLine.indexOf(" ") + 1), filesize, socket);
- }
- }
- }
- while(!(inputLine = in.readLine()).equals("")) {
- System.out.print("\n" + inputLine);
- }
- }
- out.println("logout");
- }
- out.close();
- in.close();
- userIn.close();
- socket.close();
- }
- public static void receiveFile(String fileName, long fileSize, Socket sock) throws IOException {
- BufferedInputStream is = new BufferedInputStream(sock.getInputStream());
- is.mark(0);
- System.out.println("File '" + fileName + "' has been found on the server and is " + fileSize + " bytes.");
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName)); // BOS writes to a file created using FOS
- int arrayLength = 5000000, total = 0, bytesRead = 0, current = 0, difference = 0;
- if(fileSize < arrayLength) {
- arrayLength = (int)fileSize;
- }
- byte[] myByteArray = new byte[arrayLength];
- byte[] finalArray;
- long start = System.currentTimeMillis();
- while(total < fileSize) {
- is.reset();
- difference = (int)fileSize - total;
- total = total + arrayLength;
- if(difference < arrayLength) {
- total = (int)fileSize;
- finalArray = new byte[difference];
- bytesRead = is.read(finalArray, 0, difference);
- current = bytesRead;
- do {
- bytesRead = is.read(finalArray, current, (difference-current));
- if(bytesRead >= 0)
- current += bytesRead;
- } while(bytesRead != 0);
- bos.write(finalArray, 0, difference);
- } else {
- bytesRead = is.read(myByteArray, 0, arrayLength);
- current = bytesRead;
- do {
- bytesRead = is.read(myByteArray, current, (arrayLength-current));
- if(bytesRead >= 0)
- current += bytesRead;
- } while(bytesRead != 0);
- bos.write(myByteArray, 0, arrayLength);
- is.mark(total);
- }
- }
- bos.flush();
- bos.close();
- long end = System.currentTimeMillis();
- System.out.println("Transfer complete in " + ((end - start) / 1000) + " seconds.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement