Trying to Validate the name of a file with a Validator

Hi, Im trying to validate an file name that is sent with a form
I want that the Validator Checks if the filename ends with some extensions (.jpg ,etcc) for only addmit one type of files (images)
I ve build my own Pluggable Validator , but I dont know how to extract the filename from Validator.
Can anyone help me??
the Code is something like this
where
package jmar5439.webpage.validator;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import org.apache.struts.action.*;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.*;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.commons.validator.Validator;
import org.apache.struts.validator.Resources;
import org.apache.commons.logging.LogFactory;
import jmar5439.webpage.action.InsertRoomAction;
import org.apache.commons.logging.Log;
public class ImageFileValidator implements Serializable {
// public ImageFileValidator() {}
public static boolean validateImageFile(Object bean, ValidatorAction va,
Field field, ActionMessages errors,
HttpServletRequest request, Validator validator) {
String value = null;
if (field.getProperty() != null && field.getProperty().length() > 0) {
value = ValidatorUtils.getValueAsString(bean, field.getProperty());
String file = new String(value);
file = file.toLowerCase();
if (!GenericValidator.isBlankOrNull(value)) {
try {
if (!file.endsWith(".jpeg") || !file.endsWith(".gif") ||
!file.endsWith(".jpg")) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
return false;
} catch (Exception e) {
errors.add(field.getKey(),Resources.getActionMessage(request, va,field));
return false;
return false;
return false;
I ve declared
<form-property name="file"
               type="org.apache.struts.upload.FormFile"/>
in struts-config file and
<field property="file"
                    depends="imagefile">
in validate file.
Thanks

I ve solved like this
package jmar5439.webpage.validator;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import org.apache.struts.action.*;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.*;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.commons.validator.Validator;
import org.apache.struts.validator.Resources;
import org.apache.commons.logging.LogFactory;
import jmar5439.webpage.action.InsertRoomAction;
import org.apache.commons.logging.Log;
import org.apache.struts.upload.FormFile;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.*;
public class ImageFileValidator implements Serializable {
// public ImageFileValidator() {}
public static boolean validateImageFile(Object bean, ValidatorAction va,
Field field, ActionMessages errors,
Validator validator,
HttpServletRequest request) {
Log log = LogFactory.getLog(ImageFileValidator.class);
//log.info(form.toString() );
String value = ValidatorUtils.getValueAsString(
bean,
field.getProperty());
if (!GenericValidator.isBlankOrNull(value)) {
try {
FormFile file = (FormFile) PropertyUtils.getSimpleProperty(bean,
"file");
String filename=file.getFileName().toLowerCase() ;
//log.info("Filename:" + file.getFileName());
if (filename.endsWith(".jpeg") ||
filename.endsWith(".jpg") ||
filename.endsWith(".gif")) {
log.info("return true");
return true;
} else {
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
log.info("return false");
return false;
} catch (NoSuchMethodException ex) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
return false;
} catch (InvocationTargetException ex) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
return false;
} catch (IllegalAccessException ex) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
return false;
//return false;
// log.info("Entra0");
/* if (field.getProperty() != null && field.getProperty().length() > 0) {
// log.info("Entra");
// value = ValidatorUtils.getValueAsString(bean, field.getProperty());
value = ValidatorUtils.getValueAsString(bean, field.getProperty());
String file = new String(value);
file = file.toLowerCase();
if (!GenericValidator.isBlankOrNull(value)) {
// log.info("Entra2");
try {
if (!file.endsWith(".jpeg") || !file.endsWith(".gif") ||
!file.endsWith(".jpg")) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va,
field));
//log.info("Entra3");
return false;
} catch (Exception e) {
errors.add(field.getKey(),Resources.getActionMessage(request, va,field));
return false;
return false;
// return true;
} else {
return false;
Remember to visit
http://www.lloguerjove.com
and
http://www.alquilerjoven.com

Similar Messages

  • Trying to change the name of 1 file

    I have searched the topic and have not found a solution that works for me,
    I am trying to write an AppleScript that will change the name of a single file from path:old.txt to path:new.txt
    I have tried
    tell application "Finder"
    set name of file "work.txt" to PartA & PartG & ticker & ".txt"
    end tell
    among others and always either receive an error message, access denied or unable to set old file name to new file name

    You're missing the point.
    set name of file "work.txt" to PartA & PartG & ticker & ".txt"
    When you set the name of the file, the Finder needs two things - the original file that you're trying to rename, and the new name you want it to take.
    In your revised script you're specifying the existing name (not path) of the file you want to change, and specifying its path along with the new name. That's the wrong way around.
    Think of this command in the following manner:
    set name of (original file specification) to (new file name)
    Now, in the case of the 'original file specification' you need to provide enough information for the Finder to identify the file. You're just saying 'work.txt', but that is not enough (and that's why it's complaining). There could be a hundred 'work.txt' files spread around in different directories on your disk. How is the Finder supposed to know which one you want to change?
    A better format would be:
    tell application "Finder"
      set name of (file "work.txt" of path to desktop) to "ABC.txt"
    end tell
    In this way the Finder can absolutely identify which file you want to change (the file 'work.txt' that's on your desktop) and the new name you want it to take.
    Now, if the file is not on your desktop you can amend the script to include the relevant path, e.g.:
    tell application "Finder"
      set name of (file "work.txt" of folder "blah" of disk "disk name") to "ABC.txt"
    end tell
    This tells the Finder to look in the folder 'blah' on the specified disk.
    You can also do this by including the path to the file in the form:
    tell application "Finder"
      set name of (file "disk name:blah:work.txt) to "ABC.txt"
    end tell
    where you use : to delimit each folder in the path.
    Hope that helps.

  • Trying to get the Filetype of a file with a random extension.  i.e., image.1

    Hello all,
    I am dealing with images with the .1 -.6 standard which is used by companies like Gladson imagery.  I am given these files and I need to find out there FileTypes, which I also have heard can be called "MIME" types?  If that is different I let me know please.
    I tried to use the Files API Files (Java Platform SE 7 )
    Specifically probeContentType, but I get "null" for the .1 images, while the .bmp image does give me "image/bmp" as the result.
    I am thinking of reading the file into the program, then just outputting it into a file with a readable extension.
    Or is there a better way to do this?
    Thanks,
    ~KZ

    .1-.6 means nothing, it's a file extension given by Gladson.  Gladson &gt; Our Services &gt; Product Image Database but you seem to ignore this.
    You say it means 'nothing' but your original question says it is a standard:
    I am dealing with images with the .1 -.6 standard which is used by companies like Gladson imagery
    I've never heard of any such '.1 -.6 standard' and can't find ANY reference on the web to any such standard.
    So it is only appropriate to ask you what you are talking about. You didn't explain WHAT standard you are referring to, provide ANY information about that 'standard' or provide any link to documentation about that 'standard' so we can figure out what you are talking about.
    I don't know why a lot of the people on this forum feel the need to tell people their questions aren't good enough, when the question is pretty simple.
    They do that to let you know that you need to provide MORE information or a better explanation of what you are asking.
    Just because you don't know part of the question, doesn't mean the question is invalid.
    Reread my reply and you will see that it doesn't say the question is 'invalid'. It said
    If you want help it is YOUR responsibility to provide the information anyone would need to help you. No one is going to go searching the web to try to find out what a '.1 -.6 standard' might be, assuming there even is such a thing.
    You began your thread by referring to a 'standard' and I can find NO such standard anywhere. So there is no context for your question since it relates to a standard that doesn't appear to exist.
      Especially when the part of the question, in this case the .1 fileExtention doesn't really have much to do with the question, which is why it was an example.
    You are the one that first mentioned a 'standard'. If it doesn't 'have much to do with the question' then you are just confusing the issue by including it as a key part of your thread. If there IS such a standard then, being a standard, it would be documented and that documentation would likely explain the actual binary format of files that are created using that standard. That binary format would have information that could be used to identify the file type.
    For example a PDF file uses a 'standard' file format. The format is documented by Adobe and if you examine a file you might find the first line looks like this:
    %PDF-1.4
    That 'signature' helps to identify the file as using the PDF file format.
    But now you seem to be asking this totally different question:
    Given graphic files with meaningless names and extensions (and thus of unknown type) how does one determine the actual FileTypes (BMP, GIF, etc), which I also have heard can be called "MIME" types?
    Is that the question you are really asking?
    If it is then there IS NO generic way to identify an arbitrary file format since each 'standard' format is different and will have different possible 'signature' characteristics. You would have to have code to test for EVERY possible 'signature'.
    That is why if the standard you referenced actually existed it would help determine what the list of POSSIBLE file types might be. If you know that each file MUST BE a Gladson file type then the first step is to create a list of ALL possible file types that Gladson uses.
    Then you can locate the documentation for each of those file types (e.g. BMP, GIF, etc) to determine how to create a 'parser' to check for those 'signatures'.

  • Oracle Linux 6.6 I tried to edit the /etc/resplv.conf file on nano, gedit and VI but the result doesn't appear on the file when I cat it

    Oracle Linux 6.6 I tried to edit the /etc/resplv.conf file on nano, gedit and VI but the result doesn't appear on the file when I cat it

    Hi ! do you mean the file /etc/resolv.conf ? This file should be by default in the /etc/ diretory and contains the dns-name resolutions. http://linux.die.net/man/5/resolv.conf http://www.tldp.org/LDP/nag/node84.html http://en.wikipedia.org/wiki/Resolv.conf

  • Can we get the name of fmx file at runtime

    Hi all,
    can we get the name of fmx file which is doing some DML operation on a specific table Can we back track which fmx module has done DML on a specific table. The reason I need this because I've 7 different fmx files which are doing some DML operation on a specific table and these fmx files were used by different users. Not necessarily 1:1 For eg. one user can do DML through 3 different fmx files. So in such case if I want to want to know from which fmx file this DML has take place .How can I achieve that. I know I can get the oracle username through v$session but it will not suffice my needs. I need the name of fmx file also.
    I'm using:
    Forms [32 Bit] Version 10.1.2.0.2 (Production)
    Oracle Database 10g Release 10.2.0.1.0 - Production
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    Any help?
    Warm Regards
    Percy

    Percy,
    For that you can set the Module Field in the V$session in each form by,
    DBMS_APPLICATION_INFO.SET_MODULE(MODULE_NAME => :SYSTEM.CURRENT_FORM, ACTION_NAME => USER);Write this in each form, and then you will get the form name in the V$SESSION's module field with form name.
    Hope this helps.
    Regards,
    Manu.

  • I am trying to find out how to assign files with particular extensions to the appropriate software. At the moment when I create a file using Word it is apparently given the extension .docx but Word doesn't recognise its own files. How do I alocate th

    I am trying to find out how to assign files with particular extensions to the appropriate software. At the moment when I create a file using Word it is apparently given the extension .docx but Word doesn't recognise its own files. How do I allocate the extension .docx to Word? There used to be a way of doing it, I think under "Preferences" but I can't seem to find it.

    Still in the same location:
    File > Get Info > Open with (select) > Change All (button)

  • Change the name of a file.

    Hello, I don't how can I change the name of a file ?.
    Somebody can help me, please?
    For example : c:\out\prove1.csv to  c:\out\prove1_09112007_163001.csv
    Thanks

    Another way would be to use the FILE_COPY and
    FILE_DELETE of the class CL_GUI_FRONTEND_SERVICES to first copy the file and then delete the other one.
    REgards,
    Rich Heilman

  • BEx Brodcaster for Workbook - Cannot change the name of attachment file.

    Dear All Experts,
    I am using the BEx Broadcaster to distribute report of workbook via email. The name of attchament file in email is the technical of workbook. I want change the name of attchment file and found this thread
    [Broadcasting Workbook - Change attachment filename|Broadcasting Workbook - Change attachment filename] that reference to method in background process. Can I create customize program or method to change the name of file.
    Thank you and really appreciate your help.
    Zilla D.

    Hello Zilla,
    By SAP methods, this is not possible. There is no way to do that. May you can suggest it on our Community of Innovation. See note 11.
    This is not only your concern. Some customers already request this functionallity.
    Best Regards,
    Edward John

  • I am trying to change the name in my "from" box

    I am trying to change the name in my "from" box. My name has changed and although I have changed the name on the account, I can't figure out how to make the default name in my "from" box change from my previous name to my new name.

    You may need to know that (in Snow Leopard at least) you specify multiple possible "From" email addresses as a list separated by commas.  Just type them into that little Email Address box*: they'll move over to make room.  Then, on your new messages from that point on, a pop-up menu will appear for the From field.  Neat, eh?
    *-- Notice that you can lie in this box.  Your ISP might not let you get away with it, but spammers use this loophole.
    --Gil

  • When I click on the Start Icon and type the name of a file I am searching for none appear.

    When I click on the Start Icon and type the name of a file I am searching for none appear.
    Using Widows 7

    Are you searching for a file that you downloaded via Firefox?
    If that is the case then you can check the Download Manager (Tools > Downloads) and if that file is listed there then right click that entry and choose > Open Containing Folder. If that entry is grayed then the file is no longer in the original download location and possibly removed by AV software.

  • Has anybody gotten an error when trying to unzip the Encore Content zips file? How did you fix?

    Has anybody gotten an error when trying to unzip the Encore Content zips file? How did you fix?

    You may have a corrupt download... did you download again?

  • I have a copy of PS CS5 Creative shop which was causing some difficulties with my computer Windows 7 Ultimate 64 bit. I cancelled the activation of my CS5 and uninstalled. When I tried to reinstall the message showed :"missing file unable to initialize" w

    I have a copy of PS CS5 Creative shop which was causing some difficulties with my computer Windows 7 Ultimate 64 bit. I cancelled the activation of my CS5 and uninstalled. When I tried to reinstall the message showed :"missing file unable to initialize" what do I do.

    Many thanks.
    With those symptoms, I'd try the following document:
    Apple software on Windows: May see performance issues and blank iTunes Store
    (If there's a SpeedBit LSP showing up in Autoruns, it's usually best to just uninstall your SpeedBit Video Accelerator.)

  • I purchase Adobe Elements Photoshop 12 & Premiere 12 together from a camera shop in town.  When I tried to use the Premiere, it splashed "created with the trial verson" over all of the photos. How can this unsightly watermark be removed. It has severely h

    I purchased  Adobe Elements Photoshop 12 & Premiere 12 together from a camera shop in town.  When I tried to use the Premiere, it splashed "created with the trial version" over all of the photos. How can this unsightly watermark be removed. It has severely hampered my timeline to start and finish this project. Why would a purchase software product purport to be a "trial" version?

    This indicator will appear when you have not registered your product or typed in the serial number for the product.
    Once you've added the serial number, any future products will not display this indicator.
    To remove this this indicator from videos created before you added the serial number, you'll need to delete the rendered files.

  • How do I create a product or item database so I can search fr the product and its files with hte pro

    How do I create a product or item database so I can search for the product and its files with the products name or four digit code

    Ok so I made some progress on this. I have figured out, that I can add a chained "add to cart" to certain items, then when they click the button for Buy now, it will add both items to the cart. However, this would require me to manually build each product page and generate a custom button for each one with both product IDs in it.
    Can anyone offer help on how to put some JS in that would append a second function to the onclick function that BC Generates dynamically?
    For the products that require a set up fee, I would assume I would add in to either the product templates or in to the item description some JS that would find the onclick of the buynow button and append a second function to also add to cart the setup fee product. The end result being code that looks like this:
    <input type="submit" class="productSubmitInput" onclick="AddToCart(188536,6314368,'',4,'','',true);AddToCart(188536,6314367,'',4,'','',tr ue);return false;" value="Buy Now" name="AddToCart_Submit" />
    Except of course, the product ID on the first AddToCart would be the main product with the second one being the one appended.
    Does any of that make sense? lol

  • I tried to sync the music on my PC with my old iPhone, but all of my songs on iTunes were deleted, How do I get them back?

    I tried to sync the music on my PC with my old iPhone, but all of my songs on iTunes were deleted, How do I get them back? When I go to the iTunes store and look at the songs I hade before, it says I already purchased them, but when I go back to my library, they are all gone. I thought it was just removing the songs from my phone, but now I have no music on my phone or PC. Please help!

    The sync is one way - computer to iphone.
    You need to copy everything from your old computer to your new one.

Maybe you are looking for

  • Prob when opening itunes

    Hi people, I recently encountered a problem with my itunes. when I try to open my itunes it gives me an errror about the media library being locked...??? I don't really understand why this problem occured.... I tried to reinstall itunes, redownload t

  • Cannot start CS:GO on my alienware 17 windows 8.1 (there app crashes before start)

    I have the CS:GO installed on my new alienware 17 (june 2013) but when I try to run the game the game crashes and nothing shows up. it is note worthy when I disable the mSATA acceleration from Intel rapid storage technology (disabling Intel Smart Res

  • Server Error in '/ias_relay_server' Application.

    Hello All, I am getting error while enrollment code inspect. Server Error in '/ias_relay_server' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed,

  • Error whle creating  Main Asset Master(AS01)

    Hi,    while i was trying to create the asset master  , <b>ABAP run time error</b>  is displayed <b>"snap_no_new_entry"</b> and i could not able to save the asset master. please provide some solution for this problem....... i am working on SAP 6.0 ve

  • Access abap object  from external system

    In R/3 have a Business Objects how can I access this object, for example Workflow BO, from java (external system) using XI???? My goal is create workflow´s in r/3 sending as input the workflow characteristics and then create the workflow in R/3 and s