0

The ending of poi code:

response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("დოკუმენტი-" + loan.getLoanId() + ".zip", "UTF-8"));

File f = new File("autoloanagreement.docx");
FileOutputStream fileOutputStream = new FileOutputStream(f);

document.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Success");

I have set the controller handler method on produces = "application/zip" and whenever I download the file and try to open it, I get

The archive is either in unknown format or damaged

EDIT :

I've added new lines of code and now the zip file gets downloaded and opened but I get the new error message of "Unexpected end of zip file"

ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream(), Charset.forName("UTF-8"));

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

        DocumentSettingsPart dsp = wordMLPackage.getMainDocumentPart().getDocumentSettingsPart();
        org.docx4j.wml.CTSettings settings = Context.getWmlObjectFactory().createCTSettings();
        BooleanDefaultTrue val = new BooleanDefaultTrue();
        val.setVal(true);

        // Layout Settings
        org.docx4j.wml.CTCompat compat = Context.getWmlObjectFactory().createCTCompat();
        compat.setDoNotExpandShiftReturn(val);
        settings.setCompat(compat);
        dsp.setJaxbElement(settings);
        wordMLPackage.getMainDocumentPart().addTargetPart(dsp);

        File f = new File("AutoLoanAgreement");
        zipOutputStream.putNextEntry(new ZipEntry(f.getName()));
        wordMLPackage.save(f);
        FileInputStream fileInputStream = new FileInputStream(f);
        IOUtils.copy(fileInputStream, zipOutputStream);


        // დავხუროთ სტრიმი
        fileInputStream.close();
        zipOutputStream.closeEntry();
Community
  • 1
  • 1

1 Answers1

1

You send the file as a zip, but never seem to zip the file you are sending. You can wrap the FileOutputStream in ZipOutputStream, e.g.

OutputStream out = new ZipOutputStream(new FileOutputStream(f));

Also you never seem to actually pass the file to the OutputStream of the response.

try:

OutputStream os = new ZipOutputStream(response.getOutputStream());
document.write(os);
Blokje5
  • 4,763
  • 1
  • 20
  • 37