我試圖生成PDF 2個數據,並把它放在一個ZIP文件中下載(通過response.getOutputStream),但我不知道如何做正確:如何將2 ByteArrayOutputStreams放入一個zip文件進行下載?
public void export() {
String fileName = "B2B_Price_List.zip";
String fileNameUSD = "B2B_Price_List_USD.pdf";
String fileNameEU = "B2B_Price_List_EU.pdf";
String contentTypePDF = "application/pdf";
String[] headerPDF = new String[2];
headerPDF[0] = "Content-disposition";
headerPDF[1] = "attachment; filename=\"" + fileNameUSD + "\"";
headerPDF[2] = "attachment; filename=\"" + fileNameEU + "\"";
String contentTypeZIP = "application/zip";
String[] headerZIP = new String[1];
headerZIP[0] = "Content-disposition";
headerZIP[1] = "attachment; filename=\"" + fileName + "\"";
ByteArrayOutputStream outUSD = new ByteArrayOutputStream();
outUSD = CSVHandler.downloadPriceListPDF(outUSD, fileNameUSD, ListToPDFMap(productsUSD), true);
ByteArrayOutputStream outEU = new ByteArrayOutputStream();
outEU = CSVHandler.downloadPriceListPDF(outEU, fileNameEU, ListToPDFMap(productsEU), false);
// ZIP CODING GOES HERE
}
該函數返回ByteArrayOutputStream到在以後使用:
public static ByteArrayOutputStream downloadPriceListPDF
(ByteArrayOutputStream output, final String filename,
Map<String, Map<String, List<B2BProductData>>> datas,
boolean amerCustomer) {
try {
PdfDocument pdfDoc = null;
try {
pdfDoc = new PdfDocument(new PdfWriter(output));
PageSize pageSize = new PageSize(PageSize.A4);
Document doc = new Document(pdfDoc, pageSize, false);
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
String coverImage = COVER_IMAGE;
if(!amerCustomer) {
coverImage = COVER_IMAGE_1;
}
canvas.addImage(ImageDataFactory.create(CSVHandler.class.getClassLoader().getResource(coverImage).getPath()), pageSize, false);
// loop thru category
int pageNo = 2;
Map<String, List<B2BProductData>> inputDatas = new LinkedHashMap<>();
for(String category : datas.keySet()) {
Map<String, List<B2BProductData>> prods = datas.get(category);
while(true) {
inputDatas = new LinkedHashMap<>();
Map<String, List<B2BProductData>> remaindatas = filterDatas(inputDatas, prods);
if(inputDatas.size() > 0) {
createPDFPage(pdfDoc, doc, category, inputDatas, pageNo ++, amerCustomer);
}
if(remaindatas.size() > 0) {
prods = remaindatas;
} else {
break;
}
}
}
doc.close();
return output;
} catch (IOException e) {
LOG.error(e.getMessage());
return output;
}
}
catch (final Exception ex) {
LOG.error("Export Products got error: " + ex.getMessage());
return output;
}
}
您可能還需要修改你的問題,刪除您iText的標籤和iText的代碼,因爲你的問題僅是關於拉鍊,它並不真正的問題你通過什麼它。 – sorifiend
[使用Java ByteArrayOutputstream壓縮文件]的可能重複(https://stackoverflow.com/questions/26482779/compress-file-with-java-bytearrayoutputstream) – sorifiend
同意,更改您的標題和標籤以吸引更多的觀衆,他們可能能夠幫助。 –