Struts File Download

Source: Internet
Author: User

=============================struts File Download ==================================

Step One: JSP page
<a href= "Download.action? fileName=img_0443.jpg "> Click here to download images </a>

Step Two: Action page

Package org.zm.action;

Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.InputStream;

Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionSupport;

public class Downloadaction extends actionsupport{
Read the directory of downloaded files
Private String InputPath;
filename of the download file
Private String FileName;
Read the input stream of the downloaded file
Private InputStream InputStream;

Types of download files
Private String Conetnttype;

Create InputStream input stream
Public InputStream getInputStream () throws filenotfoundexception{
String Path=servletactioncontext.getservletcontext ().
Getrealpath (InputPath);
return new Bufferedinputstream (new FileInputStream (path+ "\ \" +
FileName));
}

@Override
Public String execute () {
return SUCCESS;
}
public void Setinputstream (InputStream inputstream) {
This.inputstream = InputStream;
}
Public String GetFileName () {
return fileName;
}
public void Setfilename (String fileName) {
This.filename = FileName;
}

Public String Getconetnttype () {
return conetnttype;
}


Public String Getinputpath () {
return inputpath;
}


public void Setinputpath (String inputpath) {
This.inputpath = InputPath;
}
public void Setconetnttype (String conetnttype) {
This.conetnttype = Conetnttype;
}
}

Step Three: Struts.xml file
<action name= "Download" class= "Org.zm.action.DownLoadAction" >
<param name= "InputPath" >/upload</param>
<result name= "Success" type= "Stream" >
<param name= "ContentType" >image/pjpeg</param>
<param name= "InputName" >inputStream</param>
<param name= "contentdisposition" >attachment;filename= "${filename}" </param>
<param name= "BufferSize" >4096</param>
</result>
</action>


Hint: Stream result type
ContentType: Sets the MIME type to send to the browser
ContentLength: File Size
Contentdisposition: Sets the value of the Content-disposition parameter for the HTTP header information of the response
InputName: Specifies the property name of the InputStream type provided by the action
BufferSize: Set the size of the buffer when reading and downloading files

The variable names in both locations of the Struts.xml file and action must be correct.

Struts File Download

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.