Download of struts2--file

Source: Internet
Author: User

Learn struts2 how to download files with a personal struts2 case

1.<!--configured in action-

<result name= "Success" type= "Stream" >

Types of files downloaded

<param name= "ContentType" >image/jpeg</param>

File input stream source

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

Set the file name of the saved files

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

Buffer size

<param name= "BufferSize" >1024</param>

</result>

1.1. The above parameters can be provided by getter Mode in Action! < no longer set parameters in Result >

1.2 The following case assigns a value to a variable by getter

2.javabean Concrete Implementation Code (case)

Package com.cn.web;


Import Java.io.File;

Import Java.io.FileInputStream;

Import java.io.IOException;

Import Java.io.InputStream;


Import Org.apache.struts2.ServletActionContext;


Import Com.opensymphony.xwork2.ActionSupport;


public class DownloadFile extends Actionsupport {


/**

* struts2 File Download image

*/

Private static final long serialversionuid = 1L;

Get file name

Private String imgname;

Types of files downloaded

Private String ContentType;

Length of file downloaded

Private long contentlength;

Set the file name of the saved files

Private String contentdisposition;

File input stream source

Private InputStream InputStream;


Public String getContentType () {

return contentType;

}


public void setContentType (String contentType) {

This.contenttype = ContentType;

}

Public long getcontentlength () {

return contentlength;

}


public void Setcontentlength (long contentlength) {

This.contentlength = ContentLength;

}


Public String getcontentdisposition () {

return contentdisposition;

}

public void Setcontentdisposition (String contentdisposition) {

This.contentdisposition = contentdisposition;

}

Public String Getimgname () {

return imgname;

}


public void Setimgname (String imgname) {

This.imgname = imgname;

}


Public InputStream getInputStream () {

return inputstream;

}


public void Setinputstream (InputStream inputstream) {

This.inputstream = InputStream;

}


Get the downloaded view

Public String Downloadui () {


return "Downloadui";

}


Public String Execute () throws IOException {

Determining the values of individual member variables

ContentType = "Image/jpeg";

Contentdisposition = "Attachment;filename=" + imgname + ". jpg";

String newsrc = Servletactioncontext.getservletcontext (). Getrealpath (

"/web-inf/upload" + file.separator + imgname + ". jpg");

System.out.println (NEWSRC);

InputStream = new FileInputStream (NEWSRC);

ContentLength = Inputstream.available ();


return SUCCESS;

}

}

3. Open the Struts2.xml file and configure the <result name= "success" type= "stream" in the appropriate action ></result>

<action name= "down_*" class= "Com.cn.web.DownloadFile" method= "{1}" >

<result name= "{1}" >/WEB-INF/downloadUI.jsp</result>

<!--provide content for users to download-

<result name= "Success" type= "Stream" >

<!--buffer Size--

<param name= "BufferSize" >1024</param>

</result>

</action>



4.html Code

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"

Contenttype= "text/html; Charset=utf-8 "%>

<%@ taglib uri= "/struts-tags" prefix= "s"%>


<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >



<title>struts2 File Download </title>


<meta http-equiv= "Pragma" content= "No-cache" >

<meta http-equiv= "Cache-control" content= "No-cache" >

<meta http-equiv= "Expires" content= "0" >

<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >

<meta http-equiv= "description" content= "This is my page" >



<body>


<a href= "down_execute?imgname=886001c3-335b-4604-b547-a659f55ddf76" > File download 1:</a>

<br/>

<a href= "down_execute?imgname=eff2d51f-192a-451c-be06-050fb6529ffd" > File download 2:</a>

<br>

<a href= "down_execute?imgname=e813e7ad-18dd-461a-87cf-2b035d41026d" > File download 3:</a>

<br>



</body>



This article is from the "12276706" blog, please be sure to keep this source http://12286706.blog.51cto.com/12276706/1872149

Download of struts2--file

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.