我必须使用换行符从javax.mail.Message中读取内容。 目前我使用这个算法,我已经在stackoverflow上找到了它,但是在读取内容后,内容没有新行,所以当我读取字符串时,Scanner.nextLine()只读一行。
private String getTextFromMimeMultipart(
MimeMultipart mimeMultipart) throws Exception{
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart.getContent();
} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
} else if (bodyPart.getContent() instanceof MimeMultipart){
result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
}
}
return result;
}
答案 0 :(得分:1)
好的,我已经解决了我的问题。字符串没有新行,因为Jsoup.parse()删除所有新行。这是解决方案:
String html = (String) bodyPart.getContent();
String parsedHtml = Jsoup.parse(html.replaceAll("(?i)<br[^>]*>","br2n")).text();
result = result + System.getProperty("line.separator") + parsedHtml.replaceAll("br2n", System.getProperty("line.separator"));