当前位置: 代码迷 >> Java Web开发 >> javamail 程序检测不到附件,
  详细解决方案

javamail 程序检测不到附件,

热度:1001   发布时间:2016-04-17 11:04:15.0
javamail 程序检测不到附件,求助!
这是代码~就是运行不了if语句里面的语句,求助!!!
Java code
Multipart mp = (Multipart)msgs[msgs.length-1].getContent();            for (int i=0, n=mp.getCount(); i<n; i++) {                Part part = mp.getBodyPart(i);                String disposition = part.getDisposition();                if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) {                    //saveFile(part.getFileName(), part.getInputStream());                    writeToSDFromInput(SDPATH,"300034012109960_012503.sbd",part.getInputStream());                    System.out.println(part.getInputStream());                }            }


------解决方案--------------------
/**
*【保存附件】
*/

public void saveAttachMent(Part part) throws Exception {
String fileName = "";
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart mpart = mp.getBodyPart(i);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE)))) {
fileName = mpart.getFileName();
if (fileName.toLowerCase().indexOf("gb2312") != -1) {
fileName = MimeUtility.decodeText(fileName);
}
saveFile(fileName, mpart.getInputStream());
} else if (mpart.isMimeType("multipart/*")) {
saveAttachMent(mpart);
} else {
fileName = mpart.getFileName();
if ((fileName != null)
&& (fileName.toLowerCase().indexOf("GB2312") != -1)) {
fileName = MimeUtility.decodeText(fileName);
saveFile(fileName, mpart.getInputStream());
}
}
}
} else if (part.isMimeType("message/rfc822")) {
saveAttachMent((Part) part.getContent());
}
}

也可能是jar有冲突,导致什么都是空值
------解决方案--------------------
你用try catch检查一下有无异常,要catch Exception
------解决方案--------------------
你程序里一共用了哪些jar