文件控件上傳.msg格式文件,達到預覽效果
前端控件代碼就介紹了,重點是解析文件。我是用了outlook-message-parser來解析文件。
代碼展示
首先是maven依賴
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>outlook-message-parser</artifactId>
<version>1.7.4</version>
</dependency>
其次代碼
/**
* 解析MSG郵件,可以將郵件以HTML展示。
* @param file MSG格式郵件的全路徑
* @return vo
* @throws IOException IO異常
*/
public static EmailPreviewVo msgParseToPreview(File file) throws IOException {
EmailPreviewVo vo = new EmailPreviewVo();
OutlookMessageParser msgp = new OutlookMessageParser();
OutlookMessage msg = msgp.parseMsg(file.getAbsolutePath());
List<FileVo> attachList = new ArrayList<>();
for(int i=0; i < msg.getOutlookAttachments().size(); i++) {
OutlookFileAttachment attachment = (OutlookFileAttachment) msg.getOutlookAttachments().get(i);
String attachName = attachment.getFilename();
File attachementFile = null;
// 創建文件 可根據自己實際情況進行使用自己的方法
/**
* public static String getSuffix(String fileName) {
* if (fileName.contains(".")) {
* String suffix = fileName.substring(fileName.lastIndexOf("."));
* return trimilSpace(suffix.toLowerCase());
* }
* throw new AppException("文件沒有後綴");
* }
*/
if (Utils.existSuffix(attachName)) {
String suffix = Utils.getSuffix(attachName);
attachementFile = Utils.createTmpFile(suffix);
/**
* public static File createTmpFile(String suffix) {
* return new File(getTmpDir(), UUID.randomUUID().toString().replace("-", "") + suffix);
* }
*/
} else {
attachementFile = Utils.createTmpFileWithName(attachName);
/**
* public static File createTmpFileWithName(String fileName) {
* return new File(getTmpDir(), fileName);
* }
*/
}
InputStream is = new ByteArrayInputStream(attachment.getData());
org.apache.commons.io.FileUtils.copyInputStreamToFile(is, attachementFile);
if (attachementFile != null) {
FileVo fileVo = new FileVo();
fileVo.setFileName(attachName);
fileVo.setFileLength(attachementFile.length());
fileVo.setFilePath(attachementFile.getAbsolutePath());
attachList.add(fileVo);
}
}
vo.setAttachments(attachList);
// 內容 要處理下不然他會帶有微軟雅黑的樣式,與原郵件樣式不符
Document doc = Jsoup.parse(msg.getConvertedBodyHTML());
Elements bodyList = doc.select("body");
if (bodyList.size() > 0) {
Element bodyEle = bodyList.first();
if (bodyEle.html().length() > 0) {
vo.setContent(bodyEle.html());
}
}
// 消息頭信息
vo.setSentDate(DateUtils.fmt(msg.getClientSubmitTime()));// 日期格式化,自己手動處理下
vo.setFrom(msg.getFromEmail());
vo.setTo(getMailUser(msg, msg.getDisplayTo().trim()));
vo.setCc(getMailUser(msg, msg.getDisplayCc().trim()));
vo.setSubject(msg.getSubject());
return vo;
}
處理收、抄人顯示
/**
* MSG 以名稱獲取真實收發抄郵件地址
* @param msg OutlookMessage
* @param parm 人員成名
* @return 展示名稱
*/
private static String getMailUser(OutlookMessage msg, String parm) {
List<String> parmList = null;
OutlookRecipient recipient = null;
StringBuffer sb = new StringBuffer();
if (parm.contains(";")) {
parmList = Arrays.asList(parm.split(";")).
stream().map(s -> s.trim()).collect(Collectors.toList());
for (int i = 0; i < parmList.size(); i++) {
String value = parmList.get(i);
if (msg != null && msg.getRecipients().size() > 0) {
recipient = msg.getRecipients().stream().filter(e -> e.getName().equals(value)).collect(Collectors.toList()).get(0);
sb.append(recipient.getName());
sb.append(" <" + recipient.getAddress() + ">");
if (i != (parmList.size() - 1)) {
sb.append(",");
}
}
}
} else {
recipient = msg.getRecipients().stream().filter(e -> e.getName().equals(parm)).collect(Collectors.toList()).get(0);
sb.append(recipient.getName());
sb.append(" <" + recipient.getAddress() + ">");
}
return sb.toString();
}
EmailPreviewVo 實體
public class EmailPreviewVo {
private Long id;
private String from;
private String cc;
private String to;
private String subject;
private String sentDate;
private String content;
private List<FileVo> attachments;
}