0

我有一个 xml 文档的输出流。我将所有 PDF 文件附加到一个 zip 文件中。完成所有 PDF 文件的附加后,我想附加 xml 文档。但我得到的只是空的 xml 文件。

这就是我正在做的

byte b[] = new byte[10240];
ZipOutputStream zout = new ZipOutputStream(getOutputStream("ExampleForms.zip", attach));
for(int i = 0; i < pdfs.length; i++){
    File f = File.createTempFile(pdfs[i].NAME, ".pdf");
    FileOutputStream fo = new FileOutputStream(f);
    pdfs[i].render(fo);
    fo.close();
    InputStream in = new FileInputStream(f);
    ZipEntry e = new ZipEntry(pdfs[i].NAME + ".pdf");
    zout.putNextEntry(e);
    int len=0;
    while((len=in.read(b)) != -1) {
          zout.write(b,0,len);
    }
    in.close();
    zout.closeEntry();
    f.delete();
}       
/* out is my outputstream in which i have written xml document */
File f = File.createTempFile("SampleXmlFile.xml");
FileOutputStream fo = new FileOutputStream(f);
fo.write(out.toString().getBytes());
fo.close();
InputStream is = new FileInputStream(f);
ZipEntry e = new ZipEntry("MyXmlFile",".xml");
zout.putNextEntry(e);
int lent = 0;
while((lent = is.read(bt)) != -1){
   zout.write(bt,0,lent);
}
is.close();
zout.closeEntry();
zout.close();
4

1 回答 1

1

1)很可能out是空的

2) 为什么要使用临时文件?采用

 zout.write(out.toString().getBytes());
于 2013-04-09T04:15:38.730 回答