Microsoft Word Docx下载附件错误

时间:2013-01-10 07:24:07

标签: docx docx4j

我将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();

请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

设置正确的mime类型后线程关闭。