1

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, i m 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.

vishal
  • 11
  • 1
  • 4
  • `ImageIO.write()` returns a `boolean`, is it `true`? Aside from that, jbx's solution is usually a better approach IMO. :-) – Harald K Nov 05 '13 at 19:26

1 Answers1

1

Not sure what the rest of your environment looks like (i.e. 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.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • sorry but i made a mistake above, i am giving relative path of that same image i have written into a folder.. – vishal Nov 05 '13 at 18:51
  • but according to u, instead of writing it again and again to a folder, i should write it to the browser..!!! – vishal Nov 05 '13 at 18:52
  • @vishal If the image is dynamic and displays some data, writing to disk in one place always will result in users seeing someone else' image. If nothing about the image is dynamic, why are you creating it over and over anyway? – developerwjk Nov 05 '13 at 23:27
  • @vishal Well yes, not sure why you're so surprised. When the browser encounter's the `` tag it will make an HTTP request and will wait for the data to come through. When you write HTML through JSPs you are doing essentially the same thing. You don't output the HTML to a file on your local disk but it is sent through the `OutputStream` provided through the `HttpServletResponse` of the servlet. – jbx Nov 06 '13 at 01:31
  • @jbx, actually java and these concepts a really new to me and i m not much familiar with these, but thanx a lot for your help..i have changed the hardcoded absolute path and implemented this thing. And its working perfectly, thanx.. – vishal Nov 06 '13 at 16:27
  • @vishal OK. Actually you should look a bit into how HTTP works in general. They are the same for all web development irrespective if you're using Java, PHP, or any other language. If the answer worked for you don't forget to mark it as the answer :) – jbx Nov 06 '13 at 16:58