java Struts 2 下载 - 如何动态配置文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11752702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:14:20  来源:igfitidea点击:

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

javajquerystruts2struts-configstruts-validation

提问by Esh

I am developing one application , where people will download the required file from a location mentioned in the DB to their Local. I am using struts 2 for downloading the file from the server . I can download the file without any exception and it works perfectly. But the files am download has the filename i specified in struts.xml , i want it to be the exact filename which am downloading . example if the original file name is struts.pdf , i am downloading it as download.pdf, how to prevent it and download the file with actual filename

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

My struts.xml configuration as follows ,

我的 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> 

And i forgot to mention am using struts2-jquery for developing the UI .Please help me in this , as am in very critical stage of my project .

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

回答by Umesh Awasthi

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

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

回答by man.2067067

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

对于struts中的注解,也是一样的。该解决方案非常有帮助。谢谢你。“contentType”对我来说没有太大区别。

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