response.setContentType(mimeType);
if (possibleCached.exists()) {
// send content
byte[] buffer = new byte[(int) possibleCached.length()];
FileInputStream fis = new FileInputStream(possibleCached);
fis.read(buffer);
fis.close();
response.getOutputStream().write(buffer);
response.getOutputStream().flush();
response.getOutputStream().close();
} else {
BufferedImage image = ImageIO.read(tfile);
BufferedImage myImage =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = myImage.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
int width = image.getWidth();
int height = image.getHeight();
if (width > height) {
Image newImage =
image.getScaledInstance(w, -1, Image.SCALE_SMOOTH);
g.drawImage(
newImage,
0,
(h - newImage.getHeight(null)) / 2,
null);
} else {
Image newImage =
image.getScaledInstance(-1, h, Image.SCALE_SMOOTH);
g.drawImage(
newImage,
(w - newImage.getWidth(null)) / 2,
0,
null);
}
ImageIO.write(myImage, "jpg", possibleCached);
ImageIO.write(myImage, "jpg", response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
} else {
// retrieve mime type
String mimeType =
FileTypeMap.getDefaultFileTypeMap().getContentType(tfile);
response.setContentType(mimeType);
// send content
byte[] buffer = new byte[(int) tfile.length()];
FileInputStream fis = new FileInputStream(tfile);
fis.read(buffer);
fis.close();
response.getOutputStream().write(buffer);
response.getOutputStream().flush();
response.getOutputStream().close();
}