Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class Serwer
  7. {
  8.  
  9.     public static void main (String[] args ) throws IOException
  10.     {
  11.         ServerSocket socket = new ServerSocket(1342);
  12.         ExecutorService service = Executors.newFixedThreadPool(10);
  13.         String [] folders = {"A","B","C","D","E"};
  14.         String Serwer="A";
  15.  
  16.         int x;
  17.         while(true)
  18.         {
  19.  
  20.             Socket connection = socket.accept();
  21.             System.out.println("NOWY PLIK");
  22.             String dirPath ="C:\\Users\\andrze\\Desktop\\Serwer\\";
  23.             int min=pick_folder(dirPath+folders[0]);
  24.             for(int i= 0;i<5;i++)
  25.             {
  26.                 x=pick_folder(dirPath+folders[i]);
  27.                 if(min>=x)
  28.                 {
  29.                     Serwer=folders[i];
  30.                     min=x;
  31.                 }
  32.  
  33.             }
  34.  
  35.  
  36.             new Thread(new File_Download(connection,dirPath+Serwer)).start();
  37.         }
  38.  
  39.  
  40.  
  41.  
  42.         }
  43.     static int pick_folder(String directory)
  44.     {
  45.         File[] in = new File(directory).listFiles();
  46.  
  47.         return in.length;
  48.     }
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. class File_Download extends Thread
  57. {
  58.     final Socket socket;
  59.     final String dirPath;
  60.  
  61.     public File_Download(Socket s,String Directory)
  62.     {
  63.         this.socket=s;
  64.         this.dirPath=Directory;
  65.  
  66.     }
  67.  
  68.     public void run()
  69.     {
  70.  
  71.  
  72.             try {
  73.                 BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
  74.                 DataInputStream dis = new DataInputStream(bis);
  75.  
  76.                 long fileLength = dis.readLong();
  77.                 System.out.println(fileLength);
  78.                 String fileName = dis.readUTF();
  79.  
  80.                 File file = new File(dirPath + "/" + fileName);
  81.  
  82.                 FileOutputStream fos = new FileOutputStream(file);
  83.                 BufferedOutputStream bos = new BufferedOutputStream(fos);
  84.  
  85.                 for (int j = 0; j < fileLength; j++) bos.write(bis.read());
  86.  
  87.                 bos.close();
  88.             }catch(Exception e)
  89.             {
  90.                 getStackTrace();
  91.             }
  92.  
  93.     }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement