This seems like a problem waiting to happen:

byte [] loader = new byte [ (int) raf.length() ];
while ( (raf.read( loader )) > 0 )
{
  out.write( loader );
  out.flush();
  out.close();
}

RandomAccessFile.read(byte[]) is *not* guaranteed to completely fill the buffer
you provide. It will read *at most* loader.length bytes. Obviously, if your loop
executes more than once you're in trouble. If you really want to do this sort of
thing instead of the typical "copy bytes" approach, you could use:

raf.readFully(loader);

Quoting Adam L <[EMAIL PROTECTED]>:

> 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

-- 
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to