Struts2 File Download (0): File Download

Source: Internet
Author: User

Struts2 providesStreamResult type. The result type supports the file download function. When specifying the stream result type, you must specifyInputnameParameter. This parameter specifies an input stream, which is the entry to the downloaded file. The file downloading support of struts2 allows the system to control the permission of the viewer to download files, including downloading files with non-western European characters.

Index. jsp:

<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

Struts. xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"    "http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts>    <package name="demo" extends="struts-default">        <action name="download" class="action.DownloadAction">                     <param name="fileName">vi.jpg</param>            <param name="fileDir">/uploadDir</param>                        <result name="success" type="stream">                <param name="contentType">image/jpeg</param>                <param name="inputName">targetFile</param>                <param name="contentDisposition">attachment;filename=${fileName}</param>            </result>        </action>    </package></struts>

The action for configuring the downloaded file is not much different from that for configuring common actions. The key is to configure a result of the stream type. The result of the stream type will be downloaded as a response. To configure the stream type, you must specify the following four attributes:

① Contenttype: Specifies the file type of the downloaded object.

② Inputname: Specifies the input stream of the object to be downloaded.

③ Contentdisposition: Specifies the name of the downloaded file and the method in which the file is downloaded. Attachment indicates that the file is downloaded as an attachment.

④ Buffersize: Specifies the buffer size for downloading files.

The logic view of the stream result type returns an input stream to the client, so you do not need to specify the location attribute.

Downloadaction. Java:

Public class downloadaction extends actionsupport {private string filename; private string filedir; private inputstream targetfile; Public String getfilename () {return filename;} public void setfilename (string filename) {This. filename = filename;} Public String getfiledir () {return filedir;} public void setfiledir (string filedir) {This. filedir = filedir;} public inputstream gettargetfile () {system. out. println ("Enter the gettargetfile method"); Return targetfile;} public void settargetfile (inputstream targetfile) {system. out. println ("Enter the settargetfile method" using repeated this.tar GetFile = targetfile;} Public String execute () {system. out. println ("Enter the execute method... "); servletcontext context = servletactioncontext. getservletcontext (); string filepath = context. getrealpath (getfiledir () + file. separator + getfilename (); try {targetfile = new fileinputstream (filepath); Return "success";} catch (filenotfoundexception e) {e. printstacktrace (); Return "fail ";}}}

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.