输入文件中的值在java模型struts2中为null

时间:2017-07-13 19:32:33

标签: file-upload struts2

问题是在Java中输入文件值为空。没有错误,它正常工作,但输入文件的java中的变量为空。

此代码打印我在enviar()方法中放置的记录器“值为:null”。

怎么了?

编辑:添加了commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar

不要工作

JSP:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@include file="../../ArchivosComunes/recursosComunes.inc" %>
<!DOCTYPE HTML>
<html>
  <head>
    <title>Imágenes</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" 

  </head>

  <body>
    <s:form action="subirFicheroAction" method="POST" enctype="multipart/form-data">

            <s:text name='label'/>:<s:file name="uploadFichero" accept="application/pdf"/>

            <s:submit key="send"/>
        </s:form>
        <s:if test='uploadFicheroFileName != null'>
        <br>
        uploadFicheroFileName : <s:property value="uploadFicheroFileName" />

        <br> 
        uploadFicheroContentType :  <s:property value="uploadFicheroContentType" />
        </s:if>
  </body>
</html>

我的行动:

public class ImagesPagoExpress extends ActionSupport{
    static final Logger logger = Logger.getLogger(ImagesPagoExpress.class);

    public String execute(){
return SUCCESS;
    }

    private File uploadFichero;
    private String uploadFicheroFileName;
    private String uploadFicheroContentType;    

    /** ACCIONES **/

    public String inicio() 
    {
        return INPUT;
    }

    public String enviar()
    {
        logger.info("Ps sí estoy en enviar "+this.uploadFichero); 
        if (uploadFichero == null) //entra aqui porque llega null
        {
            return INPUT;
        }
        else
        {
            logger.info("Llegó");
            logger.info(uploadFicheroFileName);
            return SUCCESS;
        }
    }

    /** GETTERS Y SETTERS **/
    public File getUploadFichero()
    {
        return uploadFichero;
    }

    public void setUploadFichero(File uploadFichero)
    {
        this.uploadFichero = uploadFichero;
    }

    public String getUploadFicheroFileName()
    {
        return uploadFicheroFileName;
    }

    public void setUploadFicheroFileName(String uploadFicheroFileName)
    {
        this.uploadFicheroFileName = uploadFicheroFileName;
    }

    public String getUploadFicheroContentType()
    {
        return uploadFicheroContentType;
    }

    public void setUploadFicheroContentType(String uploadFicheroContentType)
    {


     this.uploadFicheroContentType = uploadFicheroContentType;
        }

}

struts.xml中

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="LATIN1" />
<package name="default" namespace="/" extends="struts-default">
    <action name="subirFicheroAction" class="actions.atencion.ImagesPagoExpress" method="enviar">


            <interceptor-ref name="defaultStack" />

            <result name="input">/JSP/Atencion/ImagesPagoExpress.jsp</result>
            <result name="success">/JSP/Atencion/ImagesPagoExpress.jsp</result>

        </action>
    </package>
</struts>

1 个答案:

答案 0 :(得分:0)

我无法重现这个例子,但我会尝试通过fileUpload拦截器设置allow类型文件,包含DefaultStack的一部分,在jsp中没有,所以:

<interceptor-ref name="defaultStack">
        <param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref> 

请注意name参数中对fileUpload的引用。