Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package socketserwer;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.OutputStream;
  15. import java.io.PrintWriter;
  16. import java.io.Writer;
  17. import java.net.InetSocketAddress;
  18. import java.net.ServerSocket;
  19. import java.net.Socket;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22.  
  23. /**
  24.  *
  25.  * @author Lukasz Laskowski
  26.  */
  27. public class SocketSerwer {
  28.  
  29.     /**
  30.      * @param args the command line arguments
  31.      */
  32.     public static void main(String[] args) {
  33.         // TODO code application logic here
  34.         try {
  35.             String host = "localhost"; // nazwa hosta
  36.             int port = 8080; // numer portu
  37.             InetSocketAddress isa = new InetSocketAddress(host, port);
  38.             ServerSocket serverSock = new ServerSocket(); // Utworzenie gniazda serwera
  39.             serverSock.bind(isa); // Związanie gniazda serwera z adresem hosta i portu
  40.             boolean serverIsRunning = true;
  41.            
  42.             //lista plików
  43.             String path = "C:\\Users\\dell\\Desktop\\Obiektowe";
  44.             File folder = new File(path);
  45.              
  46.             while (serverIsRunning) { //pętla, w której są połączenia z klientami i kolejne operacje
  47.                 Socket connection = serverSock.accept();
  48.                
  49.                 File[] listOfFiles = folder.listFiles();
  50.                
  51.                 //obsługa zleceń klienta
  52.                 OutputStream sockOut = connection.getOutputStream(); //strumień wyjściowy
  53.                 InputStream sockIn = connection.getInputStream(); //strumień wejściowy
  54.                 BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
  55.                 PrintWriter out = new PrintWriter(sockOut, true); //wyjście
  56.                 String line = in.readLine(); //odczytanie linii
  57.                
  58.                 //**************************************************************
  59.                 if ("DATE".equals(line)) {
  60.                     String date = "2019-01-4";
  61.                     out.println(date);
  62.                 }
  63.                
  64.                 //**************************************************************
  65.                 if ("MAKE".equals(line)) {
  66.                     out.println("Podaj nazwę nowego pliku!");
  67.                    
  68.                     line = in.readLine();//wczytanie nazwy pliku
  69.                     File newFile = new File(path + "\\" + line + ".txt");
  70.                     Writer wr = new FileWriter(newFile, true);
  71.                     String content;
  72.                     if (!newFile.exists()) {
  73.                         newFile.createNewFile();
  74.                         out.println("Stworzono nowy plik! Podaj co do niego dopisać!");
  75.                         line = in.readLine();//wczytanie zawartości pliku
  76.                         content = line;
  77.                     } else {
  78.                         out.println("Plik już istnieje! Podaj co do niego dopisać!");
  79.                         line = in.readLine();//wczytanie zawartości pliku
  80.                         content = line;
  81.                     }
  82.                     wr.write(content);
  83.                     wr.close();
  84.                     out.println("Pomyślnie wypełniono plik danymi!");
  85.                 }
  86.                
  87.                 //**************************************************************
  88.                 if ("CHANGE".equals(line)) {
  89.                     out.println("ZMIANA NAZWY: podaj starą nazwę");
  90.                     line = in.readLine();
  91.                    
  92.                     File newFile = new File(path + "\\" + line + ".txt");
  93.                     if (!newFile.exists()) {
  94.                         out.println("Plik nie istnieje!");
  95.                         line = in.readLine();
  96.                         out.println("Nieudana zmiana nazwy!");    
  97.                     } else {
  98.                         out.println("Podaj nową nazwę");
  99.                         line = in.readLine();
  100.                         File newFile2 = new File(path + "\\" + line + ".txt");
  101.                         boolean success = newFile.renameTo(newFile2);
  102.                        
  103.                         if (success)
  104.                             out.println("Udana zmiana nazwy!");
  105.                         else
  106.                             out.println("Nieudana zmiana nazwy!");
  107.                     }  
  108.                 }
  109.  
  110.                 //**************************************************************
  111.                 if ("DEL".equals(line)) {
  112.                     out.println("USUWANIE PLIKU: podaj nazwę pliku do usunięcia");
  113.                     line = in.readLine();
  114.                     File delFile = new File(path + "\\" + line + ".txt");
  115.                     if (!delFile.exists()) {
  116.                         out.println("Plik nie istnieje!");
  117.                        
  118.                     } else {
  119.                        
  120.                         delFile.delete();
  121.                         out.println("Plik usunięty!");
  122.                        
  123.                     }    
  124.                 }
  125.  
  126.                 //**************************************************************
  127.                 if ("LIST".equals(line)) {
  128.                     //out.println("lista_plików");
  129.                     out.println(listOfFiles.length + " plik/ów");
  130.                     for (int i = 0; i < listOfFiles.length; i++) {
  131.                         if (listOfFiles[i].isFile()) {
  132.                             out.println("File " + listOfFiles[i].getName());
  133.                         } else if (listOfFiles[i].isDirectory()) {
  134.                             out.println("Directory " + listOfFiles[i].getName());
  135.                         }
  136.                     }
  137.                 }
  138.                 connection.close();
  139.             }
  140.             serverSock.close(); // zamknięcie gniazda serwera
  141.         } catch (IOException e) {
  142.             System.out.println("Wyjątek: błąd we/wy");
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement