如何在 Struts2 中定义 StreamResult 的输出名称?

编程入门 行业动态 更新时间:2024-10-24 12:25:05
本文介绍了如何在 Struts2 中定义 StreamResult 的输出名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

伙计们,我无法在网络上清楚地找到此信息.我有一个动作,我正在生成一个文本文件,但是在客户端总是显示为generatePDF.action"文件.我希望它显示为receipt.txt 文件.

Guys, I cant find this info clearly in the web. I have an action and I'm generating a text file, however is always appearing to the client as a "generatePDF.action" file. I want it to show as a receipt.txt file.

这是我的注释:

@Action(value = "/generateTXT", results = { @Result(name = "ok", type = "stream", params = {"inputName", "inputStream", "contentType", "application/octet-stream", "contentDispostion", "attachment;filename=receipt.txt"}) })

推荐答案

如果您使用的是约定插件,那么让我们使用以下代码作为参考在/YourApplicationContext/stream/stream-test"下运行,然后解析为"/YourApplicationContext/stream/document.txt":

If you are using the conventions plug-in then lets use the following code for reference runs under "/YourApplicationContext/stream/stream-test" which then resolves to "/YourApplicationContext/stream/document.txt":

package struts2.stream; import com.opensymphony.xwork2.ActionSupport; import java.io.InputStream; import java.io.StringBufferInputStream; import org.apache.struts2.convention.annotation.Result; @Result(name = ActionSupport.SUCCESS, type = "stream", params = { "contentType", "text/hmtl", "inputName", "inputStream", "contentDisposition", "filename=document.txt" }) public class StreamTestAction extends ActionSupport{ public InputStream inputStream; @Override public String execute(){ inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action."); return SUCCESS; } }

请注意contentDisposition",其值已设置为filename='document.txt'",更改document.txt"即可得到您想要的.

Please take note of "contentDisposition" and that its value has been set to "filename='document.txt'" changing 'document.txt' gets you what you want.

更多推荐

如何在 Struts2 中定义 StreamResult 的输出名称?

本文发布于:2023-11-16 21:11:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   名称   如何在   StreamResult

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!