Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class TransferFileClient {
- private Socket ClientSocCommands;
- private Socket ClientSocFiles;
- private BufferedReader bufferedReaderCommands;
- private BufferedWriter bufferedWriterCommands;
- private DataInputStream dinFile;
- private DataOutputStream doutFile;
- private BufferedReader br;
- String lineSep = System.getProperty("line.separator");
- public TransferFileClient(Socket socCommands, Socket socFiles) {
- try {
- ClientSocCommands = socCommands;
- ClientSocFiles = socFiles;
- bufferedReaderCommands = new BufferedReader(new InputStreamReader(ClientSocCommands.getInputStream()));
- bufferedWriterCommands = new BufferedWriter(new OutputStreamWriter(ClientSocCommands.getOutputStream()));
- dinFile = new DataInputStream(ClientSocFiles.getInputStream());
- doutFile = new DataOutputStream(ClientSocFiles.getOutputStream());
- br = new BufferedReader(new InputStreamReader(System.in));
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /*
- * Funkcja logowania klienta do serwera
- */
- public void loggingToServer() throws IOException {
- do {
- String temp;
- System.out.print("USER: ");
- temp = br.readLine();
- bufferedWriterCommands.write("USER" + " " + temp + lineSep);
- bufferedWriterCommands.flush();
- if (bufferedReaderCommands.readLine().compareTo("430") == 0) {
- System.out.println("Zla nazwa uzytkownika.");
- continue;
- }
- System.out.print("PASS: ");
- temp = br.readLine();
- bufferedWriterCommands.write("PASS" + " " + temp + lineSep);
- bufferedWriterCommands.flush();
- if (bufferedReaderCommands.readLine().compareTo("430") == 0) {
- System.out.println("Haslo niepoprawne.");
- continue;
- }
- System.out.println("Zalogowano pomyslnie");
- break;
- } while (true);
- }
- /*
- * Funkcja przesylania pliku od klienta do serwera
- */
- void SendFile() throws Exception {
- String filename;
- System.out.print("Podaj nazwe pliku: ");
- filename = br.readLine();
- File f = new File(filename);
- if (!f.exists()) {
- System.out.println("Dany plik nie istnieje...");
- return;
- }
- bufferedWriterCommands.write("STOR" + " " + filename + lineSep);
- bufferedWriterCommands.flush();
- System.out.println("Przesylanie pliku ...");
- FileInputStream fin = new FileInputStream(f);
- int ch;
- do {
- ch = fin.read();
- doutFile.writeUTF(String.valueOf(ch));
- } while (ch != -1);
- fin.close();
- System.out.println("Plik zostal pomyslnie wyslany");
- }
- /*
- * Funkcja pobierania pliku z serwera do klienta
- */
- void ReceiveFile() throws Exception {
- String fileName;
- System.out.print("Podaj nazwe pliku: ");
- fileName = br.readLine();
- bufferedWriterCommands.write("RETR" + " " + fileName + lineSep);
- bufferedWriterCommands.flush();
- if (bufferedReaderCommands.readLine().compareTo("550") == 0) {
- System.out.println("Nie znaleziono pliku na serwerze ...");
- return;
- } else {
- File f = new File(fileName);
- if (f.exists()) {
- String Option;
- System.out.println("Podany plik juz istnieje. Czy chcesz go podmienic (T/N) ?");
- Option = br.readLine();
- if (Option.compareToIgnoreCase("N") == 0) {
- bufferedWriterCommands.flush();
- return;
- }
- }
- System.out.println("Pobieranie pliku ...");
- FileOutputStream fout = new FileOutputStream(f);
- int ch;
- String temp;
- do {
- temp = dinFile.readUTF();
- ch = Integer.parseInt(temp);
- if (ch != -1) {
- fout.write(ch);
- }
- } while (ch != -1);
- fout.close();
- System.out.println("Plik pobrany pomyslnie.");
- }
- }
- /*
- * Funkcja wyswietlajaca zawartosc danego folderu
- */
- public void showFilesInDirectory() {
- try {
- String temp = "";
- temp = bufferedReaderCommands.readLine();
- while (temp.compareTo("-1") != 0) {
- System.out.println(temp);
- temp = bufferedReaderCommands.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /*
- * Funkcja tworzaca nowy folder w biezacej sciezce
- */
- public void makeDirectory() throws IOException {
- System.out.print("Podaj nazwe folderu: ");
- String temp = br.readLine();
- bufferedWriterCommands.write("MKD" + " " + temp + lineSep);
- bufferedWriterCommands.flush();
- if (bufferedReaderCommands.readLine().compareTo("200") == 0) {
- System.out.println("Utworzono folder pomyslnie");
- } else {
- System.out.println("Wystapil blad przy tworzeniu folderu");
- }
- }
- /*
- * Funkcja zmieniajaca biezaca sciezke dostepu
- */
- public void changeDirectory() throws IOException {
- System.out.print("Podaj nazwe folderu: ");
- String temp = br.readLine();
- bufferedWriterCommands.write("CWD" + " " + temp + lineSep);
- bufferedWriterCommands.flush();
- }
- /*
- * Funkcja wysietlajaca menu glowne
- */
- public void displayMenu() throws Exception {
- System.out.println("[ MENU ]");
- System.out.println("1. Przeslij plik");
- System.out.println("2. Pobierz plik");
- System.out.println("3. Dzialania na katalogach");
- System.out.println("4. Rozlacz");
- System.out.print("\n$ ");
- }
- /*
- * Funkcja pobierajaca komendy od uzytkownika do dzialania w menu glownym
- */
- public void getInput() throws Exception {
- displayMenu();
- while (true) {
- /*
- * Sprawdza czy poprawny input
- */
- int choice;
- String sChoice = br.readLine();
- if (!sChoice.matches("\\d+")) {
- choice = 7;
- } else {
- choice = Integer.parseInt(sChoice);
- }
- /*
- * Przesyla plik na serwer
- */
- if (choice == 1) {
- SendFile();
- System.out.print("$ ");
- }
- /*
- * Pobiera plik z serwera
- */
- else if (choice == 2) {
- ReceiveFile();
- System.out.print("$ ");
- }
- /*
- * Operacje na folderach
- */
- else if (choice == 3) {
- directory();
- }
- /*
- * Rozlacza z serwerem i wylacza go
- */
- else if (choice == 4) {
- bufferedWriterCommands.write("QUIT" + lineSep);
- bufferedWriterCommands.flush();
- System.out.println("Zostales pomyslnie rozlaczony.");
- System.exit(1);
- } else {
- System.out.print("Nieprawidlowy wybor \n$");
- }
- }
- }
- /*
- * Funkcja wyswietlajaca menu dzialan na katalogach
- */
- public void displayDirMenu() {
- System.out.println("[ MENU ]");
- System.out.println("[DZIALANIA NA KATALOGACH]");
- System.out.println("1. Pokaz sciezke dostepu");
- System.out.println("2. Pokaz zawartosc folderu");
- System.out.println("3. Przejdz do wybranego folderu");
- System.out.println("4. Przejdz do starszego folderu");
- System.out.println("5. Dodaj folder");
- System.out.println("6. Wroc do glownego menu");
- System.out.print("\n$ ");
- }
- /*
- * Funkcja pobierajaca input od uzytkownika odnosnie dzialania na katalogach
- */
- public void directory() throws Exception {
- displayDirMenu();
- while (true) {
- /*
- * Sprawdza czy poprawny input
- */
- int choice;
- String sChoice = br.readLine();
- if (!sChoice.matches("\\d+")) {
- choice = 7;
- } else {
- choice = Integer.parseInt(sChoice);
- }
- if (choice == 1)
- /*
- * Pokazuje sciezke do katalogu
- */
- {
- bufferedWriterCommands.write("XPWD" + lineSep);
- bufferedWriterCommands.flush();
- System.out.println("\t" + bufferedReaderCommands.readLine());
- System.out.print("$ ");
- } else if (choice == 2)
- /*
- * Wyswietla zawartosc danego folderu
- */
- {
- bufferedWriterCommands.write("NLST" + lineSep);
- bufferedWriterCommands.flush();
- showFilesInDirectory();
- System.out.print("$ ");
- } else if (choice == 3)
- /*
- * Zmienia folder na inny z biezacej sciezki
- */
- {
- changeDirectory();
- System.out.print("$ ");
- } else if (choice == 4)
- /*
- * Cofa sciezke do rodzica
- */
- {
- bufferedWriterCommands.write("XCUP" + lineSep);
- bufferedWriterCommands.flush();
- System.out.print("$ ");
- } else if (choice == 5)
- /*
- * Tworzy nowy folder
- */
- {
- makeDirectory();
- System.out.print("$ ");
- } else if (choice == 6) {
- getInput();
- } else {
- System.out.print("Nieprawidlowy wybor \n$");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement