Struts2文件下载
1、Struts2提供了stream结果类型,该结果类型就是专门用于支持文件下载的。指定stream类型时,需要指定一个inputName参数,指定了一个文件输入流,是文件下载的入口。
2、struts2的文件下载与普通的Action并没有太大的不同,仅仅是该Action需要提供一个返回
InputStream的方法,该输入流代表了被下载文件的入口。
3、Action中包含一个getTargetFile()的方法,
该方法返回一个InputStream,这个输入流返回的是下载目标文件的入口。该方法的方法名为getTargetFile,则stream类型的结果映射中inputName参数值为targetFile。
4、配置Action需要配置一个类型为stream的结果,该stream类型结果将使用文件下载作为响应。需要配置stream类型指定的4个属性。
inputName:指定被下载文件的入口输入流。
contentType:指定下载文件的文件类型。
contentDisposition:指定下载的文件名。
bufferSize:指定下载文件时的缓存大小。
写代码前,先说一下大体流程:
login.jsp
<body>
<font color="red">${request.error} </font>
<form action="loginAction" method="post">
用户名:<input type="text" name="username">
密 码:<input type="password" name="password">
<input type="submit" name="提交">
</form></body>
loginAction.java
package com.zmy.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String username;
private String password;
private String usertype;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
public String loginMethod(){
if(username.equals("zmy")&&password.equals("123"))
{
ActionContext.getContext().getSession().put("username", username);
}
else{
this.addFieldError("error", "û벻ƥ");
return "input";
}
return "loginOK";
}
}
验证用户名和密码,如果正确就把用户名写到Session中,跳到下一个Action,否则跳回登陆页。
Struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="LoginIntercepter" class="com.zmy.inteecepter.LoginIntercepter" >
</interceptor>
</interceptors>
<action name="download_list" class="com.zmy.action.DowenLoadAction" method="getDowenLoadList" >
<interceptor-ref name="LoginIntercepter"></interceptor-ref>
<result name="listOK">/ShowDownLoadList.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="download_getFile" class="com.zmy.action.DowenLoadAction" method="dowenLoadMethod">
<result name="downLoadOK" type="stream">
<param name="contentType">${contentType}</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="contentLength">${contentLength}</param>
<param name="inputName">tagetFile</param>
<param name="bufferSize">4096</param>
</result>
</action>
<action name="loginAction" class="com.zmy.action.LoginAction" method="loginMethod">
<result name="loginOK" type="chain">download_list</result>
<result name="iunput">/login.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
DowenLoadAction.java
package com.zmy.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zmy.bean.FileItem;
import com.zmy.dao.FileDao;
public class DowenLoadAction extends ActionSupport {
private FileDao fileDao=new FileDao();
private String contentType;
private String fileName;
private Long contentLength;
private InputStream targetFile;
private String fileId;
public String getContentType() {
return contentType;
}
public String getFileName() {
return fileName;
}
public Long getContentLength() {
return contentLength;
}
public InputStream getTargetFile() {
return targetFile;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public String getDowenLoadList(){
List<FileItem> fileList=fileDao.getDownLoadFileList();
ActionContext.getContext().put("fileList", fileList);
return "listOK";
}
public String downloadMethod() throws FileNotFoundException{
FileItem fileItem=fileDao.getFileById(fileId);
contentType=fileItem.getContentType();
fileName=fileItem.getFileName();
contentLength=fileItem.getContentLength();
targetFile=new FileInputStream(fileItem.getLocationPath());
return "downLoadOK";
}
}
获取到要下载的列表
ShowDownLoadList.jsp
<h3>下载列表</h3>
<s:iterator value="#request.fileList">
<a href="download_getFile?fileId=${fileId}"><s:property value="fileName"/></a><br>
</s:iterator>
DownLoad.jsp
<body>
<a href="C:/">下载固定非图片文件</a>
<a href="da.jpg">下载固定图片文件</a>
<a href="WEB-INF/da.jpg">下载webInf下内容</a>
<a href="download_list">下载文件列表</a>
</body>
注意:文件位置以自己电脑为准
具体源码:
点击