Peter
This is a screen which returns an image. Hope this helps...
Rodrigo
/*
* Created on Mar 17, 2003
*
*/
package com.sipecom.modules.screens;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import org.apache.turbine.modules.screens.RawScreen;
import org.apache.turbine.util.RunData;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">Rodrigo Reyes C.</a>
*/
public class ImageGetter extends RawScreen {
protected String getContentType(RunData data) {
return "image/jpeg";
}
protected void doOutput(RunData data) throws Exception {
try {
String path = data.getServletContext().getRealPath("/");
String fileName = data.getParameters().getString("src");
fileName = path + fileName;
data.getResponse().setHeader("Content-Disposition", "inline;
filename=" + fileName);
ServletOutputStream out = data.getResponse().getOutputStream();
InputStream is = null;
File file = null;
is = new FileInputStream(fileName);
file = new File(fileName);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset,
bytes.length - offset))>= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file " +
file.getName());
}
// Close the input stream and return bytes
is.close();
out.write(bytes);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
----- Original Message -----
From: "Peter Courcoux" <[EMAIL PROTECTED]>
To: "Turbine Developers List" <[EMAIL PROTECTED]>
Sent: Wednesday, March 19, 2003 4:00 PM
Subject: Re: Direct Response and IllegalStateException
> Rodrigo,
>
> I am not familiar with that pattern of use. How do you use the
> RawScreen? Are you doing this with 2.2? I'm sure I tried extending
> RawScreen and hit a problem but I cannot remember what.
>
>
> On Wed, 2003-03-19 at 20:34, Rodrigo Reyes wrote:
> > When I need to use the ServletOutputStream directly, I extend a screen
from
> > RawScreen. That RawScreen has been very useful to me. Still, I think we
are
> > removing it from Turbine 2.3. Hope I am wrong...
> >
> > Rodrigo
> >
> > ----- Original Message -----
> > From: "Peter Courcoux" <[EMAIL PROTECTED]>
> > To: "Turbine Developers List" <[EMAIL PROTECTED]>
> > Sent: Wednesday, March 19, 2003 3:19 PM
> > Subject: Re: Direct Response and IllegalStateException
> >
> >
> > > Henning,
> > >
> > >
> > > On Wed, 2003-03-19 at 13:12, Henning P. Schmiedehausen wrote:
> > > > Peter Courcoux <[EMAIL PROTECTED]> writes:
> > > >
> > > > >Hi all,
> > > >
> > > > >I have recently changed from using turbine 2.2-b3 to 2.2.1 and from
> > > > >using VelocityECSLayout to VelocityOnlyLayout.
> > > >
> > > > Actually, I'm getting more and more unhappy with the deprecation of
> > > > VelocityECSLayout. There seem to be many quirks that can't be fixed
> > > > in a clean manner. We might have to think about this a little more.
> > > >
> > > > >One of my actions which extends VelocitySecureAction handles a file
> > > > >download by obtaining the HttpServletResponse setting the headers
and
> > > > >then obtaining and writing directly to the ServletOutputStream.
> > > >
> > > > >Before the changes outlined above this caused no problem. Having
made
> > > > >the changes the following Exception occurs after the completion of
the
> > > > >download and closing the ServletOutputStream.
> > > >
> > > > Could you please post some code snippets. How do you do the file
> > > > download? Directly from the action (you shouldn't do that) or do you
> > > > have a screen which does the download to which you redirect? I'm
> > > > getting a little scared by reading that you try to manipulate the
> > > > Servlet output stream by yourself. :-)
> > > >
> > > > Regards
> > > > Henning
> > >
> > > As mentioned off-list, I have a need to build a zip file of multiple
> > > download files and the easiest way I have found is to directly
> > > manipulate the ServletOutputStream. This also applies where I am
> > > building images for embedding within pages of data.
> > >
> > > I have solved this by creating a DirectResponseLayout class which does
> > > nothing except check that declareDirectResponse() has been called on
> > > RunData. I then call
> > >
> > > data.setLayout("DirectResponseLayout");
> > >
> > > in my Action class.
> > >
> > > Source of DirectResponseLayout :-
> > >
> > > package com.whatever.modules.layouts;
> > >
> > > // Turbine Classes
> > > import org.apache.turbine.modules.Layout;
> > > import org.apache.turbine.util.RunData;
> > >
> > > /**
> > > * This layout allows an action to manipulate the
> > > * ServletOutputStream directly. It requires that
> > > * data.declareDirectResponse() has been called to
> > > * indicate that the OutputStream is being
> > > * handled else an Exception is thrown
> > > *
> > > * @author <a href="mailto:[EMAIL PROTECTED]">Peter Courcoux</a>
> > > */
> > > public class DirectResponseLayout extends Layout
> > > {
> > > /**
> > > * Method called by LayoutLoader.
> > > *
> > > * @param RunData
> > > */
> > > public void doBuild( RunData data ) throws Exception
> > > {
> > > if (!data.isOutSet())
> > > {
> > > throw new Exception(
> > > "data.declareDirectResponse() has not been
called");
> > > }
> > > }
> > > }
> > >
> > >
> > >
> > > One drawback is that it calls data.isOutSet() which is deprecated.
> > >
> > > Questions.
> > >
> > > 1. Is there a better way?
> > > 2. If not, would it be worth including the DirectResponseLayout class
in
> > > the turbine distribution.
> > > 3. Is there a case for removing the deprecation of RunData.isOutSet()?
> > > 4. Is this worth documenting. At least one other user appears to be
> > > doing something similar.
> > >
> > > Regards
> > >
> > > Peter
> > > --
> > > Peter Courcoux <[EMAIL PROTECTED]>
> > >
> > > ---------------------------------------------------------------------
> > > 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]
>
> Regards,
>
> Peter
> --
> Peter Courcoux <[EMAIL PROTECTED]>
>
> ---------------------------------------------------------------------
> 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]