如何在 Struts2 中定义 StreamResult 的输出名称? [英] How can I define the output name of a StreamResult in Struts2?
问题描述
伙计们,我无法在网络上清楚地找到此信息.我有一个动作,我正在生成一个文本文件,但是在客户端总是显示为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 的输出名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!