In the html (jsp) of register I have a field that the user registers his photo getting the tag img
with the following result.
<img src="data:image/png;Base64,/9/0kmkdmkewnsjdncijndcjdxncdjcdscjnccc/ccnkjdncnsjnidckcmokcmoskcmkosmcokmdscjncjncjcsnckdjncojnscjncdjnckmkdmcokscmksmdckcmsokcmnjncjemncmokkjcmdjsdccmslckjplqmkmokswkmxokamlamklmockmclmdkcmlodkcmokcmkmcokmsomcksmcsfhhgfrfcghjkuytredfhjolkjhsSWswjshuhuwhuhyhuimcoskcmkmsckmkmkcmc"
/>
I am converting the excerpt Base64,
to a byte [] with sun.misc.BASE64Decoder, as below:
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
/**
* @author Tiago Ferezin
*
* para o tratamento da imagem
*
*/
public class Imagem {
public static byte[] convertBase64StringToByteArray(String imagem){
byte[] retorno = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
retorno = decoder.decodeBuffer(imagem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return retorno;
}
public static String convertByteArrayToBase64String(byte[] imagem){
String retorno="";
try {
BASE64Encoder encoder = new BASE64Encoder();
retorno = encoder.encode(imagem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return retorno;
}
}
And save the generated byte to the bank.
First I wanted to know how before writing to the bank, get the image type (png, ttf, jpg, psd) and convert it to JPEG
, and then generate an array of it and write to the bank?
Second, I would like to know how to convert this byte [] stored in the database to a String data:image/jpeg;Base64,{byte[]}
to put in a img
tag on another JSP page?