I wrote a validator for this but I'm not totally sure this doesn't end
up uploading the whole file anyway.. doesn't seem like it though.
public void validate(IFormComponent field, ValidationMessages messages,
Object object) throws ValidatorException
{
IUploadFile imageFile = (IUploadFile)object;
InputStream fis = imageFile.getStream();
ImageInputStream iis = null;
try
{
iis = ImageIO.createImageInputStream(fis);
Iterator imageReaders = ImageIO.getImageReaders(iis);
if (!imageReaders.hasNext())
{
// There are no image readers for this file
fis.close();
throw new ValidatorException("Invalid image
file.");
}
ImageReader firstReader =
(ImageReader)imageReaders.next();
String imageFormat =
firstReader.getFormatName().toLowerCase();
if (!imageFormat.equals("jpeg") &&
!imageFormat.equals("jpg") &&
!imageFormat.equals("gif") &&
!imageFormat.equals("png"))
{
// This is a valid image but we only accept
jpeg, jpg, gif, or png
fis.close();
throw new ValidatorException("The image format
must be JPG, GIF, or PNG.");
}
BufferedImage image = ImageIO.read(iis);
if (image.getWidth() > maxWidth || image.getHeight() >
maxHeight)
{
// This image is too big
fis.close();
throw new ValidatorException("The image exceeds
the maximum width/height.");
}
fis.close();
}
catch (IOException ioe)
{
// There was some other problem uploading the image
throw new ValidatorException("Invalid image file.");
}
finally
{
if (iis != null) {
try { iis.close(); } catch (IOException ioe) {}
}
if (fis != null) {
try { fis.close(); } catch (IOException ioe) {}
}
}
}
HTH
Ben
-----Original Message-----
From: Daniel M Garland [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 13, 2005 10:01 AM
To: [email protected]
Subject: Client-side validated uploads
Hi all,
I'm trying to figure out how to get around a brick wall:
I want to validate an image file based upon its dimensions and filesize
before allowing the file to be written up to the server. I thought about
achieving this using a signed applet to read the file, and then pass an
acceptable filename to an Upload component. However, the value field
will not be supported in most browsers, so is there any other way I can
use the Uploading features of Tapestry by somehow extending Upload?
If not, where is the code that does this? I hear something about the
commons project at apache but currently jakarta.apache.org/commons/
appears to be down.
Thanks for your help in advance.
--
Dan Garland
------------------------
[EMAIL PROTECTED]
icq: 120963437
aim: dmgarland1767
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
---------------------------------------------------------------------
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]