I’m writing an image to servlet response with best performance. Any advices, practices, experience?
I’m writing an image to servlet response with best performance. Any advices, practices, experience?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For best performance and efficiency, don’t put the entire content in
byte[]. Eachbyteeats, yes, one byte from Java’s memory. Imagine 100 concurrent users which requests 10 images of each 100KB, that’s already 100MB of Java memory eaten away.Get the image as an
InputStreamfrom the DB usingResultSet#getBinaryStream(), wrap it in anBufferedInputStreamand write it to theOutputStreamof the response wrapped in anBufferedOutputStreamthrough a smallbyte[]buffer.Assuming that you select images by the database key as identifier, use this in your HTML:
Create a
Servletclass which is mapped inweb.xmlon anurl-patternof/images/*and implement itsdoGet()method as follows.:In the
ImageDAO#find()you can useResultSet#getBinaryStream()to get the image as anInputStreamfrom the database.