内容简介: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>
就此,我们就实现了文件下载的功能。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 前端培训-初级阶段-场景实战(2019-06-06)-下载文件&下载进度
- Android原生下载(下篇)多文件下载+多线程下载
- Netty接收HTTP文件上传及文件下载
- Struts2 文件下载
- 前端文件下载通识篇
- jsp文件下载功能实现代码
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
PHP和MySQL Web开发(原书第4版)
Luke Welling、Laura Thomson / 武欣 / 机械工业出版社 / 2009 / 95.00元
本书将PHP开发与MySQL应用相结合,分别对PHP和MySQL做了深入浅出的分析,不仅介绍PHP和MySQL的一般概念,而且对PHP和MySQL的Web应用做了较全面的阐述,并包括几个经典且实用的例子。. 本书是第4版,经过了全面的更新、重写和扩展,包括PHP 5.3最新改进的特性(例如,更好的错误和异常处理),MySQL的存储过程和存储引擎,Ajax技术与Web 2.0以及Web应用需要......一起来看看 《PHP和MySQL Web开发(原书第4版)》 这本书的介绍吧!