【发布时间】: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