Struts2 File download
1 Public classDownloadactionextendsactionsupport{2 3 Private Static Final LongSerialversionuid = 1L;4 //the file name to download5 PrivateString filename;6 PublicString GetFileName () {7 returnfilename;8 }9 Public voidsetfilename (String filename) {Ten This. FileName =filename; One } A - /** - * The method name of this method is determined by the property value of the type of result corresponding to the properties of the class. the * (InputName) - * @throwsFileNotFoundException - */ - PublicInputStream getInputStream ()throwsFileNotFoundException { +String path = Servletactioncontext.getrequest (). Getrealpath ("/download/"); - return NewFileInputStream (NewFile (path, filename)); + } A at /** - * struts2 inside the download - * We actually visited the page just to do a page jump - */ - PublicString Stream () { - returnSUCCESS; in } -}
Struts.xml
1 <!DOCTYPE Struts public2 "-//apache software foundation//dtd Struts Configuration 2.3//en"3 "Http://struts.apache.org/dtds/struts-2.3.dtd">4 <Struts>5 <!--Configure Constants -6 <!--Configuring STRUTS2 Encoding constants only for post submission methods7 the garbled problem of get submit method need us to solve manually8 -9 <constantname= "Struts.i18n.encoding"value= "UTF-8"/>Ten <!--Configure the suffix name of the access action - One <constantname= "Struts.action.extension"value= "Action,,"/> A <!-- - STRUTS.DEVMODE:STRUTS2 's developer model - in the development phase: we are generally set to true, so that when we modify the Struts.xml, we do not have to restart the server the production environment, set to False - - - <constantname= "Struts.devmode"value= "true"/> - <!--To introduce an associated configuration file - + <includefile= "Struts-json.xml"/> - <includefile= "Struts-upload.xml"/> + <includefile= "Struts-download.xml"/> A </Struts>
Struts-download.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Struts public3 "-//apache software foundation//dtd Struts Configuration 2.3//en"4 "Http://struts.apache.org/dtds/struts-2.3.dtd">5 <Struts>6 <constantname= "Struts.multipart.maxSize"value= "1000000000"/>7 < Packagename= "Bamaw_download"namespace="/"extends= "Struts-default">8 <Actionname= "Stream"class= "Com.tbamaw.web.action.DownLoadAction">9 <resulttype= "Stream"name= "Success">Ten <paramname= "Contentdisposition">Attachment;filename=${filename}</param> One </result> A </Action> - </ Package> - </Struts>
1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Insert Title here</title>8 </Head>9 <Body>Ten <ahref= "${pagecontext.request.contextpath}/stream.action?filename=baba.jpg">Download</a> One </Body> A </HTML>
servlet-api Way file download 1. EstablishDownloadaction
2. ConfigurationStruts.xml
3. send file download request
Struts2 file Download <*>servlet-api way file download