Hi,
I am trying to display a JSP as an image which can be used in another JSP's img tags.
Here is how I coded image.jsp.
I observed that for 'bmp' image type, image object is 'null'. When I directly open 'bmp' image url in browser, it opens correctly.
<%@ page import = "java.io.*" %>
<%@ page import = "java.awt.image.BufferedImage"%>
<%@ page import = "javax.imageio.ImageIO"%>
<%@ page import = "javax.imageio.spi.IIORegistry"%>
<%@ page import = "java.net.URL"%>
<%@ page import = "java.net.URLEncoder"%>
<%
try{
String imgUrl = URLEncoder.encode(request.getParameter("imgUrl"));
imgUrl = imgUrl.replaceAll("%3A",":");
imgUrl = imgUrl.replaceAll("%2F","/");
imgUrl = imgUrl.replaceAll("\\+","%20");
String[] urlSplit = imgUrl.split("\\.");
String imgTyp = urlSplit[urlSplit.length-1];
URL url = new URL(imgUrl);
BufferedImage image = null;
image = ImageIO.read(url);
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(image,imgTyp, bas);
byte[] imgData = bas.toByteArray();
response.setContentType("image/"+imgTyp);
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally{
}
%>
Any idea, why is this happening?
With regards,
Amey