1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| package chiffrement;
import java.net.*;
import java.io.*;
import java.util.*;
public class FileSender extends Thread
{
private ServerSocket socketListener;
private Socket s;
private File fileToSend;
public FileSender(int port,File fileToSend)
{
this.fileToSend = fileToSend;
System.out.println("Fichier = "+fileToSend);
try
{
socketListener = new ServerSocket(port); //Ce ServerSocket écoute les tentatives de connexion
}
catch(IOException e)
{
System.err.println("Impossible découter sur le port.");
}
start();
}
public class FileTransfer extends Thread
{
private File fileToSend;
private Socket client;
private DataOutputStream dataOut;
private FileInputStream fis = null;
/** Creates a new instance of FileTransfer */
public FileTransfer(Socket client,File fileTosend)
{
super("ServerMultiThread");
this.client=client;
this.fileToSend = fileTosend;
try
{
dataOut = new DataOutputStream(client.getOutputStream());
}
catch(IOException ioe)
{
System.out.println("Erreur lors de la création du flux de sortie : "+ioe);
}
boolean fileExists = true;
try
{
fis = new FileInputStream(fileToSend); //Flux de lecture dans le fichier
System.out.println("FileInputStream crée = "+fis.toString());
}
catch (FileNotFoundException e)
{
fileExists = false;
}
if (fileExists)
{
//Création d"un buffer de 4K
byte[] buffer= new byte[4096];
int bytes = 0;
try
{
//Envoie du nom du fichier dans le flux de sortie
//dataOut.writeUTF(fileToSend.getName());
//Copie du fichier dans le flux de sortie
while((bytes = fis.read(buffer)) != -1)
dataOut.write(buffer, 0, bytes);
fis.close();
}
catch(IOException ioe)
{
System.out.println("Erreur d'IO : "+ioe);
}
}
}
}
public void run()
{
try
{
while(true)
{
s = socketListener.accept();
System.out.println("Socket accepté = "+s.toString());
new FileTransfer(s,fileToSend);
break;
}
}
catch(IOException ioe)
{
System.out.println("Erreur d'IO : "+ioe);
}
}
public static void main (String[] args) throws IOException{
File fichier = new File("C:/Users/workspace/Projet/src/chiffrement/fichier1.txt");
FileSender o=new FileSender(4004,fichier);
o.run();
}
} |
Partager