Struts2 (b)

Source: Internet
Author: User

1:stream

<result-type name="stream" class="Org.apache.struts2.result.StreamResult"/>

A custom Result type for sending raw data (via an InputStream) directly to the HttpServletResponse. Very useful for allowing users to download content.

This result type takes the following parameters:

    • ContentType -The stream Mime-type as sent to the Web browser (default = Text/plain).
    • contentlength -The stream length in bytes (The browser displays a progress bar).
    • contentdisposition -The content disposition header value for specifing the file name (default = inline, values a Re typically attachment;filename= "Document.pdf".
    • InputName -the name of the InputStream property from the chained action (default = InputStream).

Step 1: Develop an actionthat provides a getinputstream (): InputStream

Public class downaction extends Actionsupport {

@Override

Public String Execute () throws Exception {

System. err. println (" Judging the user's points, judging the state of the user . ");

return SUCCESS;

}

Public InputStream getInputStream () throws Exception {

String Path = Servletactioncontext. Getservletcontext (). Getrealpath ("/files/yy.zip");

InputStream in = new fileinputstream (path);

return in;

}

}

Step 2: Configure to stuts.xml and return the <result type="stream"/>

<action name= "Down" class="Cn.down.DownAction">

<result type="stream"></result>

</action>

<action name= "Down" class="Cn.down.DownAction">

<result type="Stream">

<param name="ContentType">application/force-download</param>

<param name="contentdisposition">attachment;filename= "Document.zip" </param>

</result>

</action>

Additional:

1: Modify name when downloading

String name = " my file . zip";

Name = Urlencoder. encode (Name, "UTF-8");

Actioncontext. GetContext (). Put ("FileName", name);

To take a value in XML :

use OGNL in XML to take a value, is through ${...}

<action name= "Down" class="Cn.struts2.down.DownAction">

<result type="Stream">

<param name="ContentType">application/force-download</param>

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

</result>

</action>

-- Specify the method name by inputname :

<action name= "Down" class="Cn.struts2.down.DownAction">

<result type="Stream">

<param name="ContentType">application/force-download</param>

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

<!-- in downaction.getdownfile (): InputStream--

<param name="InputName">downfile</param>

</result>

</action>

Public InputStream Getdownfile () throws Exception {

String Path = Servletactioncontext. Getservletcontext (). Getrealpath ("/files/yy.zip");

InputStream in = new fileinputstream (path);

return in;

}

Struts2 (b)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.