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
|
public class Codage {
public static byte[] readFile(String filename) throws IOException {
File file = new File(filename);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int bytes = (int) file.length();
byte[] buffer = new byte[bytes];
int readBytes = bis.read(buffer);
bis.close();
return buffer;
}
// Méthode de création de la chaîne de caractères reprenant l'image
// NB : le nom du fichier doit être complet (ie chemin relatif ou absolu et
// extension)
public static String coder(String fichier) throws Exception {
byte[] buffer = readFile(fichier);
String encode = Base64.encode(buffer);
return encode;
}
public static void writeFile(byte[] tab, String fichier)
throws IOException {
File file = new File(fichier);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(tab);
bos.close();
}
// Méthode pour décoder l'image
// NB : la chaîne de caractères est le code Base64, le nom du fichier doit
// être complet (ie chemin relatif ou absolu et extension)
public static void decoder(String image, String fichier) throws Exception {
byte[] buffer = Base64.decode(image);
writeFile(buffer, fichier);
}
} |
Partager