I was trying to send the images in servlet to ajax call but its showing some characters in my browser instead.
my question is how to encode servlet response in base64
.
I have an image as response.
Servlet code :
response.setContentType("image/jpeg");
ServletOutputStream out;
out = response.getOutputStream();
FileInputStream fin = new FileInputStream("D:\\Astro\\html5up-arcana (1)\\images\\1.jpg");
BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(out);
int ch =0; ;
while((ch=bin.read())!=-1) {
bout.write(ch);
}
Ajax code :
$(document).ready(function() {
$('#nxt').click(function() {
$.ajax({
url : 'DisplayImage',
data : {
userName : 'Hi'
},
success : function(result) {
$('#gallery-overlay').html('<img src="data:image/jpeg;base64,'+result+'"/>');
}
});
});
});