文件下载原理为数据传输,将服务器上的文件以流的方式发送到客户端,具体方法有多种,本处为下载指定目标文件模板,填写后上传导入数据,本文描述文件下载部分。
本项目下载的模板在WebRoot下,按需修改文件路径。
步骤:获取File路径;判断是否存在;设置响应头,用Stream进行输出。
本处下载文件会产生问题,当数据量很大时,下载文件使用Office无法打开,但使用WPS可以打开,此问题待解决。
1.jsp:按键button
<input type="button" value="下载模板" onclick="downloadTemplate()"
style="background-color: #004B97;color:white;font-family:SimSun;
font-size:10px;width: 10%;height: 5%;" />
onclick="downloadTemplate()"
style="background-color: #004B97;color:white;font-family:SimSun;
font-size:10px;width: 10%;height: 5%;" />
2. JavaScript 函数
//下载模板
function downloadTemplate() {
window.open(parent.formateUrl("downTem.xls(访问url)", "_self"));
$.messager.show({
title : '提示',
msg : "导出成功!",
showType : 'show',
timeout : 1000,
style : {
right : '',
top : document.body.scrollTop
+ document.documentElement.scrollTop,
bottom : ''
}
});
}
window.open(parent.formateUrl("downTem.xls(访问url)", "_self"));
$.messager.show({
title : '提示',
msg : "导出成功!",
showType : 'show',
timeout : 1000,
style : {
right : '',
top : document.body.scrollTop
+ document.documentElement.scrollTop,
bottom : ''
}
});
}
3.Conntroller
获取路径:request.getSession().getServletContext().getRealPath("")
/**
* 下载模板
*
*/
@RequestMapping("/downTem.xls")
public void downTem(HttpServletRequest request, HttpServletResponse res) {
try{
String path=request.getSession().getServletContext().getRealPath("");
String fileName="模板.xlsx";
String filePath=path+"\\"+fileName;
System.out.println(filePath);
FileUtil.downloadFile(filePath, fileName, "xlsx", res);
} catch (Exception e) {
e.printStackTrace();
}
}
request.getSession().getServletContext().getRealPath("");
String fileName="模板.xlsx";
String filePath=path+"\\"+fileName;
System.out.println(filePath);
FileUtil.downloadFile(filePath, fileName, "xlsx", res);
} catch (Exception e) {
e.printStackTrace();
}
}
4.public static void downloadFile(String filePath,String fileName,String fileType,HttpServletResponse response)
public static void downloadFile(String filePath,
String fileName,String fileType,HttpServletResponse response)
throws Exception{
File file=new File(filePath);
if("xlsx".equals(fileType)){
response.setContentType("application/vnd.ms-excel;charset=utf-8");
}
response.setHeader("content-disposition",
"attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
response.setContentLength((int)file.length());
byte[] buffer=new byte[1024];
BufferedInputStream in=null;
BufferedOutputStream out=null;
try {
in=new BufferedInputStream(new FileInputStream(file));
out=new BufferedOutputStream(response.getOutputStream());
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer,0,len);
}
out.flush();
response.flushBuffer();
} catch (Exception e) {
throw new Exception("downloadFile error.");
} finally{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
}
BufferedInputStream in=null;
BufferedOutputStream out=null;
try {
in=new BufferedInputStream(new FileInputStream(file));
out=new BufferedOutputStream(response.getOutputStream());
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer,0,len);
}
out.flush();
response.flushBuffer();
} catch (Exception e) {
throw new Exception("downloadFile error.");
} finally{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
}