Rodrigo, Many thanks for this. I'll have to look again at this as it seems straight forward.
On Wed, 2003-03-19 at 21:27, Rodrigo Reyes wrote: > 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(); > } > } > } <snip /> Regards, Peter -- Peter Courcoux <[EMAIL PROTECTED]> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
