Monday, November 23, 2015

open & delete file (server)

This server receives commands from a client and deal with files.

code:

import java.awt.Desktop;
import java.io.File;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class FileSearchServer{
public static void main (String args[]){
 try {
        // Create a new server socket that listens at port 8080
        ServerSocket ServerSocket1 = new ServerSocket(8080);
        System.out.println("Server is lisenting ... ");
        Socket socket1 = ServerSocket1.accept(); //accept a connection from client on port 8080

        OutputStream outputStream1 = socket1.getOutputStream();
        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(outputStream1);
        InputStream inputStream1 = socket1.getInputStream();
        ObjectInputStream objectInputStream1 = new ObjectInputStream(inputStream1);

        String rcv_path, send_msg, storewords2;
        String storewords1 = "";

        while(true) {
           // Recieve a message from client
           System.out.println("Waiting for client to respond ... ");
           rcv_path = (String)objectInputStream1.readObject();
           if(rcv_path != null &&  !(rcv_path.startsWith("del ")) && !(rcv_path.startsWith("open "))) {
          System.out.println("From Client : " + rcv_path);
            File file1 = new File(rcv_path);
       File[] files = file1.listFiles();
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);

        for (int i = 0; i < files.length; i++){
         String mime = "";
        File file = files[i];
        System.out.println((i + 1) + ":    " + file + " *information is sent*");

        Path source = Paths.get(file.getPath());
           mime = Files.probeContentType(source);

        storewords2 = ((i + 1) + ":  " + file + "\nName:  "+ file.getName() + "\nLast modified time:  " + sdf.format(file.lastModified()) + "\nFile size:  " + getKB(file.length()) + " KB" + "\nMime type: " + mime);
        storewords1 = storewords1 + "\n" + storewords2 + "\n";
       }
            }else if( rcv_path != null && rcv_path.startsWith("del ")){
             System.out.println("From Client : " + rcv_path);
             String d_path = "";
             d_path = rcv_path.substring(4, (rcv_path.length()));
             File file1 = new File(d_path);
              if(file1.delete()){
               storewords1 = "Successfully deleted";
              } else{
               storewords1 = d_path + "\nThis file could not be deleted";
              }
            }else if( rcv_path != null && rcv_path.startsWith("open ")){
             System.out.println("From Client : " + rcv_path);
             String o_path = "";
             o_path = rcv_path.substring(5, (rcv_path.length()));
             File file1 = new File(o_path);
             Desktop desktop = Desktop.getDesktop();
             desktop.open(file1);
            }

           // Send a message to client
           System.out.print("\nTo Client : ");
           send_msg = storewords1;
           objectOutputStream1.writeObject(send_msg);
       }
     } catch(Exception e) {
        System.out.println(e);
     }
  }

 private static BigDecimal getKB(long byte1){
  double kbyte1;
  double byte2;

  byte2 = byte1;
  kbyte1 = byte2/1024;

  BigDecimal value = new BigDecimal(kbyte1);
  BigDecimal roundHalfUp1 = value.setScale(2, BigDecimal.ROUND_HALF_UP);

  return roundHalfUp1;
 }
}