/**
* 解析邮件中的嵌入资源
*/
public List<RelatedMap> getMailRelated(Part part, String realpath, String path, Long id) throws Exception {
List<RelatedMap> list = new ArrayList<RelatedMap>();
// String contentType = part.getContentType().toLowerCase();
// String ct = null;
if ((part.isMimeType("application/octet-stream") || part.isMimeType("image/*"))
&& part instanceof MimeBodyPart) {
String cid = ((MimeBodyPart)part).getContentID();
cid = cid.replace("<", "").replace(">", "");
// System.out.println("---cid:" + cid);
InputStream is = part.getInputStream();
BASE64DecoderStream bds = new BASE64DecoderStream(is);
int buffer = bds.available();
//文件名称=邮件id+cid
String filename = id+cid;
String filepath = "related"+"/"+filename;
File dir = new File(realpath+"related");
if (!dir.exists())dir.mkdirs();
String fullpath = realpath + filepath;
FileOutputStream fos = new FileOutputStream(fullpath);
BufferedOutputStream dest = new BufferedOutputStream(fos, buffer);
int count = 0;
byte[] bt = new byte[part.getSize()];
while((count = is.read(bt, 0, buffer)) != -1)
{
bds.decode(bt);
dest.write(bt,0,buffer);
}
dest.flush();
dest.close();
is.close();
fos.close();
cid = "cid:"+cid;
RelatedMap related = new RelatedMap(cid, filepath);
list.add(related);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
list.addAll(getMailRelated(multipart.getBodyPart(i), realpath, path, id));
}
}
return list;
}