It might be your output stream.. here's my code, which does work:
File imagedata = new File(img.getImageLocation(),
img.getImageId() );
resp.setContentType( img.getImageType() );
RandomAccessFile raf = new RandomAccessFile( imagedata, "r" );
resp.setContentLength( (int) raf.length() );
ServletOutputStream out = resp.getOutputStream();
byte [] loader = new byte [ (int) raf.length() ];
while ( (raf.read( loader )) > 0 )
{
out.write( loader );
out.flush();
out.close();
}
----- Original Message -----
From: "Mark Lowe" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Wednesday, December 03, 2003 3:32 AM
Subject: [ot] image file to byte array
>
> Although its not a struts problem as such I do happen to be using
> struts in my app..
>
> Here some code as it will say more in less words that it will take to
> explain
>
> In my action servlet I have something like this..
>
> <code>
> response.setContentType("image/jpeg");
> response.setHeader("Cache-Control", "no-cache");
> OutputStream ou = response.getOutputStream();
>
> String imageStr = "file:///foo/test.jpg";
> java.net.URI imgUri = new java.net.URI(imageStr);
> File imageFile = new File(imgUri);
> FileInputStream imageFileStream = new FileInputStream(imageFile);
> byte[] image = new byte[(int)
> imageFile.length()];System.out.println("imageFile.length()::::"+
> imageFile.length());
> ou.write(image);
>
> </code>
>
> Now the system out returns the correct amount of bytes so its seeing
> the image and not getting file not found exception. but it just wont
> stream back to my jsp when i run the action.
>
> This code is basically a else clause when no results are returned from
> the database.. The results from the db (via hibernate works just fine)
> but this bit doesn't..
>
> Can anyone who's done this cast their eye over it for anything
> obviously wrong. Could it be a permissions issue?
>
> Cheers Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]