美文网首页
Java下载Excel文件

Java下载Excel文件

作者: Akademos | 来源:发表于2016-12-12 16:24 被阅读0次
    Paste_Image.png
        public String downTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
            File f = ResourceUtils.getFile("classpath:template/导出成绩.xls");
            try {
                response.setCharacterEncoding("UTF-8");
                String fileName = new String("导出成绩.xls".getBytes("UTF-8"),
                        "ISO8859-1");
                fileName = fileName.replaceAll("\\+", "%20");
                response.setHeader("Cache-control", "private");
                response.setHeader("Cache-Control", "maxage=3600");
                response.setHeader("Pragma", "public");
                response.setContentType("application/vnd.ms-excel");
                response.setContentType("application/octet-stream;charset=UTF-8;");
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            ServletOutputStream out = response.getOutputStream();
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(new FileInputStream(f));
                bos = new BufferedOutputStream(out);
                byte[] buff = new byte[2048];
                int bytesRead;
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                    bos.write(buff, 0, bytesRead);
                }
            } catch (final IOException e) {
                throw e;
            } finally {
                if (bis != null)
                    bis.close();
                if (bos != null)
                    bos.close();
            }
            return null;
        }
    

    相关文章

      网友评论

          本文标题:Java下载Excel文件

          本文链接:https://www.haomeiwen.com/subject/pdwmmttx.html