【问题标题】:Microsoft Word Docx Download Attachment ErrorMicrosoft Word Docx 下载附件错误
【发布时间】:2013-01-31 15:11:22
【问题描述】:

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

请帮忙。谢谢。

【问题讨论】:

  • docx4j 不相关,除非您需要创建或修改 docx
  • 您使用什么 mime 类型,显示垃圾的客户端浏览器和操作系统是什么?
  • 用于创建文件的 mime 类型是 application/msword。浏览器是 Mozilla Firefox。谢谢。请帮忙。
  • 添加一个处理 mime 类型的应用程序。见kb.mozillazine.org/MimeTypes.rdf
  • 对于一个 docx,你应该使用 application/vnd.openxmlformats-officedocument.wordprocessingml.document

标签: docx docx4j


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2016-10-08
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多