You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ahmed Khan <ah...@gmail.com> on 2012/07/20 20:40:34 UTC

Fileupload misbehaving

Hi,

I have a struts 2 application where 2 pages have struts file upload. One
works perfectly while the other just wont work. I gives me* "**Invalid
field value for field "secImage"*.

Here is the code for JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>

<html>
    <head>
    </head>
    <body>
        <%@ include file="header.jsp" %>

        <s:form action="Test" method="POST" style="width:90%;">
               Another form here......
        </s:form>
        <s:form action="saveOrUpdateVulMGR" method="POST"
enctype="multipart/form-data" style="width:65%;">
            <s:push value="secVul">
                <s:hidden name="vulId" />
                <s:hidden name="scanHistory.scanId" />
                <s:textfield name="protocol" label="Protocol" style="width:
80%;" />
                <s:textarea name="description" label="Description"
style="width: 80%;" />
                <s:textarea name="solution" label="solution" />
                <more form elements here>sdfasdfasdfasd

*                <s:file name="secImage" label="Image to Upload" size="40"/>
*
                <s:submit value="Add/Update" align="center"/>
            </s:push>
        </s:form>
<%@ include file="footer.jsp" %>
    </body>
</html>


*Here is the struts configuration*

                <interceptor-ref name="fileUpload">
                    <param name="maximumSize">909715200</param>
                    <param name="allowedTypes">

text/xml,application/octet-stream,image/png,image/gif,image/jpeg,image/pjpeg
                    </param>
                </interceptor-ref>
                <interceptor-ref name="defaultStack" />

        <action name="Test" method="saveOrUpdate" class="com.Test">
            <result name="success" type="redirect">listVulsMGR</result>
            <result name="input">/WEB-INF/pages/secVuls.jsp</result>
            <result name="error">/WEB-INF/pages/secVuls.jsp</result>
        </action>

*Action Class Code:*


