• <i id='A0SWR'><tr id='A0SWR'><dt id='A0SWR'><q id='A0SWR'><span id='A0SWR'><b id='A0SWR'><form id='A0SWR'><ins id='A0SWR'></ins><ul id='A0SWR'></ul><sub id='A0SWR'></sub></form><legend id='A0SWR'></legend><bdo id='A0SWR'><pre id='A0SWR'><center id='A0SWR'></center></pre></bdo></b><th id='A0SWR'></th></span></q></dt></tr></i><div id='A0SWR'><tfoot id='A0SWR'></tfoot><dl id='A0SWR'><fieldset id='A0SWR'></fieldset></dl></div>
    <legend id='A0SWR'><style id='A0SWR'><dir id='A0SWR'><q id='A0SWR'></q></dir></style></legend>

  • <tfoot id='A0SWR'></tfoot>

        • <bdo id='A0SWR'></bdo><ul id='A0SWR'></ul>

        <small id='A0SWR'></small><noframes id='A0SWR'>

        Struts2 Fileupload 在动作类中给出空文件

        Struts2 Fileupload giving null file in the action class(Struts2 Fileupload 在动作类中给出空文件)

        1. <tfoot id='hnheO'></tfoot>
          <legend id='hnheO'><style id='hnheO'><dir id='hnheO'><q id='hnheO'></q></dir></style></legend>
            <tbody id='hnheO'></tbody>

            <i id='hnheO'><tr id='hnheO'><dt id='hnheO'><q id='hnheO'><span id='hnheO'><b id='hnheO'><form id='hnheO'><ins id='hnheO'></ins><ul id='hnheO'></ul><sub id='hnheO'></sub></form><legend id='hnheO'></legend><bdo id='hnheO'><pre id='hnheO'><center id='hnheO'></center></pre></bdo></b><th id='hnheO'></th></span></q></dt></tr></i><div id='hnheO'><tfoot id='hnheO'></tfoot><dl id='hnheO'><fieldset id='hnheO'></fieldset></dl></div>

              <small id='hnheO'></small><noframes id='hnheO'>

                • <bdo id='hnheO'></bdo><ul id='hnheO'></ul>
                • 本文介绍了Struts2 Fileupload 在动作类中给出空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 struts2 fileUpload 拦截器在我的 Web 应用程序中实现文件上传过程.以下是我在

                  I am trying to implement the file upload process in my web application using struts2 fileUpload interceptor. below is my code in

                  index.jsp

                  <tags:form action="fileUpload" method="post" enctype="multipart/form-data">
                     <tags:file name="fileUpload" label="Choose File"/>
                     <tags:submit value="Upload"/>     
                  </tags:form> 
                  

                  struts.xml

                  <action name="fileUpload" class="com.hibernate.action.FileUploadAction">
                      <interceptor-ref name="fileUploadStack"/>
                      <interceptor-ref name="fileUpload">
                          <param name="maximumSize">1024000</param>
                          <param name="allowedTypes">application/pdf</param>
                      </interceptor-ref>
                      <result name="success">/viewChapters.jsp</result>
                  </action>
                  

                  FileUploadAction.java

                  FileUploadAction.java

                  public class FileUploadAction extends ActionSupport
                  {
                  private File fileUpload;
                  private String contentType;
                  private String fileName;
                  private String destPath;
                  /// setter and getter methods
                   public String execute()
                  {
                      destPath="C:\WebPortal_testing";
                      try
                      {
                          System.out.println("Source File Name:"+fileUpload);
                          System.out.println("Destination File Name:"+fileName);
                  
                          File destFile= new File(destPath,fileName);
                          FileUtils.copyFile(fileUpload, destFile);
                      }
                      catch(IOException exception)
                      {
                          exception.printStackTrace();
                          return ERROR;
                      }
                      return SUCCESS;
                   }
                  

                  当我在 index.jsp 页面中选择一个 pdf 文件并单击上传按钮时,它会为操作类的 fileUpload 字段提供空值.

                  when I select a pdf file in the index.jsp page and click on upload button it is giving null value to the fileUpload field of the action class.

                  我正在调试模式下执行应用程序并给出了这个

                  I am executing the application in debug mode and gave this

                  System.out.println("Source File Name:"+fileUpload);
                  

                  检查它返回的内容,我得到空值.

                  to check what it is returning and I am getting null.

                  推荐答案

                  1.拦截器配置错误

                  FileUploadStack 是:

                  <!-- Sample file upload stack -->
                  <interceptor-stack name="fileUploadStack">
                      <interceptor-ref name="fileUpload"/>
                      <interceptor-ref name="basicStack"/>
                  </interceptor-stack>
                  

                  那么你真正定义的是:

                      <interceptor-ref name="fileUpload"/>
                      <interceptor-ref name="basicStack"/>
                      <interceptor-ref name="fileUpload">
                          <param name="maximumSize">1024000</param>
                          <param name="allowedTypes">application/pdf</param>
                      </interceptor-ref>
                  

                  使用

                  • 文件上传拦截器的两倍
                  • 仅将您对 maximumSize 和 allowedTypes 的限制应用于第二个.

                  做吧

                  <interceptor-ref name="fileUploadStack">
                      <param name="fileUpload.maximumSize">1024000</param>
                      <param name="fileUpload.allowedTypes">application/pdf</param>
                  </interceptor-ref>
                  

                  <小时>

                  <强>2.文件属性错误

                  内容类型和文件名属性必须以文件属性名开头.

                  Content type and file name attributes must start with the File attribute name.

                  在你的情况下:

                  private File fileUpload;
                  private String fileUploadContentType;
                  private String fileUploadFileName;
                  

                  您可以在 this question 上找到完整示例.

                  You can find a full example on this question.

                  3.您正在打印文件而不是文件名

                  System.out.println("Source File Name:"+fileUpload);
                  

                  那是文件,而不是文件名,顺便说一句,文件名是在另一个变量中传递的.

                  That is the file, not the filename, and btw the filename is passed in the other variable.

                  修复此问题并重试.另请注意,当全世界都在使用 <s: 时,使用 作为前缀是不安全的.这样做没有任何好处,只有并发症.只需使用 <s:.

                  Fix this and retry. Also note that is not safe to use <tags: as prefix when the whole world is using <s:. There's no gain in doing that, only complications. Just use <s:.

                  这篇关于Struts2 Fileupload 在动作类中给出空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                    <tfoot id='CHQwu'></tfoot>

                        • <legend id='CHQwu'><style id='CHQwu'><dir id='CHQwu'><q id='CHQwu'></q></dir></style></legend>
                            <bdo id='CHQwu'></bdo><ul id='CHQwu'></ul>
                          • <small id='CHQwu'></small><noframes id='CHQwu'>

                            <i id='CHQwu'><tr id='CHQwu'><dt id='CHQwu'><q id='CHQwu'><span id='CHQwu'><b id='CHQwu'><form id='CHQwu'><ins id='CHQwu'></ins><ul id='CHQwu'></ul><sub id='CHQwu'></sub></form><legend id='CHQwu'></legend><bdo id='CHQwu'><pre id='CHQwu'><center id='CHQwu'></center></pre></bdo></b><th id='CHQwu'></th></span></q></dt></tr></i><div id='CHQwu'><tfoot id='CHQwu'></tfoot><dl id='CHQwu'><fieldset id='CHQwu'></fieldset></dl></div>
                              <tbody id='CHQwu'></tbody>