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();
}
}
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();
}
}