struts实战--文件下载

栏目: Struts · 发布时间: 7年前

内容简介:struts实战--文件下载

struts实现文件下载

一、页面

这个页面的里面的简历资料就是我们的下载入口。

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<HTML>
	<HEAD>
		<meta http-equiv="Content-Language" content="zh-cn">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<LINK href="${pageContext.request.contextPath}/css/Style.css" type="text/css" rel="stylesheet">
		<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
	</HEAD>
	<body>
		<form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/user/userAction_save.do" method="post" enctype="multipart/form-data">
			 
			<table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
				<tr>
					<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"
						height="26">
						<strong><STRONG>查看用户</STRONG>
						</strong>
					</td>
				</tr>

				<tr>
					<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
						登录名: <s:debug />
					</td>
					<td class="ta_01" bgColor="#ffffff">
						<s:property value="model.logonName"/>
					</td>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						用户姓名:
					</td>
					<td class="ta_01" bgColor="#ffffff">
						<s:property value="model.userName"/>
					</td>
				</tr>
				
				<tr>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						性别:
					</td>
					<td class="ta_01" bgColor="#ffffff">
						<s:property value="model.sex"/>
					</td>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						学历:
					</td>
					<td class="ta_01" bgColor="#ffffff">
						<s:property value="model.education"/>
					</td>
				</tr>
				<tr>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						出生日期:
					</td>
					<td class="ta_01" bgColor="#ffffff">
						<s:property value="model.birthday"/>
					</td>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						电话:
					</td>
					<td class="ta_01" bgColor="#ffffff">
						<s:property value="model.telephone"/>
					</td>
				</tr>
				<tr>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						兴趣爱好:
					</td>
					<td class="ta_01" bgColor="#ffffff" colSpan="3">
						<s:property value="model.interest"/>
					</td>
				</tr>
				<tr>
					<td align="center" bgColor="#f5fafe" class="ta_01">
						简历资料:
					</td>
					<td class="ta_01" bgColor="#ffffff" colSpan="3">
						<s:a action="user_download" namespace="/" cssClass="cl_01">
							<s:param name="userID" value="model.userID"></s:param>
							<s:property value="model.filename"/>
						</s:a>
					</td>
				</tr>
				<TR>
					<TD class="ta_01" align="center" bgColor="#f5fafe">
						备注:
					</TD>
					<TD class="ta_01" bgColor="#ffffff" colSpan="3">
						<s:property value="model.remark"/>
					</TD>
				</TR>
				<TR>
					<td align="center" colSpan="4" class="sep1">
						<img src="${pageContext.request.contextPath}/images/shim.gif">
					</td>
				</TR>
				<TR>
					<td class="ta_01" style="WIDTH: 100%" align="right" bgColor="#f5fafe" colSpan="4">
						<FONT face="宋体">       </FONT>
						<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/>
						<span id="Label1"></span>
					</td>
				</TR>
			</table>
		</form>
	</body>
</HTML>

二、action类

下载的时候,我们需要下载的文件输入流,MIME类型和文件名。

1、根据用户id查询到用户。

2、根据用户获取到文件输入流。

3、根据用户获取文件名。

4、在下载的时候可能会出现名称乱码,所以我们需要用encodeDownloadFilename方法解决。

/**
	 * 下载简历 (struts2 下载 一个流、 两个头信息)
	 * 
	 * @return
	 */
	public String download() {
		UserService userService = new UserService();
		user = userService.findById(user.getUserID());

		return "downloadSUCCESS";
	}

	// 返回文件流
	public InputStream getInputStream() throws IOException {
		if (user == null || user.getPath() == null) {
			return null;
		}
		File file = new File(ServletActionContext.getServletContext()
				.getRealPath(user.getPath()));
		return new FileInputStream(file);
	}

	// 返回简历 MIME类型
	public String getContentType() {
		if (user == null || user.getFilename() == null) {
			return null;
		}
		return ServletActionContext.getServletContext().getMimeType(
				user.getFilename());
	}

	// 返回编码后的文件名
	public String getDownloadFilename() throws IOException {
		if (user == null || user.getFilename() == null) {
			return null;
		}
		return encodeDownloadFilename(user.getFilename(), ServletActionContext
				.getRequest().getHeader("user-agent"));
	}

	/**
	 * 下载文件时,针对不同浏览器,进行附件名的编码
	 * 
	 * @param filename
	 *            下载文件名
	 * @param agent
	 *            客户端浏览器
	 * @return 编码后的下载附件名
	 * @throws IOException
	 */
	public String encodeDownloadFilename(String filename, String agent)
			throws IOException {
		if (agent.contains("Firefox")) { // 火狐浏览器
			filename = "=?UTF-8?B?"
					+ new BASE64Encoder().encode(filename.getBytes("utf-8"))
					+ "?=";
		} else { // IE及其他浏览器
			filename = URLEncoder.encode(filename, "utf-8");
		}
		return filename;
	}

三、struts.xml文件配置

<result name="downloadSUCCESS" type="stream">
				<!-- 下载流 getInputStream -->
				<param name="contentType">${contentType}</param>
				<param name="contentDisposition">attachment;filename=${downloadFilename}</param>
			</result>
就此,我们就实现了文件下载的功能。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Creative Selection

Creative Selection

Ken Kocienda / St. Martin's Press / 2018-9-4 / USD 28.99

Hundreds of millions of people use Apple products every day; several thousand work on Apple's campus in Cupertino, California; but only a handful sit at the drawing board. Creative Selection recounts ......一起来看看 《Creative Selection》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具