zipOutputStream匯出zip壓縮包
阿新 • • 發佈:2018-12-19
def basePath = request.getSession().getServletContext().getRealPath(".") def outFile = basePath+"/export/8D.xls" InputStream input =new BufferedInputStream(new FileInputStream(outFile)) def results = get8DData(record.problemSubId) response.reset(); response.setContentType('APPLICATION/OCTET-STREAM') response.setHeader('Content-Disposition', 'Attachment;Filename="problemReport.zip"') ZipOutputStream zip = new ZipOutputStream(response.outputStream); def file1Entry = new ZipEntry('8D.xls'); zip.putNextEntry(file1Entry); def workbook = new XLSTransformer().transformXLS(input, results.data) //將EXCEL寫入到zip輸出流中 workbook.write(zip)results.files.each{it -> // InputStream fileBytes = new FileInputStream("D:/logo1.png") def fileBytes = new ByteArrayInputStream(it.image) def files= new ZipEntry(it.image_filename); // def file1Entry = new ZipEntry("logo1.png"); zip.putNextEntry(files); def buf = new byte[1024 * 1024] int len = 0 // 迴圈將輸入流中的內容讀取到緩衝區中 while ((len = fileBytes.read(buf)) > 0) { // 輸出緩衝區內容到瀏覽器,實現檔案下載 // response.outputStream.write(buf, 0, len) zip.write(buf, 0, len) } fileBytes.close() } input.close() zip.close();