2017-07-13 25 views
0

問題是在Java中輸入文件值到達空值。沒有錯誤,其工作正常,但輸入文件的java變量爲空。來自輸入文件的值在java模型中爲null struts2

此代碼打印我在enviar()方法中放置的記錄器「value is:null」。

怎麼了?

編輯:新增公地文件上傳-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> 
+0

這個問題爲什麼用2種不同的語言寫成?你能把前兩段翻譯成英文嗎?或者,將其他句子翻譯成西班牙語,然後在西班牙語網站上提問:https://es.stackoverflow.com/。 – cybersam

+0

因爲我需要所有我能得到的支持。也許有人知道答案,但不會說英語或viseversa – user2930137

+0

哦,我明白了,它的好,我已經編輯它。無論如何,我需要幫助 – user2930137

回答

0

我不能重現這個例子,但我會嘗試設置通過文件上傳的攔截器的允許類型的文件,包括DefaultStack的一部分,沒有在JSP,所以:在

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

注意參考文件上傳名稱參數。

相關問題