Forums Register Login

Display Images dynamically to a servlet

+Pie Number of slices to send: Send
I saw postings earlier on this topic and adopted the suggestions given , but I can display only a single image.This works for a single image jpg .All the files uploaded are
into c:\temp. The Servlet program parses through the dir and picks up
only jpg's (since I want to display all jpg's first and then expand to
gif's).The servlet now displays only an icon but no images .
I initially brought all the images in to a ArrayList but I thought its
better to display directly from the filesystem.
Please let me know what is the problem

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.* ;
/**
*
*
*
*
*
* @author
*/
public class DisplayImage extends HttpServlet
{
java.util.List aList = new ArrayList();

public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{


String path= "c:\\temp\\";
File f = new File(path);
String[] dirList = f.list();

int l=100;
int k=100;
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
PrintWriter out = response.getWriter();

for( int i = 0; i < dirList.length; ++i )
{
String fExt= "jpg";
// System.out.println(">>The files in the dir are>>:"+dirList[i] );
String filename = dirList[i].toString();
// System.out.println("FileName is"+filename);
int sLength = filename.length();
int sLastIndex = filename.lastIndexOf(".",sLength);

//System.out.println(">>Last Index Value>>"+sLastIndex);

String imageExt = filename.substring(sLastIndex+1,sLength);
//System.out.println(">>ImageExtensionis>>"+imageExt);
if(imageExt.equalsIgnoreCase(fExt))

{
String imagefilename = path+filename;
//System.out.println(">>Final filename is>> "+imagefilename);
Image myImage = new ImageIcon(imagefilename).getImage();

BufferedImage outImage = new BufferedImage(10,10 ,BufferedImage.TYPE_INT_RGB);
Graphics2D d2g = outImage.createGraphics();


d2g.drawImage(myImage, l, k, null);
//AffineTransformOp afo = new AffineTransformOp(af,1);
//BufferedImageFilter bf = new BufferedImageFilter(afo);

//d2g.drawImage(myImage, null, l,k);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(outImage);




k+=100;

}

else
{
System.out.println(">>Not a jpeg file>>");
}

}


out.close();

}
}
+Pie Number of slices to send: Send
You are mixing up servlet functions with GUI functions in a single class. Exactly what are you trying to do? Where do you want the image(s) to be displayed?
Bill
+Pie Number of slices to send: Send
What I intend to do is :
1.Displa.jsp calls a servlet DisplayImage
2.DisplayImage goes to a known directory in the filesystem and picks up all images.
3.Display all the images on the servlet at once in thumbnail fashion.
4.Click on each image to enlarge it.
So far I am stuck at step 3.
So how do I proceed about on this problem if I am mixing up.
Any help is appreciated.
Thx
+Pie Number of slices to send: Send
William,
Which is a better way to display?
Thx
+Pie Number of slices to send: Send
I did not clean up code, there is a PrintWriter class which needs to be commented, since I was not getting result i though of doing with HTML <IMG/> tag.
+Pie Number of slices to send: Send
We seem to have a conceptual problem here:

1.Displa.jsp calls a servlet DisplayImage
2.DisplayImage goes to a known directory in the filesystem and picks up all images.
3.Display all the images on the servlet at once in thumbnail fashion.
4.Click on each image to enlarge it.


Servlets don't display images. Servlets serve web pages and other resources in response to client requests. If you are going to program web applications you have to keep this in mind at all times - every servlet result is in response to a client request. Display takes place on the client browser.
Look at the HTML for a page that contains images - every image that is displayed comes from a separate request in an IMG tag that has a SRC attribute. If you make an HTML page that has an IMG tag where the SRC addresses a servlet, that servlet can serve the image data so the client browser can display it. One request can only serve one image.
Bill
+Pie Number of slices to send: Send
I agree with everything that William is saying.
Basically I think you can acheive your aim with 2 servlets as follows:
First servlet generates a dynamic HTML page that contains and <img> tag for each image in your directory. The href of these <img> tags points to your 2nd servlet (e.g. second servlet listens on '/images' the href for could be something like href='images/image_name1.gif')
The second servlet would then receive the requests for the individual images and could generate the thumbnails and serve the image byte[]. (The name of the requested image can be retrieved from the request.getPathInfo() method I think)
Hope this helps.
Andy Bowes
+Pie Number of slices to send: Send
hi
go on with andy's answer, you can specify the size of the image in the <img> tag, so that thumbnail are generated. & on its click event you can call other brower window to show the image in original size.
hth
MB
+Pie Number of slices to send: Send
Welll... specifying the size in the IMG tag doesn't really create a thumbnail because the browser has to download the whole image, then scale it to specified size so you don't gain anything.
Java does have the image manipulating capabilities to generate a true thumbnail (smaller image file) at the server and send that - but it has been quite a while since I did any image manipulation and I think a whole new API is now available.
Bill
+Pie Number of slices to send: Send
Thanks William, Andy , and Malhar.I appreciate all of you for the response.As mentioned I did a concept mistake of trying to rendering images on the servlet.Graphics2D API has the image manipulation capabilities.
Grow your own food... or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1607 times.
Similar Threads
Displaying image in applet on click event
How do I display JPEG, crop it, display cropped JPEG and undo crop.
java.awt.Image to a HTML file via a servlet
servlets and thumbnails
how to save text as gif ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:49:52.