Microsoft Word Docx 下载附件错误

问题描述 投票:0回答:1

我有 doc 或 docx 文档保存在 Unix 目录中,并与网页集成,允许用户下载附件。我有以下代码来流式传输字符并保存为具有正确 MIME 类型的 Word 文档,但为什么打开时显示垃圾字符。这与字符编码问题有关。怎么解决这个问题呢?我应该使用 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();

请帮忙。谢谢。

docx docx4j
1个回答
0
投票

设置正确的 MIME 类型解决了该问题。

© www.soinside.com 2019 - 2024. All rights reserved.