下載模板文件
window.location.href = encodeURI(basePath + "/util/downtemplate.jsp?filename="+filename);
<%@page pageEncoding="UTF-8"%>
<%@page import="javax.servlet.ServletOutputStream"%>
<%@page import="java.io.*"%>
<%@page import="com.eos.web.taglib.util.*" %>
<%@ page import="java.net.URLEncoder" %>
<%
InputStream bis = null;
OutputStream bos = null;
response.setCharacterEncoding("utf-8");
String fileName = request.getParameter("filename");
String filePath = "templatefile" + File.separator+fileName;
System.out.println(filePath+"===");
// 解決中文亂碼
fileName = URLEncoder.encode(fileName, "utf-8");
//System.out.println(fileName);
try {
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setStatus(HttpServletResponse.SC_OK);
bos = response.getOutputStream();
byte[] buff = new byte[1024];
int bytesRead;
ServletContext sc = request.getSession().getServletContext();
String serverRealPath = sc.getRealPath("/");
serverRealPath = serverRealPath + filePath;
// 路徑分隔符轉換
serverRealPath = serverRealPath.replace("//", File.separator);
System.out.println("========serverRealPath=======" + serverRealPath);
bis = new FileInputStream(new File(serverRealPath));
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
out.clear();
out = pageContext.pushBody();
} catch (IOException e) {
System.out.println("==========下載異常=========" + e.getMessage());
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
%>
模板文件導入校驗
var excelFile = $("#file").val();
var excelFile = $("#empItemFile").val();
if (excelFile=="") {
nui.alert('請選擇您要導入的Excel文件!');//請選擇您要導入的Excel文件!
return;
}
var re= /.xls$/;
if (!re.test(excelFile))
{
nui.alert('請選擇Excel文件!'); //請選擇Excel文件!
return;
}
excelFile = excelFile.substr(excelFile.lastIndexOf("\\") + 1);//校驗是否是導出的模板文件