public class Test extends ActionSupport implements ModelDriven<Test>,
Preparable,ServletRequestAware {

    private HttpServletRequest servletRequest;
    private static final Logger logger =
Logger.getLogger(ListVulsActions.class);


    private File secImage;
    private String secImageContentType;
    private String secImageFileName;


    private HashMap userFilters = new HashMap();
    public Test() {


    }

    public String saveOrUpdate() {
        try {

            ServletContext context =
ServletActionContext.getServletContext();
            String filePath = context.getInitParameter("UploadDirectory");
            logger.debug("Server path:" + filePath);
            logger.debug("this.secImageFileName " + this.secImageFileName);

            File fileToCreate = new File(filePath, this.secImageFileName);

            FileUtils.copyFile(this.secImage, fileToCreate);

}
            addActionMessage("successfully added/updated.");
            return SUCCESS;
        } catch (Exception ex) {
            logger.error("Error in Test", ex);
            addActionError("Error occured in Test. Please contact your
administrator.\n" + ex.getMessage());
            return ERROR;
        }

    }

--------------------------------------------

Not sure what I am doing wrong here.

-- 
------------------------------------
http://www.operationbadar.net

Re: Fileupload misbehaving

Posted by Ahmed Khan <ah...@gmail.com>.
Hi Antonios,

I have have the getter and setter methods. I had missed them below but I
have them.

In fact as I said - the same application has another JSP page and action
where the file upload works fine.

 Ahmed

On Fri, Jul 20, 2012 at 10:08 PM, Antonios Gkogkakis <gk...@tcd.ie>wrote:

> Hi Ahmed,
>
> you have to have a setter for your these fields,
>  private File secImage;
>  private String secImageContentType;
>  private String secImageFileName;
>
> On 20 July 2012 19:40, Ahmed Khan <ah...@gmail.com> wrote:
>
> > Hi,
> >
> > I have a struts 2 application where 2 pages have struts file upload. One
> > works perfectly while the other just wont work. I gives me* "**Invalid
> > field value for field "secImage"*.
> >
> > Here is the code for JSP
> >
> > <%@page contentType="text/html" pageEncoding="UTF-8"%>
> > <%@taglib prefix="s" uri="/struts-tags" %>
> > <%@taglib uri="http://displaytag.sf.net" prefix="display" %>
> >
> > <html>
> >     <head>
> >     </head>
> >     <body>
> >         <%@ include file="header.jsp" %>
> >
> >         <s:form action="Test" method="POST" style="width:90%;">
> >                Another form here......
> >         </s:form>
> >         <s:form action="saveOrUpdateVulMGR" method="POST"
> > enctype="multipart/form-data" style="width:65%;">
> >             <s:push value="secVul">
> >                 <s:hidden name="vulId" />
> >                 <s:hidden name="scanHistory.scanId" />
> >                 <s:textfield name="protocol" label="Protocol"
> style="width:
> > 80%;" />
> >                 <s:textarea name="description" label="Description"
> > style="width: 80%;" />
> >                 <s:textarea name="solution" label="solution" />
> >                 <more form elements here>sdfasdfasdfasd
> >
> > *                <s:file name="secImage" label="Image to Upload"
> > size="40"/>
> > *
> >                 <s:submit value="Add/Update" align="center"/>
> >             </s:push>
> >         </s:form>
> > <%@ include file="footer.jsp" %>
> >     </body>
> > </html>
> >
> >
> > *Here is the struts configuration*
> >
> >                 <interceptor-ref name="fileUpload">
> >                     <param name="maximumSize">909715200</param>
> >                     <param name="allowedTypes">
> >
> >
> >
> text/xml,application/octet-stream,image/png,image/gif,image/jpeg,image/pjpeg
> >                     </param>
> >                 </interceptor-ref>
> >                 <interceptor-ref name="defaultStack" />
> >
> >         <action name="Test" method="saveOrUpdate" class="com.Test">
> >             <result name="success" type="redirect">listVulsMGR</result>
> >             <result name="input">/WEB-INF/pages/secVuls.jsp</result>
> >             <result name="error">/WEB-INF/pages/secVuls.jsp</result>
> >         </action>
> >
> > *Action Class Code:*
> >
> >
> > public class Test extends ActionSupport implements ModelDriven<Test>,
> > Preparable,ServletRequestAware {
> >
> >     private HttpServletRequest servletRequest;
> >     private static final Logger logger =
> > Logger.getLogger(ListVulsActions.class);
> >
> >
> >     private File secImage;
> >     private String secImageContentType;
> >     private String secImageFileName;
> >
> >
> >     private HashMap userFilters = new HashMap();
> >     public Test() {
> >
> >
> >     }
> >
> >     public String saveOrUpdate() {
> >         try {
> >
> >             ServletContext context =
> > ServletActionContext.getServletContext();
> >             String filePath =
> context.getInitParameter("UploadDirectory");
> >             logger.debug("Server path:" + filePath);
> >             logger.debug("this.secImageFileName " +
> this.secImageFileName);
> >
> >             File fileToCreate = new File(filePath,
> this.secImageFileName);
> >
> >             FileUtils.copyFile(this.secImage, fileToCreate);
> >
> > }
> >             addActionMessage("successfully added/updated.");
> >             return SUCCESS;
> >         } catch (Exception ex) {
> >             logger.error("Error in Test", ex);
> >             addActionError("Error occured in Test. Please contact your
> > administrator.\n" + ex.getMessage());
> >             return ERROR;
> >         }
> >
> >     }
> >
> > --------------------------------------------
> >
> > Not sure what I am doing wrong here.
> >
> > --
> > ------------------------------------
> > http://www.operationbadar.net
> >
>



-- 
------------------------------------
http://www.operationbadar.net

Re: Fileupload misbehaving

Posted by Antonios Gkogkakis <gk...@tcd.ie>.
Hi Ahmed,

you have to have a setter for your these fields,
 private File secImage;
 private String secImageContentType;
 private String secImageFileName;

On 20 July 2012 19:40, Ahmed Khan <ah...@gmail.com> wrote:

> Hi,
>
> I have a struts 2 application where 2 pages have struts file upload. One
> works perfectly while the other just wont work. I gives me* "**Invalid
> field value for field "secImage"*.
>
> Here is the code for JSP
>
> <%@page contentType="text/html" pageEncoding="UTF-8"%>
> <%@taglib prefix="s" uri="/struts-tags" %>
> <%@taglib uri="http://displaytag.sf.net" prefix="display" %>
>
> <html>
>     <head>
>     </head>
>     <body>
>         <%@ include file="header.jsp" %>
>
>         <s:form action="Test" method="POST" style="width:90%;">
>                Another form here......
>         </s:form>
>         <s:form action="saveOrUpdateVulMGR" method="POST"
> enctype="multipart/form-data" style="width:65%;">
>             <s:push value="secVul">
>                 <s:hidden name="vulId" />
>                 <s:hidden name="scanHistory.scanId" />
>                 <s:textfield name="protocol" label="Protocol" style="width:
> 80%;" />
>                 <s:textarea name="description" label="Description"
> style="width: 80%;" />
>                 <s:textarea name="solution" label="solution" />
>                 <more form elements here>sdfasdfasdfasd
>
> *                <s:file name="secImage" label="Image to Upload"
> size="40"/>
> *
>                 <s:submit value="Add/Update" align="center"/>
>             </s:push>
>         </s:form>
> <%@ include file="footer.jsp" %>
>     </body>
> </html>
>
>
> *Here is the struts configuration*
>
>                 <interceptor-ref name="fileUpload">
>                     <param name="maximumSize">909715200</param>
>                     <param name="allowedTypes">
>
>
> text/xml,application/octet-stream,image/png,image/gif,image/jpeg,image/pjpeg
>                     </param>
>                 </interceptor-ref>
>                 <interceptor-ref name="defaultStack" />
>
>         <action name="Test" method="saveOrUpdate" class="com.Test">
>             <result name="success" type="redirect">listVulsMGR</result>
>             <result name="input">/WEB-INF/pages/secVuls.jsp</result>
>             <result name="error">/WEB-INF/pages/secVuls.jsp</result>
>         </action>
>
> *Action Class Code:*
>
>
> public class Test extends ActionSupport implements ModelDriven<Test>,
> Preparable,ServletRequestAware {
>
>     private HttpServletRequest servletRequest;
>     private static final Logger logger =
> Logger.getLogger(ListVulsActions.class);
>
>
>     private File secImage;
>     private String secImageContentType;
>     private String secImageFileName;
>
>
>     private HashMap userFilters = new HashMap();
>     public Test() {
>
>
>     }
>
>     public String saveOrUpdate() {
>         try {
>
>             ServletContext context =
> ServletActionContext.getServletContext();
>             String filePath = context.getInitParameter("UploadDirectory");
>             logger.debug("Server path:" + filePath);
>             logger.debug("this.secImageFileName " + this.secImageFileName);
>
>             File fileToCreate = new File(filePath, this.secImageFileName);
>
>             FileUtils.copyFile(this.secImage, fileToCreate);
>
> }
>             addActionMessage("successfully added/updated.");
>             return SUCCESS;
>         } catch (Exception ex) {
>             logger.error("Error in Test", ex);
>             addActionError("Error occured in Test. Please contact your
> administrator.\n" + ex.getMessage());
>             return ERROR;
>         }
>
>     }
>
> --------------------------------------------
>
> Not sure what I am doing wrong here.
>
> --
> ------------------------------------
> http://www.operationbadar.net
>