how to render an image in browser from server to client

I can able to upload an image in a repository and i can able to display the image on my local machine ,but i cant able to render from server (i used <img> tag in jsp) ,and i also tried thru buffer outputstream();i badly need help from yousrinath@incubatesoft.com
[289 byte] By [seethasrinatha] at [2007-11-27 8:07:48]
# 1

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

rakesh_mscita at 2007-7-12 19:50:34 > top of Java-index,Core,Core APIs...
# 2
sorry friend i already tried it,but i unable to render the image to the browser ,iam getting a cross mark .if any thing is there other than this please tell methanks for your advice
seethasrinatha at 2007-7-12 19:50:34 > top of Java-index,Core,Core APIs...
# 3
No, there isn't anything besides that. But it works when you do it correctly, so apparently you didn't do it correctly.
DrClapa at 2007-7-12 19:50:34 > top of Java-index,Core,Core APIs...
# 4

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

}

%>

seethasrinatha at 2007-7-12 19:50:34 > top of Java-index,Core,Core APIs...
# 5

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.

ejpa at 2007-7-12 19:50:34 > top of Java-index,Core,Core APIs...