Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class Serwer
- {
- public static void main (String[] args ) throws IOException
- {
- ServerSocket socket = new ServerSocket(1342);
- ExecutorService service = Executors.newFixedThreadPool(10);
- String [] folders = {"A","B","C","D","E"};
- String Serwer="A";
- int x;
- while(true)
- {
- Socket connection = socket.accept();
- System.out.println("NOWY PLIK");
- String dirPath ="C:\\Users\\andrze\\Desktop\\Serwer\\";
- int min=pick_folder(dirPath+folders[0]);
- for(int i= 0;i<5;i++)
- {
- x=pick_folder(dirPath+folders[i]);
- if(min>=x)
- {
- Serwer=folders[i];
- min=x;
- }
- }
- new Thread(new File_Download(connection,dirPath+Serwer)).start();
- }
- }
- static int pick_folder(String directory)
- {
- File[] in = new File(directory).listFiles();
- return in.length;
- }
- }
- class File_Download extends Thread
- {
- final Socket socket;
- final String dirPath;
- public File_Download(Socket s,String Directory)
- {
- this.socket=s;
- this.dirPath=Directory;
- }
- public void run()
- {
- try {
- BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
- DataInputStream dis = new DataInputStream(bis);
- long fileLength = dis.readLong();
- System.out.println(fileLength);
- String fileName = dis.readUTF();
- File file = new File(dirPath + "/" + fileName);
- FileOutputStream fos = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- for (int j = 0; j < fileLength; j++) bos.write(bis.read());
- bos.close();
- }catch(Exception e)
- {
- getStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement