struts2的文件下载以及在线浏览
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<!-- Add packages here -->
</struts>
example.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/example" extends="struts-default">
<action name="download" type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="struts.gif"</param>
<param name="bufferSize">4096</param>
</result>
</action>
<action name="download2" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="struts-gif.zip"</param>
<param name="bufferSize">4096</param>
</result>
</action>
<!-- Add actions here -->
</package>
</struts>
FileDownloadAction
package example;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* Demonstrates file resource download.
* Set filePath to the local file resource to download,
* relative to the application root ("/images/struts.gif").
*
*/
public class FileDownloadAction implements Action {
private String inputPath;
public void setInputPath(String value) {
inputPath = value;
}
public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String execute() throws Exception {
return SUCCESS;
}
}
HelloWorld.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<h3>Languages</h3>
<h1>File Download Example</h1>
<ul>
<li>
<s:url var="url" action="download"/><s:a href="%{url}">Download image file.</s:a>
The browser should display the Struts logo.
</li>
<li>
<s:url var="url" action="download2"/><s:a href="%{url}">Download ZIP file.</s:a>
The browser should prompt for a location to save the ZIP file.
</li>
</ul>
</body>
</html>