@RequestMapping(value = "getZip",method = RequestMethod.GET) @ResponseBody public CommonResponse getZip(HttpServletResponse response){ try { long a = System.currentTimeMillis(); response.setContentType("text/html;charset=UTF-8"); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment; filename=" + new String("压缩包测试.zip".getBytes("gb2312"), "ISO8859-1")); ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream()); StringBuilder builder = new StringBuilder(""); for (int i = 0; i < 1000000; i++) { if (i%3==0){ builder.append("\n"+"dsdsdsds"+i).append(","); } else { builder.append("dsdsdsdds"+i).append(","); } } byte[] bytes = builder.toString().getBytes(); InputStream excelIS = new ByteArrayInputStream(bytes); ZipEntry zipEntry = new ZipEntry("测试.csv"); zipOut.putNextEntry(zipEntry); byte b[] = new byte[2048]; int length = 0; while ((length = excelIS.read(b)) != -1) { zipOut.write(b, 0, length); } zipOut.closeEntry(); excelIS.close(); zipOut.close(); long bb = System.currentTimeMillis(); System.out.println("1000000 take time:"+(bb-a)); return new CommonResponse(0,"SUCCESS"); } catch (IOException e) { e.printStackTrace(); log.error("getZip exception:{}",e.getMessage()); return new CommonResponse(1,"FAIL"); } }
java csv的zip导出 代码示例
最新推荐文章于 2023-05-25 11:49:57 发布