try { String fileName = courseWare.getTitle(); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); //设置字符集和文件后缀名 response.setContentType("application/octet-stream"); //兼容问题 String name = new String(fileName.getBytes("UTF-8"), "ISO-8859-1"); if (request.getHeader("User-Agent").toUpperCase().indexOf("TRIDENT") > -1 || request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > -1) { name = URLEncoder.encode(fileName, "UTF-8"); } // 设置文件名称 response.setHeader("Content-Disposition", "attachment; filename=" + name + ".zip"); OutputStream out = response.getOutputStream(); // 声明压缩流对象 ZipOutputStream zipOut = new ZipOutputStream(out) ; // 定义文件输入流 int temp = 0; File file; byte[] buffer = new byte[1024 * 8]; Field[] fields = courseWare.getExtendVo().getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String pkAttached = field.get(courseWare.getExtendVo()).toString(); if(pkAttached != null && !"".equals(pkAttached)){ Attached attached = attachedService.query(pkAttached); if(attached != null && attached.getFilePath() != null && !"".equals(attached.getFilePath())){ String docFileName = attached.getFilePath(); docFileName = docFileName.replace(ip,uploadurl); file = new File(docFileName); InputStream input = new FileInputStream(file); zipOut.putNextEntry(new ZipEntry(attached.getFileName())); while((temp = input.read(buffer)) > 0) { zipOut.write(buffer, 0, temp); } input.close(); } } } String pkAttached = courseWare.getUrl(); if(pkAttached != null && !"".equals(pkAttached)){ Attached attached = attachedService.query(pkAttached); if(attached != null && attached.getFilePath() != null && !"".equals(attached.getFilePath())){ String docFileName = attached.getFilePath(); docFileName = docFileName.replace(ip,uploadurl); file = new File(docFileName); InputStream input = new FileInputStream(file); zipOut.putNextEntry(new ZipEntry(attached.getFileName())); while((temp = input.read(buffer)) > 0) { zipOut.write(buffer, 0, temp); } input.close(); } } zipOut.flush(); zipOut.close(); } catch (Exception e) { e.printStackTrace(); }