In my application i have a uploaded functionality.For that i have used apache-commons file upload and spring multipart and i have stored the image in directory folder(not within project context). The problem where i stuck is fetching the image and rendering it on the jsp.I tried to read the image from that folder using Buffered image and ImageIO but can't figured out how to render it in jsp using img tag. Any help would be appreciated.
//Code for reading
BufferedImage im=ImageIO.read(new File(imagePath));
Finally able to display the image on web browser using img tag. Step that i follow now : 1. I read the image using BufferedImage. 2.Converted the bufferedImage into byte using ByteArrayOutputStream. 3.Encoded that stream into Base64 using apache commons codec lib and coverted into string 4.Returned this string value of image on html using img tag
//Pseudo Code
BufferedImage bufferedImage=ImageIO.read(new File(imagePath));
//imageDao contains the image name that i stored in the database
String []formatSplit=imageDao.split("\\.");
if(formatSplit.length==2){
String format=formatSplit[1];
//ImageUtility is class that contain code for converting bufferedimage to string
String traineeImage=ImageUtility.encodeToString(bufferedImage,format );
model.addAttribute("imagePath", traineeImage);
}
//ImageUtilty class -method
public static String encodeToString(BufferedImage image, String type) {
String imageString=null;
String encodedImage=null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
encodedImage=org.apache.commons.codec.binary.Base64.encodeBase64String(imageBytes);
imageString = "data:image/"+type+";base64,"+encodedImage;
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
And in the img tag src attribute i passed imageString and it worked. For finding the solution lot of hint i found from stackoverflow and from other blog that helped me to achieve what i was looking for. Thanks.