Struts 2下载-如何动态配置文件名? [英] Struts 2 Download - How to configure the file name dynamically?

查看:111
本文介绍了Struts 2下载-如何动态配置文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.xml配置如下,

My struts.xml configuration as follows ,

<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的情况.在我项目的关键阶段,请帮助我.

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 .

推荐答案

如果我是对的,那么您想传递存储在数据库中的文件,如果是这种情况,则可以通过传递所有这些参数来轻松实现从您的动作课中获得

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> 

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

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 2下载-如何动态配置文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