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

来源:互联网 发布:java转义代码 编辑:程序博客网 时间:2024/05/19 19:56
<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>

0 0
原创粉丝点击