When migrating to Struts2 2.3.14.2, Stream result doesn't work anymore

前端 未结 1 482
逝去的感伤
逝去的感伤 2021-01-29 12:04

After updating to struts2-core-2.3.14.2, it gives me the following error during a download with Stream result:

Stacktrace

Servlet.service() for servlet         


        
相关标签:
1条回答
  • 2021-01-29 12:31

    It seems fine except that for a couple of things:

    1. Specify the contentType:

      <result name="success" type="stream">
         <param name="contentType">text/plain</param>
         <param name="contentDisposition">attachment;filename="${fileName}"</param>
         <param name="inputName">downloadFile</param>
      </result>
      

      text/plain is the default, and in your example you have a txt file, but it is better to explicit it in the configuration;

    2. Follow the JavaBeans Conventions; a getter should not have logic, and the name should match the one of the private variable returned:

      For example,

      String foo;
      public String getBar(){ 
          return this.foo; 
      }
      

      is not valid and may cause problems to Struts2 (that makes use of Reflection internally)

      Then try changing your Action code like this:

      String filePathName="/home/download/aaa.txt";
      String fileName;
      InputStream donwloadFile;
      
      public String getFileName() {
         return this.fileName;
      }
      
      public InputStream getDownloadFile() {
          return this.downloadFile;
      }
      
      public String download() throws Exception {
         File file = new File(filePathName);       
         this.fileName = file.getName();
         this.downloadFile = new BufferedInputStream(new FileInputStream(file));
         return SUCCESS;
      }
      
    0 讨论(0)
提交回复
热议问题