我将 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();
请帮忙。谢谢。