Advertisement
cesarcardinale

M4102C - TP4_Serveur

Mar 28th, 2019
1,791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.io.*;
  3. import java.net.Socket;
  4.  
  5. public class TaskServeurFichier implements Runnable {
  6.  
  7.     Socket client;
  8.  
  9.     public TaskServeurFichier(Socket client) {
  10.         this.client = client;
  11.     }
  12.  
  13.     public void run() {
  14.  
  15.         try {
  16.  
  17.             client.setSoTimeout(20000);
  18.  
  19.             File f;
  20.             String[] paths;
  21.             f = new File("./");
  22.             paths = f.list();
  23.             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
  24.  
  25.             BufferedReader inServeur = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
  26.  
  27.             OutputStream outServeur = client.getOutputStream();
  28.  
  29.             String fileName = inServeur.readLine();
  30.  
  31.             System.out.println(fileName);
  32.  
  33.             FileInputStream file = new FileInputStream(fileName);
  34.  
  35.             byte buf[] = new byte[512];
  36.             int nbOctets;
  37.  
  38.             for (String path:paths){
  39.                 outServeur.write(("| " + path + "\n").getBytes(), 0, ("| " + path + "\n").length());
  40.             }
  41.             outServeur.write("[END]\n".getBytes(), 0, "[END]\n".length());
  42.  
  43.             while ((nbOctets = file.read(buf)) != -1) {
  44.                 outServeur.write(buf, 0, nbOctets);
  45.             }
  46.  
  47.             outServeur.close();
  48.             inServeur.close();
  49.             file.close();
  50.             client.close();
  51.  
  52.         } catch (IOException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         }
  56.  
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement