java 在 Struts2 中使用流结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257566/
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-29 11:39:52  来源:igfitidea点击:

Using Stream Result with Struts2

javastruts2resulttype

提问by Vincent Ramdhanie

I am attempting to use a Stream Result to return an image from a struts2 application. I seem to be having problem with configuring the action. Here is the configuration:

我正在尝试使用流结果从 struts2 应用程序返回图像。我似乎在配置操作时遇到问题。这是配置:

    <result name="success" type="stream">
            <param name="contentType">image/jpeg</param>
            <param name="inputName">inputStream</param>
            <param name="contentDisposition">filename="${filename}"</param>
            <param name="bufferSize">1024</param>
    </result>

The problem seem to be the inputName parameter which according to the docs is:

问题似乎是 inputName 参数,根据文档是:

the name of the InputStream property from the chained action (default = inputStream).

来自链式操作的 InputStream 属性的名称(默认值 = inputStream)。

I am not sure what name I should put there. The error I get is:

我不确定我应该把什么名字放在那里。我得到的错误是:

Can not find a java.io.InputStream with the name [inputStream] in the invocation stack.

在调用堆栈中找不到名称为 [inputStream] 的 java.io.InputStream。

Has anyone used this before? Any advice?

有没有人以前用过这个?有什么建议吗?

Thanks.

谢谢。

回答by

I believe you have the contentDisposition wrong, it should be:

我相信你的 contentDisposition 错了,应该是:

<param name="contentDisposition">attachment; filename="${filename}"</param>

(Chris)

(克里斯)

回答by Vincent Ramdhanie

I found thiswhich explained that the InputStreamhas to be created by me. It makes sense that I create an InputStreamfrom the file that I want the user to download and then pass the Stream to the result. I guess that's my answer.

我发现解释了InputStream必须由我创建。我InputStream从希望用户下载的文件中创建一个,然后将 Stream 传递给结果是有道理的。我想这就是我的答案。

回答by Vincent Ramdhanie

Inputname defines the name of the method that outputs the "stream"

Inputname 定义输出“流”的方法的名称

public InputStream getInputStream () { return new ByteArrayInputStream ( _bytes ); }

public InputStream getInputStream () { return new ByteArrayInputStream ( _bytes ); }