Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class Client {
- File file=null;
- public static void main(String[] args) throws IOException
- {
- InetAddress ip = InetAddress.getByName("localhost");
- List<String> user_files = new ArrayList<String>();
- String directory = "C:\\Users\\andrze\\Desktop\\uzytkownik1";
- ExecutorService executors = Executors.newFixedThreadPool(10);
- while (true) {
- String f=File_Handler.check_changes_in_folder(directory,user_files);
- if(!f.equals("new_files_not_found")) {
- Socket socket = new Socket(ip, 1342);
- Runnable task = new Send_File(socket,f,directory);
- executors.execute(task);
- }
- }
- }
- }
- class File_Handler
- {
- static String check_changes_in_folder(String user_directory,List<String>user_files )
- {
- File[] in = new File(user_directory).listFiles();
- for(int i=0;i< in.length;i++)
- {
- String curr_f = in[i].getName();
- if(!user_files.contains(curr_f))
- {
- user_files.add(curr_f);
- return curr_f;
- }
- }
- return "new_files_not_found";
- }
- }
- class Send_File extends Thread{
- final Socket s;
- final String directory;
- final String f;
- public Send_File(Socket s,String f,String directory)
- {
- this.s = s;
- this.directory= directory;
- this.f=f;
- }
- public void run()
- {
- System.out.println("NEW THREAD"+ Thread.currentThread().getName());
- try {
- BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
- DataOutputStream dos = new DataOutputStream(bos);
- File file = new File(directory + "\\" + f);
- long length = file.length();
- dos.writeLong(length);
- String name = file.getName();
- dos.writeUTF(name);
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- int theByte = 0;
- while ((theByte = bis.read()) != -1)
- {
- bos.write(theByte);
- }
- bis.close();
- }catch(Exception e) {
- System.out.println(e);
- }
- finally {
- try {
- s.close();
- }catch(Exception f)
- {
- System.out.println(f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement