I have a form field of type org.apache.struts.upload.FormFile. I would like to validate the field's size with a custom struts validator. To get the field's value, i have used for a text field the next code:
String value = ValidatorUtils.getValueAsString(bean,field.getProperty());
However, how do i get the value for a FormFile field? I would like to get the value as a FormFile object.
FormFile myFile = (FormFile) // TODO
Next i post the involved file's snippets.
struts-config.xml
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="ChangePictureForm" type="struts.administration.ChangePictureForm">
<form-property name="id" type="java.lang.String" />
<form-property name="file" type="org.apache.struts.upload.FormFile" />
</form-bean>
</form-beans>
...
..
.
validator-rules.xml
<!-- User Custom Validation Rules -->
<validator name="size"
classname="struts.StrutsValidator"
method="validateSize"
methodParams="java.lang.Object,
org.apache.commons.validator.Validator,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.size"/>
StrutsValidator.java
package struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.Resources;
public class StrutsValidator {
public static boolean validateSize (
Object bean,
Validator validator,
ValidatorAction validatorAction,
Field field,
ActionMessages errors,
HttpServletRequest request) {
FormFile myFile = null; //TODO (FormFile) get it from somewhere
String type = field.getVarValue("type");
int size = myFile.getFileSize();
// MySQL Storage Requirements http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
int allowed_size = 65538; //Blob L + 2 bytes, where L < 2 ^ 16
if(type.equalsIgnoreCase("tinyblob")) {
allowed_size = 257; // L + 1 bytes, where L < 2 ^ 8
} else if(type.equalsIgnoreCase("mediumblog")) {
allowed_size = 16777219; // 16 Mb - L + 3 bytes, where L < 2 ^ 24
} else if(type.equalsIgnoreCase("longblob")) {
allowed_size = 16777219; // 4096 Mb - L + 4 bytes, where L < 2 ^ 32
}
try {
if(size>=allowed_size) {
errors.add(field.getKey(),Resources.getActionMessage(validator,request,validatorAction,field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(),Resources.getActionMessage(validator,request,validatorAction,field));
return false;
}
return true;
}
}
validation.xml
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<formset>
<form name="ChangePictureForm">
<field property="file" depends="required,size">
<arg0 key="file" />
<arg1 name="type" key="${var:type}" resource="false" />
<var>
<var-name>type</var-name>
<var-value>blob</var-value>
</var>
</field>
</form>
</formset>
</form-validation>