Hi,
Write a servlet which will read the image data as bytes and write them to the response object. It'll also set the content type of the response to the corrent MIME type [check for the correct one of images].
set this servlet path as the src attribute for the img tag.
Hope it solves your problem.
Regards,
Rakesh
i got the answer with scaling of image if any body care u can check it
<%
String filepath=request.getParameter("file");
File theImageFile = new File (filepath);
String name=filepath.substring(filepath.lastIndexOf("\\")+1);
if(name!="Thumbs.db")
{
//outputImage(theImageFile, "jpeg", response);
//public static void outputImage(File theImageFile, String type, HttpServletResponse response) throws IOException
// {
response.setContentType("image/jpeg");
FileInputStream fin = new FileInputStream(theImageFile);
OutputStream os = response.getOutputStream();
byte[] buf = new byte[4026];
int count1 = 0;
while(true) {
int n = fin.read(buf);
if(n == -1) {
break;
}
count1 = count1 + n;
int thumbWidth = Integer.parseInt("200");
int thumbHeight = Integer.parseInt("200");
Image img = Toolkit.getDefaultToolkit().getImage(filepath);
// Image image = img.getScaledInstance(thumbWidth, thumbHeight ,Image.SCALE_FAST);
Image image = img.getScaledInstance(thumbWidth, thumbHeight ,Image.SCALE_SMOOTH);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
// determine thumbnail size from WIDTH and HEIGHT
double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0,0, thumbWidth, thumbHeight, null);
// save thumbnail image to OUTFILE
BufferedOutputStream op = new BufferedOutputStream(new
FileOutputStream(filepath));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(op);
JPEGEncodeParam param = encoder.
getDefaultJPEGEncodeParam(thumbImage);
int quality = Integer.parseInt("50");
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
os.write(buf,0,n);
}
os.flush();
os.close();
fin.close();
}
%>
OK, so I checked it.
byte[] buf = new byte[4026];
A very strange value. Where did you get that from? 4096 or 8192 or 10000 would be more common.
int count1 = 0;
while(true) {
int n = fin.read(buf);
if(n == -1) {
break;
}
Everything from here to
os.write(buf,0,n);
}
creates a local thumbnail but doesn't send it to the client. It has no effect on what you're reading from 'fin' or writing to 'os', and therefore has no effect on the client whatsoever. It is just 44 lines of uselessness. And to make it worse, it is executed every time around this read loop.
Please.