front end
show(e) { this.axios({ url: this.url.labelPrint, method: 'post', responseType: 'blob', type: 'multipart/form-data', timeout: 150000, data: { //Encapsulate the parameters to be used sampleNumber: e.reportNumber, sampleName: e.sampleName, sampingDate: e.sampingDate, deliveryDate: e.deliveryDate, deliveryDates: e.deliveryDates, deliveryAddress: e.deliveryAddress, standbyWhere: e.standbyWhere, sampleQuantity: e.sampleQuantity, key: e.id } }).then(res => { const elink = document.createElement('a') elink.style.display = 'none' document.body.appendChild(elink); //The name of the duplicate name const fileName = e.reportNumber + e.sampleName+'.zip' elink.download = fileName let binaryData = []; binaryData.push(res); elink.href = window.URL.createObjectURL(new Blob(binaryData)); elink.click(); URL.revokeObjectURL(elink.href); // Release the URL object this.$message.success("Download succeeded"); document.body.removeChild(elink); }) },
java background
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
// Generate multiple files
for (int a = shu; a > 0; a--) { // Create each label docx file String fileName = sampleNumber + '-' + a + sampleName + "label.docx"; String filePath = csbcbhUrl.getUrlFile() + fileName; contractTestReportController.parse(templatePath, templateName, filePath, map); // Add the label file to the Zip file byte[] buffer = new byte[1024]; FileInputStream inputStream = new FileInputStream( filePath); zipOutputStream.putNextEntry(new ZipEntry(fileName)); int len; while ((len = inputStream.read(buffer)) != -1) { zipOutputStream.write(buffer, 0, len); } inputStream.close ();
zipOutputStream.closeEntry();
// Record the file name of the Zip file
objects.add(filePath);
}
// Set the response type and header information of the Zip file
httpServletResponse.setContentType("application/zip");
httpServletResponse.setHeader("Content -Disposition", "attachment; filename=\"sample_labels.zip\"");
}
// Download all the bytes of the Zip file to the client
ServletOutputStream out = httpServletResponse.getOutputStream();
outputStream.writeTo(out);
out .flush();
out.close();
outputStream.close();
// Delete temporarily generated files
for (String filePath : objects) { File file = new File(filePath); file.delete(); }