docxdocx4j

Microsoft Word Docx Download Attachment Error


I have doc or docx document saved in Unix directory and integrate with web page which allow user to download the attachment. I have following code to stream the character and saves as Word document with correct MIME type but why when open it shows garbage character. It is relate to character encoding problem. How to solve this? Should I use docx4j?

String fullfilename = filename;

        File f = new File(fullfilename);
        int length = 0;

        ServletOutputStream op = response.getOutputStream();
        ServletContext context = getContext();
        String mimetype = context.getMimeType(fullfilename);

        response.setContentType((mimetype != null) ? mimetype
                : "application/x-download");
        response.setContentLength((int) f.length());
        response.setHeader("Content-Disposition", "attachment;filename="
                + filename);

        byte[] bbuf = new byte[fullfilename.length()];
        DataInputStream in = new DataInputStream(new FileInputStream(f));
        while ((in != null) && ((length = in.read(bbuf)) != -1)) {
            op.write(bbuf, 0, length);

        }

        in.close();
        op.flush();
        op.close();

Please help. Thanks.


Solution

  • Setting the correct mime type resolved the issue.