我试图生成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
同意,更改您的标题和标签以吸引更多的观众,他们可能能够帮助。 –