Struts 2 Download – 如何动态配置文件名?

我正在开发一个应用程序,人们将所需的文件从数据库中提到的位置下载到本地。 我使用struts 2从服务器下载文件。 我可以毫无例外地下载文件,它完美无缺。 但是下载的文件具有我在struts.xml中指定的文件名,我希望它是下载的确切文件名。 例如,如果原始文件名是struts.pdf,我将其下载为download.pdf,如何防止它并下载具有实际文件名的文件

我的struts.xml配置如下,

  application/octet-stream fileInputStream attachment;filename="download.log" 1024  /live/useradminerror.jsp  

我忘了提到使用struts2-jquery来开发UI。请帮助我,因为我处于项目非常关键的阶段。

如果我是正确的你想要传递存储在你的数据库中的文件,如果是这种情况你可以通过从你的动作类传递所有这些参数来轻松地做到这一点

 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 } }   application/octet-stream fileInputStream attachment;filename="${filename}" 1024  /live/useradminerror.jsp  

你可以在struts.xml类中动态传递每个参数。希望这对你有帮助。这就是你在XML中使用这个文件名的方法

对于struts中的注释,同样如此。 解决方案非常有用。 谢谢。 对我来说,“contentType”并没有太大的区别。

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