Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedImage 'null' with ImageIO.read(url)

AmeyFeb 4 2011 — edited Feb 7 2011
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
This post has been answered by EJP on Feb 4 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 7 2011
Added on Feb 4 2011
10 comments
5,719 views