I have stored my image as mediumblob in database
In my Image bean class I have store photo property as byte[] like
private byte[] photo;
// getter and setter method for photo
I get image from database using to store in Image bean class
image.setPhoto(resultset.getBinaryStreams(1));
then i get the image in Servlet as:
InputStream input = null;
OutputStream output = null;
try {
input = new ByteArrayInputStream(image.getPhoto());
output = // What type of stream should I use here
byte[] buffer = new byte[10240];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
output.flush();
input.close();
}
Questions:
- WHat should I write at output line to show photo using response.getOutputStream or something else?
- Is this method correct or is there any better way?
Your
outputstream can be that stream returned byresponse.getOutputStream()directly. Just be sure to flush the stream after writing all the data to it.If your servlet container isn’t buffering output streams, you can consider wrapping the output in a
BufferedOutputStream.