标签:style class blog code java http
struts2
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>blk</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
/WEB-INF/classes/struts.xml
<default-action-ref name="download"/> <global-results> <result name="sql">/exceptionHandle/exception.jsp</result> <result name="root">/exceptionHandle/exception.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.sql.SQLException" result="sql"/> <exception-mapping exception="java.lang.Exception" result="root"/> </global-exception-mappings> <action name="download" class="down.FileDownloadAction"> <param name="inputPath">\down\images\文件.zip</param> <result name="success" type="stream"> <param name="contentType">application/zip</param> <param name="inputName">targetFile</param> <param name="contentDisposition">filename="文件.zip"</param> <param name="bufferSize">4096</param> </result> </action>
/down/strutsDown.html
<html> <head> <title>Struts2的文件下载</title> </head> <body> <h1>Struts2的文件下载</h1> 下载Struts2的Logo:<a href="download.action">下载文件</a> </body> </html>
down.FileDownloadAction
package down; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class FileDownloadAction implements Action { private String inputPath; public void setInputPath(String value) { inputPath = value; } /* 下载用的Action应该返回一个InputStream实例, 该方法对应在result里的inputName属性值为targetFile */ public InputStream getTargetFile() throws Exception { return ServletActionContext.getServletContext().getResourceAsStream(inputPath); } public String execute() throws Exception { return SUCCESS; } }
下载的时候如果文件名是中文就变成zip.zip,布布扣,bubuko.com
标签:style class blog code java http
原文地址:http://www.cnblogs.com/qrlozte/p/3782378.html