Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7.  
  8. public class Client {
  9.  
  10.     File file=null;
  11.     public static void main(String[] args) throws IOException
  12.     {
  13.         InetAddress ip = InetAddress.getByName("localhost");
  14.         List<String> user_files = new ArrayList<String>();
  15.         String directory = "C:\\Users\\andrze\\Desktop\\uzytkownik1";
  16.  
  17.         ExecutorService executors = Executors.newFixedThreadPool(10);
  18.         while (true) {
  19.             String f=File_Handler.check_changes_in_folder(directory,user_files);
  20.  
  21.             if(!f.equals("new_files_not_found")) {
  22.                 Socket socket = new Socket(ip, 1342);
  23.  
  24.                 Runnable task = new Send_File(socket,f,directory);
  25.                 executors.execute(task);
  26.             }
  27.         }
  28.  
  29.     }
  30. }
  31.  
  32. class File_Handler
  33. {
  34.  
  35.     static String check_changes_in_folder(String user_directory,List<String>user_files )
  36.     {
  37.         File[] in = new File(user_directory).listFiles();
  38.  
  39.         for(int i=0;i< in.length;i++)
  40.         {
  41.             String curr_f = in[i].getName();
  42.             if(!user_files.contains(curr_f))
  43.             {
  44.                 user_files.add(curr_f);
  45.                 return curr_f;
  46.             }
  47.         }
  48.         return "new_files_not_found";
  49.     }
  50.  
  51.  
  52. }
  53.  
  54. class Send_File extends Thread{
  55.     final Socket s;
  56.     final String directory;
  57.     final String f;
  58.  
  59.     public Send_File(Socket s,String f,String directory)
  60.     {
  61.         this.s = s;
  62.         this.directory= directory;
  63.         this.f=f;
  64.     }
  65.  
  66.     public void run()
  67.     {
  68.         System.out.println("NEW THREAD"+ Thread.currentThread().getName());
  69.         try {
  70.             BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
  71.             DataOutputStream dos = new DataOutputStream(bos);
  72.             File file = new File(directory + "\\" + f);
  73.             long length = file.length();
  74.             dos.writeLong(length);
  75.             String name = file.getName();
  76.             dos.writeUTF(name);
  77.  
  78.             FileInputStream fis = new FileInputStream(file);
  79.             BufferedInputStream bis = new BufferedInputStream(fis);
  80.             int theByte = 0;
  81.             while ((theByte = bis.read()) != -1)
  82.             {
  83.                 bos.write(theByte);
  84.             }
  85.             bis.close();
  86.         }catch(Exception e) {
  87.             System.out.println(e);
  88.         }
  89.         finally {
  90.             try {
  91.                 s.close();
  92.             }catch(Exception f)
  93.             {
  94.                 System.out.println(f);
  95.             }
  96.         }
  97.  
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement