简体   繁体   中英

displaying bufferedimage in jsp

Actually i am trying to create an online image editor using jsp/struts2 framework. i have used imageIO.write() method to write buffered image that have been processed through various filters i have created. Now the thing is that i have to display that image on the same page. So far i have done this much.

 {
 File imagefile = new File("C:/Users/Documents/NetBeansProjects/project/web/images/"+img+"");
 image = ImageIO.read(imagefile);

 ImageFlipps imgflip=new ImageFlipps(image); /* image filter class that takes a bufferedimage and return a processed bufferedImage. */

 imgflip.setBufferedImages();
 BufferedImage img2= imgflip.vhIImageFlipps(); // returned buffered image.

 ImageIO.write(img2, "jpg", new File("C:/Users/Documents/NetBeansProjects/project/web/images/new.jpg")); // writing image to some specific folder.

  }

Below this code in the same page, im trying to display that saved image using image tag but it is not getting the image to display.

<img src="C:/Users/Documents/NetBeansProjects/project/web/images/new.jpg" alt="image not found" />

a simple logic, but not working.... Please help me out..have been into this problem since last 2 weeks.

Not sure what the rest of your environment looks like (ie what servlets or filters you have), but if it is a web application I don't know why you are writing things to your private local disk! (Apart from the fact that your file paths are hard coded to the NetBeans folder so they won't work the moment you deploy your application somewhere else).

Since an image is not a page as such, I would put your code in a simple servlet. The doGet() method is the right place to put your code in. However, instead of writing to a file on your hard disk you need to stream it to the browser.

So something like:

response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
ImageIO.write(img2, "jpg", out);
out.close()

The response is the HttpServletResponse passed to your doGet() method.

On your page then your <img> tag should map to a URL which routes to your servlet (through the web.xml mapping). So something like this:

<img src="http://www.yoursite.com/yourapp/imageserv">

If you need to process different images or identify which image you want to render you need to pass URL parameters like:

 <img src="http://www.yoursite.com/yourapp/imageserv?image=imageid">

And in your doGet() method you check the URL parameter from the HttpServletRequest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM