您的位置:首页 > 其它

流形式下载文件(结合POI生成excel使用)

2015-12-23 16:17 585 查看
<span style="font-size:18px;">      String downFileName;
//配置本地生成路径
String savePath = "E:/";
if (exportType.equals("0")) {
downFileName = I18NUtil.getMessage(request, "employee.iodata.exportreport") + ".xls";
downFileName = URLEncoder.encode(downFileName, "UTF-8");
savePath += downFileName;
exportIOData(name, employeeId, serial, areaId, doorId, cardType, doorStatus, beginDate, endDate, savePath, request);
} else if (exportType.equals("1")) {
downFileName = I18NUtil.getMessage(request, "doorstatus.detail.report.note") + ".xls";
downFileName = URLEncoder.encode(downFileName, "UTF-8");
savePath += downFileName;
exportDoorStatus(doorId, areaId, doorStatus, beginDate, endDate, savePath, request);
} else {
downFileName = I18NUtil.getMessage(request, "report.urgentenvnt.type.note") + ".xls";
downFileName = URLEncoder.encode(downFileName, "UTF-8");
savePath += downFileName;
byte eventType = 2;
exportUrgentEvent(eventType, urgentType, beginDate, endDate, savePath, request);
}
//File file = new File(savePath);
// 设置response的Header
/* response.addHeader("Content-Disposition", "attachment;filename=" + new String(downFileName.getBytes("utf-8"), "iso-8859-1"));  //转码之后下载的文件不会出现中文乱码
response.addHeader("Content-Length", "" + file.length());                                                                   //需将后缀名一同转码,否则会造成下载无扩展名
response.reset();*/
// 清空response
response.reset();
response.setContentType("application/msexcel");//设置生成的文件类型
response.setCharacterEncoding("UTF-8");//设置文件头编码方式和文件名
response.setHeader("Content-Disposition", "attachment; filename="+ new String(downFileName.getBytes("utf-8"), "ISO8859-1"));
try {
//以流的形式下载文件
//savePath = new String(savePath.toString().getBytes("utf-8"));
// savePath = URLEncoder.encode(savePath, "UTF-8");
InputStream fis = new BufferedInputStream(new FileInputStream(savePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();

OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: