-->

Struts 2的下载 - 如何动态配置文件名?(Struts 2 Download - How t

2019-09-20 13:52发布

我正在开发一个应用,人们将在数据库中提到他们的本地位置下载所需的文件。 我用struts 2从服务器下载文件。 我可以下载该文件没有任何异常,它完美的作品。 但这些文件是下载有我在struts.xml中指定的文件名,我想这是我这下载确切的文件名。 例如,如果原来的文件名是struts.pdf,我下载它作为download.pdf,如何预防和下载实际文件名的文件

我struts.xml的配置如下,

<action name="download" class="action.DownloadAction">
        <result name="success" type="stream">
            <param name="contentType">application/octet-stream</param>
            <param name="inputName">fileInputStream</param>
            <param name="contentDisposition">attachment;filename="download.log"</param>
            <param name="bufferSize">1024</param>
        </result>
        <result name="error">/live/useradminerror.jsp</result>
    </action> 

我也忘了我使用的struts2-的jQuery开发的UI。请帮我在这,我也是如此在我的项目中非常关键的阶段,就更不用说了。

Answer 1:

如果我是正确你想通过它被存储在你的数据库文件,如果这是你可以很容易地从你的动作类传递所有的参数,如做到这一点的情况下,

class MyFileDownloadAction extends ActionSupport{

     private String fileName;
     // getter and setter

    public String fileDownload() throws exception{
      // file download logic
      fileName ="abc"  // can set name dynamic from DB
   }

}

<action name="download" class="action.DownloadAction">
        <result name="success" type="stream">
            <param name="contentType">application/octet-stream</param>
            <param name="inputName">fileInputStream</param>
            <param name="contentDisposition">attachment;filename="${filename}"</param>
            <param name="bufferSize">1024</param>
        </result>
        <result name="error">/live/useradminerror.jsp</result>
    </action> 

您可以动态传递的每个参数在你的struts.xml class.Hope这将帮助你,这是你将如何在你的XML使用这个文件名



Answer 2:

对于支柱注释,其相同。 该解决方案是非常有益的。 谢谢。 该“的contentType”对我来说并没有太大的差别。

@Action(value = "/download", results = { @Result(name = "success", type = "stream", 
params= {"contentType", "application/octet-stream", "inputName","fileInputStream",    
"contentDisposition","attachment; filename=\"${fileName}\"", "bufferSize", "1024" })
})


文章来源: Struts 2 Download - How to configure the file name dynamically?