Struts2文件下载

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>

注意:文件位置以自己电脑为准

具体源码: 点击







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值