java代码
/**
* 下载模板方法
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward doDownload(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//文件路径
String filePath = (String) request.getParameter("filePath");
//文件名称
String fileName = (String) request.getParameter("fileName");
//获取文件后缀
String fileSuffix = filePath.substring(filePath.lastIndexOf("."));
//获取文件路径
String realFilePath = this.getServlet().getServletContext().getRealPath(filePath);
String filedisplay = "终端厂商信息模板";//给用户提供的下载文件名
try {
filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
} catch (UnsupportedEncodingException e1) {
filedisplay = "uploadTemplate";
}
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=/"" + filedisplay + fileSuffix + "/"");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
FileInputStream inStream = null;
try {
inStream = new FileInputStream(realFilePath);
OutputStream out = response.getOutputStream();
byte[] buff = new byte[2048];
while (true) {
int read = inStream.read(buff, 0, 2048);
if (read <= 0)
break;
out.write(buff, 0, read);
}
out.flush();
out.close();
inStream.close();
} catch (IOException e) {
SysLog.writeLogs("dmreg", GlobalParameters.ERROR,"VendorManagementAction--doDownload--1:" + e.getMessage());
throw new ActionException(e);
} finally {
if(inStream != null) {
try {
inStream.close();
} catch (IOException e) {
SysLog.writeLogs("dmreg", GlobalParameters.ERROR,"VendorManagementAction--doDownload--2:" + e.getMessage());
throw new ActionException(e);
}
}
}
return null;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
js代码
function doDownload(){
document.myform.action="vendorManagementAction.do?method=doDownload&filePath=/views/dmreg/dm/devicemanager/dmcompany/file/Template.xls&fileName=uploadTemplateUnicom";
document.myform.submit();
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
jsp代码
<form name="myform" method="post" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0" class="formTable">
<!-- 表格标题行-->
<tr class="tableTitleTR2">
<td colspan="6">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="tableTitleLeft2">
 
</td>
<td class="tableTitle2">
新增终端厂商信息
</td>
<td class="tableTitleRight2">
 
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="formTableL">
 
</td>
<td class="formTableC">
<!-- BEGIN 表单核心区域 -->
<table align="center" border="0" cellpadding="0" cellspacing="3"
class="formTableCore">
<tr>
<td class="formLabel" style="width: 20%">
文件导入
<font color="red">*</font>
</td>
<td class="formField">
<input type="file" style="width: 450px" name="codeFile"
id="codeFile" size="50" οnkeydοwn="okd(this)"
style="ime-mode:disabled" />
</td>
<td class="formField">
<input type="button" id="closeBtn" name="closeBtn"
class="formButton" value="下载模板" οnclick="doDownload();"/>
<input type="button" id="closeBtn" name="closeBtn"
class="formButton" value="导入厂商信息" οnclick="doImport();" />
</td>
</tr>
</table>
<!-- END 表单核心区域 -->
</td>
<td class="formTableR">
 
</td>
</tr>
<tr>
<td class="formTableLB">
 
</td>
<td class="formTableB">
 
</td>
<td class="formTableRB">
 
</td>
</tr>
</table>
<!-- BEGIN 这里放入按钮 两种按钮定义方式 建议采用前者 button 更符合w3c规范 且功能更强大 支持html代码 -->
<div id="bottom" align="center">
</div>
<!-- END 这里放入按钮 -->
<div id="bottom" align="center" style="display: block">
<iframe frameborder="0" scrolling="auto" marginwidth="0"
marginheight="0" style="height: 50%; width: 100%;" name="grid"
id="grid">
</iframe>
</div>
</form>