Advertisement
Guest User

Untitled

a guest
May 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class TransferFileClient {
  5.     private Socket ClientSocCommands;
  6.     private Socket ClientSocFiles;
  7.  
  8.     private BufferedReader bufferedReaderCommands;
  9.     private BufferedWriter bufferedWriterCommands;
  10.     private DataInputStream dinFile;
  11.     private DataOutputStream doutFile;
  12.     private BufferedReader br;
  13.     String lineSep = System.getProperty("line.separator");
  14.  
  15.     public TransferFileClient(Socket socCommands, Socket socFiles) {
  16.         try {
  17.             ClientSocCommands = socCommands;
  18.             ClientSocFiles = socFiles;
  19.             bufferedReaderCommands = new BufferedReader(new InputStreamReader(ClientSocCommands.getInputStream()));
  20.             bufferedWriterCommands = new BufferedWriter(new OutputStreamWriter(ClientSocCommands.getOutputStream()));
  21.             dinFile = new DataInputStream(ClientSocFiles.getInputStream());
  22.             doutFile = new DataOutputStream(ClientSocFiles.getOutputStream());
  23.             br = new BufferedReader(new InputStreamReader(System.in));
  24.         } catch (Exception ex) {
  25.             ex.printStackTrace();
  26.         }
  27.     }
  28.  
  29.     /*
  30.      * Funkcja logowania klienta do serwera
  31.      */
  32.     public void loggingToServer() throws IOException {
  33.  
  34.         do {
  35.             String temp;
  36.             System.out.print("USER: ");
  37.             temp = br.readLine();
  38.             bufferedWriterCommands.write("USER" + " " + temp + lineSep);
  39.             bufferedWriterCommands.flush();
  40.             if (bufferedReaderCommands.readLine().compareTo("430") == 0) {
  41.                 System.out.println("Zla nazwa uzytkownika.");
  42.                 continue;
  43.             }
  44.  
  45.             System.out.print("PASS: ");
  46.             temp = br.readLine();
  47.             bufferedWriterCommands.write("PASS" + " " + temp + lineSep);
  48.             bufferedWriterCommands.flush();
  49.  
  50.             if (bufferedReaderCommands.readLine().compareTo("430") == 0) {
  51.                 System.out.println("Haslo niepoprawne.");
  52.                 continue;
  53.             }
  54.             System.out.println("Zalogowano pomyslnie");
  55.             break;
  56.         } while (true);
  57.     }
  58.  
  59.     /*
  60.      * Funkcja przesylania pliku od klienta do serwera
  61.      */
  62.     void SendFile() throws Exception {
  63.  
  64.         String filename;
  65.         System.out.print("Podaj nazwe pliku: ");
  66.         filename = br.readLine();
  67.  
  68.         File f = new File(filename);
  69.         if (!f.exists()) {
  70.             System.out.println("Dany plik nie istnieje...");
  71.             return;
  72.         }
  73.  
  74.         bufferedWriterCommands.write("STOR" + " " + filename + lineSep);
  75.         bufferedWriterCommands.flush();
  76.  
  77.         System.out.println("Przesylanie pliku ...");
  78.         FileInputStream fin = new FileInputStream(f);
  79.         int ch;
  80.         do {
  81.             ch = fin.read();
  82.             doutFile.writeUTF(String.valueOf(ch));
  83.         } while (ch != -1);
  84.         fin.close();
  85.         System.out.println("Plik zostal pomyslnie wyslany");
  86.     }
  87.  
  88.     /*
  89.      * Funkcja pobierania pliku z serwera do klienta
  90.      */
  91.     void ReceiveFile() throws Exception {
  92.         String fileName;
  93.         System.out.print("Podaj nazwe pliku: ");
  94.         fileName = br.readLine();
  95.         bufferedWriterCommands.write("RETR" + " " + fileName + lineSep);
  96.         bufferedWriterCommands.flush();
  97.         if (bufferedReaderCommands.readLine().compareTo("550") == 0) {
  98.             System.out.println("Nie znaleziono pliku na serwerze ...");
  99.             return;
  100.         } else {
  101.             File f = new File(fileName);
  102.             if (f.exists()) {
  103.                 String Option;
  104.                 System.out.println("Podany plik juz istnieje. Czy chcesz go podmienic (T/N) ?");
  105.                 Option = br.readLine();
  106.                 if (Option.compareToIgnoreCase("N") == 0) {
  107.                     bufferedWriterCommands.flush();
  108.                     return;
  109.                 }
  110.             }
  111.             System.out.println("Pobieranie pliku ...");
  112.             FileOutputStream fout = new FileOutputStream(f);
  113.             int ch;
  114.             String temp;
  115.             do {
  116.                 temp = dinFile.readUTF();
  117.                 ch = Integer.parseInt(temp);
  118.                 if (ch != -1) {
  119.                     fout.write(ch);
  120.                 }
  121.             } while (ch != -1);
  122.             fout.close();
  123.             System.out.println("Plik pobrany pomyslnie.");
  124.         }
  125.     }
  126.  
  127.     /*
  128.      * Funkcja wyswietlajaca zawartosc danego folderu
  129.      */
  130.     public void showFilesInDirectory() {
  131.         try {
  132.             String temp = "";
  133.             temp = bufferedReaderCommands.readLine();
  134.             while (temp.compareTo("-1") != 0) {
  135.                 System.out.println(temp);
  136.                 temp = bufferedReaderCommands.readLine();
  137.             }
  138.         } catch (IOException e) {
  139.             e.printStackTrace();
  140.         }
  141.     }
  142.  
  143.     /*
  144.      * Funkcja tworzaca nowy folder w biezacej sciezce
  145.      */
  146.     public void makeDirectory() throws IOException {
  147.         System.out.print("Podaj nazwe folderu: ");
  148.         String temp = br.readLine();
  149.         bufferedWriterCommands.write("MKD" + " " + temp + lineSep);
  150.         bufferedWriterCommands.flush();
  151.         if (bufferedReaderCommands.readLine().compareTo("200") == 0) {
  152.             System.out.println("Utworzono folder pomyslnie");
  153.         } else {
  154.             System.out.println("Wystapil blad przy tworzeniu folderu");
  155.         }
  156.     }
  157.  
  158.     /*
  159.      * Funkcja zmieniajaca biezaca sciezke dostepu
  160.      */
  161.     public void changeDirectory() throws IOException {
  162.         System.out.print("Podaj nazwe folderu: ");
  163.         String temp = br.readLine();
  164.         bufferedWriterCommands.write("CWD" + " " + temp + lineSep);
  165.         bufferedWriterCommands.flush();
  166.     }
  167.  
  168.     /*
  169.      * Funkcja wysietlajaca menu glowne
  170.      */
  171.     public void displayMenu() throws Exception {
  172.         System.out.println("[ MENU ]");
  173.         System.out.println("1. Przeslij plik");
  174.         System.out.println("2. Pobierz plik");
  175.         System.out.println("3. Dzialania na katalogach");
  176.         System.out.println("4. Rozlacz");
  177.         System.out.print("\n$ ");
  178.     }
  179.  
  180.     /*
  181.      * Funkcja pobierajaca komendy od uzytkownika do dzialania w menu glownym
  182.      */
  183.     public void getInput() throws Exception {
  184.         displayMenu();
  185.         while (true) {
  186.             /*
  187.              * Sprawdza czy poprawny input
  188.              */
  189.             int choice;
  190.             String sChoice = br.readLine();
  191.             if (!sChoice.matches("\\d+")) {
  192.                 choice = 7;
  193.             } else {
  194.                 choice = Integer.parseInt(sChoice);
  195.             }
  196.             /*
  197.              * Przesyla plik na serwer
  198.              */
  199.             if (choice == 1) {
  200.                 SendFile();
  201.                 System.out.print("$ ");
  202.             }
  203.             /*
  204.              * Pobiera plik z serwera
  205.              */
  206.             else if (choice == 2) {
  207.                 ReceiveFile();
  208.                 System.out.print("$ ");
  209.             }
  210.             /*
  211.              * Operacje na folderach
  212.              */
  213.             else if (choice == 3) {
  214.                 directory();
  215.             }
  216.             /*
  217.              * Rozlacza z serwerem i wylacza go
  218.              */
  219.             else if (choice == 4) {
  220.                 bufferedWriterCommands.write("QUIT" + lineSep);
  221.                 bufferedWriterCommands.flush();
  222.                 System.out.println("Zostales pomyslnie rozlaczony.");
  223.                 System.exit(1);
  224.             } else {
  225.                 System.out.print("Nieprawidlowy wybor \n$");
  226.             }
  227.         }
  228.     }
  229.  
  230.     /*
  231.      * Funkcja wyswietlajaca menu dzialan na katalogach
  232.      */
  233.     public void displayDirMenu() {
  234.         System.out.println("[ MENU ]");
  235.         System.out.println("[DZIALANIA NA KATALOGACH]");
  236.         System.out.println("1. Pokaz sciezke dostepu");
  237.         System.out.println("2. Pokaz zawartosc folderu");
  238.         System.out.println("3. Przejdz do wybranego folderu");
  239.         System.out.println("4. Przejdz do starszego folderu");
  240.         System.out.println("5. Dodaj folder");
  241.         System.out.println("6. Wroc do glownego menu");
  242.         System.out.print("\n$ ");
  243.     }
  244.  
  245.     /*
  246.      * Funkcja pobierajaca input od uzytkownika odnosnie dzialania na katalogach
  247.      */
  248.     public void directory() throws Exception {
  249.         displayDirMenu();
  250.         while (true) {
  251.             /*
  252.              * Sprawdza czy poprawny input
  253.              */
  254.             int choice;
  255.             String sChoice = br.readLine();
  256.             if (!sChoice.matches("\\d+")) {
  257.                 choice = 7;
  258.             } else {
  259.                 choice = Integer.parseInt(sChoice);
  260.             }
  261.             if (choice == 1)
  262.             /*
  263.              * Pokazuje sciezke do katalogu
  264.              */
  265.             {
  266.                 bufferedWriterCommands.write("XPWD" + lineSep);
  267.                 bufferedWriterCommands.flush();
  268.                 System.out.println("\t" + bufferedReaderCommands.readLine());
  269.                 System.out.print("$ ");
  270.             } else if (choice == 2)
  271.             /*
  272.              * Wyswietla zawartosc danego folderu
  273.              */
  274.             {
  275.                 bufferedWriterCommands.write("NLST" + lineSep);
  276.                 bufferedWriterCommands.flush();
  277.                 showFilesInDirectory();
  278.                 System.out.print("$ ");
  279.             } else if (choice == 3)
  280.             /*
  281.              * Zmienia folder na inny z biezacej sciezki
  282.              */
  283.             {
  284.                 changeDirectory();
  285.                 System.out.print("$ ");
  286.             } else if (choice == 4)
  287.             /*
  288.              * Cofa sciezke do rodzica
  289.              */
  290.             {
  291.                 bufferedWriterCommands.write("XCUP" + lineSep);
  292.                 bufferedWriterCommands.flush();
  293.                 System.out.print("$ ");
  294.             } else if (choice == 5)
  295.             /*
  296.              * Tworzy nowy folder
  297.              */
  298.             {
  299.                 makeDirectory();
  300.                 System.out.print("$ ");
  301.             } else if (choice == 6) {
  302.                 getInput();
  303.             } else {
  304.                 System.out.print("Nieprawidlowy wybor \n$");
  305.             }
  306.         }
  307.     }
  308.  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement