Struts2的文件上传和下载(二)

二、文件下载

要在Struts2中实现文件下载,需要在struts.xml配置文件中先配置用于下载的拦截器download,然后配置

<result name="success" type="stream">

中stream的参数值,stream的常用参数如下所示:
(1)、contentType:用于指定下载文件的类型,该文件类型应与互联网MIME标准中的规定一致,如text/xml表示XML类型的文件,text/gif表示gif图片,text/plain表示纯文本类型。
(2)、inputName:用于指定下载文件的输入流入口,在Action中需要指定该输入流入口。
(3)、contentDisposition:用于指定文件下载的处理方法,有内嵌(Inline)和附件(Attachment)两种方法,内嵌方式表示浏览器会尝试直接显示文件,附件方式会弹出文件保存对话框,默认值为内嵌。
(4)、bufferSize:用于设置下载文件时的缓存大小。

下面通过一个项目来理解一下在Struts2框架中是怎样实现文件下载功能的。

项目文件框架
在这里插入图片描述
fileDown.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
	<head>
		<title>文件下载</title>
	</head>
	<body>
		文件下载
		<hr>
		<a href="fileDownload.action">单击此处下载头像</a>
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaeee/web-app_3_1.xsd" 
	id="WebApp_ID" 
	version="3.1" >
		<filter>
		<!-- 配置Struts2核心控制器的名字 -->
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	
	<filter-mapping>
		<!-- Struts2控制器的名字 -->
		<filter-name>struts2</filter-name>
		<!-- 拦截所有的URL请求-->
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<welcome-file-list>
		<welcome-file>fileDown.jsp</welcome-file>
	</welcome-file-list>
	
	
</web-app>

FileDownload.java

package fileDownload;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileDownload extends ActionSupport{
	//指定文件的下载路径
	private String path;

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	//该方法返回一个InputStream类型的输入流,是下载目标文件的入口
	public InputStream getInputStream() throws Exception{
		return ServletActionContext.getServletContext().getResourceAsStream(path);
	}
	public String execute() throws Exception{
		return SUCCESS;
	}

}

struts.xml

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="upload" extends="struts-default">
		<action name="upLoad" class="fileUpDown.UploadAction">
			<!-- fileUpload拦截器设置 -->
			<interceptor-ref name="fileUpload">
				<!-- 设置上传文件的最大字节数 -->
				<param name="maximumSize">10000000</param>
				<!-- 设置上传文件的类型 -->
				<param name="allowedTypes">
					image/gif,image/png,image/jpeg,image/jpg,image/pjpeg
				</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<!-- 设置上传文件保存的文件夹 -->
			<param name="savePath">/save</param>
			<result name="input">/fileUpload/fileUp.jsp</result>
			<result name="success">/fileUpload/fileUpSuccess.jsp</result>
		</action>
		
		<action name="fileDownload" class="fileDownload.FileDownload">
			<!-- 设置文件所在的位置,需要在项目中加一个名为download的文件夹 -->
			<param name="path">/download/头像.jpg</param>
			<!-- 设置stream属性 -->
			<result name="success" type="stream">
				<!-- 设置stream对应的参数 -->
				<param name="contentType">image/jpg</param>
				<param name="inputName">inputStream</param>
				<!-- 设置为附件方式,且默认的下载名字为hlm.jpg -->
				<param name="contentDisposition">attachment;filename="hlm.jpg"</param>
				<param name="bufferSize">40960</param>
			</result>
		</action>
	</package>
</struts>

运行结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值