【发布时间】:2013-04-29 10:46:37
【问题描述】:
我编写了一个 servlet,它使用 msgparser 读取 Outlook .msg 文件并将内容写入 ServletOutputStream 以便在点击 url 后可以下载它。
我面临的问题是,当我尝试在 Outlook 中打开下载的文件时,该文件报告错误。
错误提示:无法打开该文件,因为它可能不存在或者您可能无权访问该文件......
即使文件格式正确(.msg),也会出现这样的错误。我确定我在解析时做错了什么。请建议。下面是servlet代码:
MsgParser msgp = new MsgParser();
Message msg = msgp.parseMsg("D:\\Demo.msg");
String str1=msg.toString();
byte[] b=str1.getBytes();// here b is byte array
//The below code is to open show the pop up so that user can save the msg file..
response.setContentType("application/vnd.ms-outlook"+" ;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=" + "Demo.msg");
ServletOutputStream servletOutputStream = response.getOutputStream();
DataOutput dataOutput = new DataOutputStream(servletOutputStream);
if (b!= null) {
response.setContentLength(b.length);
for (int i = 0; i < b.length; i++) {
dataOutput.writeByte(b[i]);
}
}
if (servletOutputStream != null) {
servletOutputStream.flush();
servletOutputStream.close();
}
PrintWriter pw = response.getWriter();
pw.println(dataOutput);
【问题讨论】:
-
“即使文件格式正确(.msg)”。文件扩展名不足以说明格式正确。使用文本查看器或编辑器打开文件并检查其内容。但是,这是权限错误,而不是格式错误。
-
是的!我检查了notepad++中的内容,内容是正确的。但是当我双击它在outlook中打开时,它仍然报告错误。
-
有人请对此有所了解...这会有所帮助!
-
MsgParser只是没有返回有效.msg-stream 的功能。您使用的toString方法与您期望的不同:它只是返回人类可读的文本。您必须使用POIFS-library 将Message转换为有效的.msg-data 流。 -
我在 POIFS 库中没有看到任何针对此要求的实现。请提供一些链接或文档以实现此目的。
标签: servlets outlook outputstream msg