/**
* 描述:下载模板
* @author myw
* @date 2015-11-15
* @param filePath
* @param response
* @param isOnLine 是否在线打开
* @throws Exception
*/
private String downLoadTemplate(String filePath, HttpServletResponse response, String dateStr,
boolean isOnLine) throws Exception {
String dateStr0 = dateStr.replace("-", "");
File file = new File(filePath);
String[] urlSplit = file.getName().split("\\.");
String filename = urlSplit[0]+dateStr0+"."+urlSplit[1];
if (!file.exists()) {
response.sendError(404, "File not found!");
return null;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
response.setCharacterEncoding("utf-8");
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename,"utf-8"));
} else { //下载方式
response.setContentType("application/msexcel;charset=utf-8");
//response.setContentType("application/x-msdownload");
//response.setContentType(ServletActionContext.getServletContext().getMimeType(f.getName()));
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename,"utf-8"));
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
return filename;
}
* 描述:下载模板
* @author myw
* @date 2015-11-15
* @param filePath
* @param response
* @param isOnLine 是否在线打开
* @throws Exception
*/
private String downLoadTemplate(String filePath, HttpServletResponse response, String dateStr,
boolean isOnLine) throws Exception {
String dateStr0 = dateStr.replace("-", "");
File file = new File(filePath);
String[] urlSplit = file.getName().split("\\.");
String filename = urlSplit[0]+dateStr0+"."+urlSplit[1];
if (!file.exists()) {
response.sendError(404, "File not found!");
return null;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
response.setCharacterEncoding("utf-8");
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename,"utf-8"));
} else { //下载方式
response.setContentType("application/msexcel;charset=utf-8");
//response.setContentType("application/x-msdownload");
//response.setContentType(ServletActionContext.getServletContext().getMimeType(f.getName()));
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename,"utf-8"));
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
return filename;
}