注:对于本身就是非压缩的excel,不需要先把文件压缩然后再读取zip并下载,谁有更好的方法可以留言。 public void download(String id, HttpServletRequest request, HttpServletResponse response) { try { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e1) { } String filePath = "D:/upload/test.xls"; String excel_filename= "test"; InputStream myxls = null; try { myxls = new FileInputStream(filePath ); } catch (FileNotFoundException e1) { e1.printStackTrace(); } HSSFWorkbook wb = null; try { wb = new HSSFWorkbook(myxls); } catch (IOException e1) { e1.printStackTrace(); } // 将excel文件压缩到zip文件并输出 response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment;filename=" + excel_filename + ".zip"); try { OutputStream out = response.getOutputStream(); ZipOutputStream zos = new ZipOutputStream(out); ZipEntry ze = new ZipEntry(excel_filename+".xls"); zos.putNextEntry(ze); wb.write(zos); wb.close(); zos.closeEntry(); zos.finish(); zos.close(); out.flush(); out.close(); } catch (IOException e) { } } |
|
来自: 书屋随身带 > 《对文件的操作-上传/下载...》