Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package socketserwer;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.io.Writer;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Lukasz Laskowski
- */
- public class SocketSerwer {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- try {
- String host = "localhost"; // nazwa hosta
- int port = 8080; // numer portu
- InetSocketAddress isa = new InetSocketAddress(host, port);
- ServerSocket serverSock = new ServerSocket(); // Utworzenie gniazda serwera
- serverSock.bind(isa); // Związanie gniazda serwera z adresem hosta i portu
- boolean serverIsRunning = true;
- //lista plików
- String path = "C:\\Users\\dell\\Desktop\\Obiektowe";
- File folder = new File(path);
- while (serverIsRunning) { //pętla, w której są połączenia z klientami i kolejne operacje
- Socket connection = serverSock.accept();
- File[] listOfFiles = folder.listFiles();
- //obsługa zleceń klienta
- OutputStream sockOut = connection.getOutputStream(); //strumień wyjściowy
- InputStream sockIn = connection.getInputStream(); //strumień wejściowy
- BufferedReader in = new BufferedReader(new InputStreamReader(sockIn)); //wejście
- PrintWriter out = new PrintWriter(sockOut, true); //wyjście
- String line = in.readLine(); //odczytanie linii
- //**************************************************************
- if ("DATE".equals(line)) {
- String date = "2019-01-4";
- out.println(date);
- }
- //**************************************************************
- if ("MAKE".equals(line)) {
- out.println("Podaj nazwę nowego pliku!");
- line = in.readLine();//wczytanie nazwy pliku
- File newFile = new File(path + "\\" + line + ".txt");
- Writer wr = new FileWriter(newFile, true);
- String content;
- if (!newFile.exists()) {
- newFile.createNewFile();
- out.println("Stworzono nowy plik! Podaj co do niego dopisać!");
- line = in.readLine();//wczytanie zawartości pliku
- content = line;
- } else {
- out.println("Plik już istnieje! Podaj co do niego dopisać!");
- line = in.readLine();//wczytanie zawartości pliku
- content = line;
- }
- wr.write(content);
- wr.close();
- out.println("Pomyślnie wypełniono plik danymi!");
- }
- //**************************************************************
- if ("CHANGE".equals(line)) {
- out.println("ZMIANA NAZWY: podaj starą nazwę");
- line = in.readLine();
- File newFile = new File(path + "\\" + line + ".txt");
- if (!newFile.exists()) {
- out.println("Plik nie istnieje!");
- line = in.readLine();
- out.println("Nieudana zmiana nazwy!");
- } else {
- out.println("Podaj nową nazwę");
- line = in.readLine();
- File newFile2 = new File(path + "\\" + line + ".txt");
- boolean success = newFile.renameTo(newFile2);
- if (success)
- out.println("Udana zmiana nazwy!");
- else
- out.println("Nieudana zmiana nazwy!");
- }
- }
- //**************************************************************
- if ("DEL".equals(line)) {
- out.println("USUWANIE PLIKU: podaj nazwę pliku do usunięcia");
- line = in.readLine();
- File delFile = new File(path + "\\" + line + ".txt");
- if (!delFile.exists()) {
- out.println("Plik nie istnieje!");
- } else {
- delFile.delete();
- out.println("Plik usunięty!");
- }
- }
- //**************************************************************
- if ("LIST".equals(line)) {
- //out.println("lista_plików");
- out.println(listOfFiles.length + " plik/ów");
- for (int i = 0; i < listOfFiles.length; i++) {
- if (listOfFiles[i].isFile()) {
- out.println("File " + listOfFiles[i].getName());
- } else if (listOfFiles[i].isDirectory()) {
- out.println("Directory " + listOfFiles[i].getName());
- }
- }
- }
- connection.close();
- }
- serverSock.close(); // zamknięcie gniazda serwera
- } catch (IOException e) {
- System.out.println("Wyjątek: błąd we/wy");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement