Struts 2 Download - How to configure the file name dynamically?

ぃ、小莉子 提交于 2019-12-05 07:48:08

IF i am correct you want to pass the file which is being stored in your DB, if this is the case you can easily do this by passing all those parameters from you action class like

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> 

You can pass each parameter dynamically in your struts.xml class.Hope this will help you This is how you will use this file name in your XML

For annotations in struts, its same. The solution was very helpful. Thank you. The "contentType" for me did not make much difference.

@Action(value = "/download", results = { @Result(name = "success", type = "stream", 
params= {"contentType", "application/octet-stream", "inputName","fileInputStream",    
"contentDisposition","attachment; filename=\"${fileName}\"", "bufferSize", "1024" })
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!