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
|
File f = new File(pathname);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
OutputStream os = null;
try {
os = socket.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
int[] aecrire = new int[4096];
int compteur = 0;
long ouonestrendu = 0;
int lu = -1;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
lu = bis.read();
} catch (IOException e) {
e.printStackTrace();
}
//Tant qu'on est pas à la fin fu chier
while(lu > -1) {
// On lit les données du fichier
aecrire[compteur] = lu;
try {
lu = bis.read();
} catch (IOException e) {
e.printStackTrace();
}
compteur++;
//Quand on a rempli le tableau, on envoie un paquet de 4096 octets
if(compteur == 4096){
compteur=0;
ouonestrendu++;
// On remplit le tampon
for(int x=0;x<4096;x++){
try {
bos.write(aecrire[x]);
} catch (IOException e) {
e.printStackTrace();
}
}
// Et on l'envoie
try {
os.write(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
baos.reset();
}
}
//On envoie le dernier paquet, qui ne fait pas forcément 4096 octets
// On remplit le tampon
for(int x=0 ; x<compteur ; x++){
try {
bos.write(aecrire[x]);
} catch (IOException e) {
e.printStackTrace();
}
}
//Et on l'envoie
try {
bos.flush();
os.write(baos.toByteArray());
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Youpi finished");
try {
bos.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
} |
Partager