我正在尝试使用 Apache POI 类将 Outlook .MSG 文件解码为文本文件。
一切正常,除了println
的方法PrintWriter
:它不会创建新行。
它只是将每个句子直接一个接一个地连接起来。下面代码片段的结果是
"De: textPara: " iso
"De: "
"Para: "
我在几台机器上尝试了该代码:它在我的本地 tomcat 安装(Windows 机器)上运行,但在 Solaris 平台上的 tomcat 或 Weblogic 安装上失败。我认为这与编码算法有关,所以我使用了PrintStream
代替Printwriter
,表示编码 ISO-8859-1,但也没有运气。
任何想法?
try {
byte [] msgByte = Base64.decodeBase64(msgBase64);
InputStream inputMsg = new ByteArrayInputStream(msgByte);
msg = new MAPIMessage(inputMsg);
/* 1. Transform MSG to TXT. */
try {
txtOut = new PrintWriter(outputMsg);
try {
String displayFrom = msg.getDisplayFrom();
txtOut.println("De: "+displayFrom);
} catch (ChunkNotFoundException e) {
_logger.info("Error extrayendo displayFrom: "+e);
}
try {
String displayTo = msg.getDisplayTo();
txtOut.println("Para: "+displayTo);
} catch (ChunkNotFoundException e) {
_logger.info("Error extrayendo displayTo: "+e);
}
} finally {
if(txtOut != null) {
txtOut.close();}
else {
_logger.error("No se ha podido parsear el mensaje.");
}
}
更改以下内容:
txtOut.print("De: "+displayFrom + "\r\n");
txtOut.print("Para: "+displayTo + "\r\n");
这与如何PrintWriter.println() http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#println%28%29生成越线 http://en.wikipedia.org/wiki/Line_break_%28computing%29取决于操作系统。对于unix系统来说是LF (\n),对于 Windows 来说是CR+LF (\r\n).
请注意我如何添加“\r\n“ 意思是CR+LF并使用print()代替打印()。这样生成的换行符不依赖于平台。
您还可以将以下方法添加到您的类中以避免口是心非,只需调用此自定义 println() 而不是直接调用 txtOut.print()。
private static final String LINE_SEPARATOR = "\r\n";
public void println(String str) {
txtOut.print(str + LINE_SEPARATOR);
}
这样你只需调用打印() method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)