问题描述
- javamail 收取邮件的附件保存,出现很奇葩的错误,各位大神快看看吧
-
/*** 【保存附件】
/
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 != null)){
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());
}
private void saveFile(String fileName, InputStream in) throws Exception {
String root = ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(root); if (!file.exists()) { file.mkdir();
}
System.out.println("file path: " + file.toString()+"/"+fileName);BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(root,fileName)));
bis = new BufferedInputStream(in);
int c;
byte[] buffer = new byte[500];
while ((c = bis.read()) != -1) { //到这报错了
bos.write(c);bos.flush();
}
} catch (Exception exception) {
exception.printStackTrace();
throw new Exception("文件保存失败!");
} finally {
bos.close();
bis.close();
}
错误很奇怪:
java.io.IOException: A9 BAD Parse command error
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:151)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:208)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:234)
at com.sun.mail.util.BASE64DecoderStream.getByte(BASE64DecoderStream.java:354)
at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:245)
at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:140)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at ericsson.csdp.entity.ReciveOneMail.saveFile(ReciveOneMail.java:170)
at ericsson.csdp.entity.ReciveOneMail.saveAttachMent(ReciveOneMail.java:135)
at ericsson.csdp.service.hc.RecordService.updateRecordFromEmail(RecordService.java:2063)
at ericsson.csdp.service.hc.RecordService$$FastClassByCGLIB$$fd58d774.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
解决方案
应该是系统的安全性能做的太好了把,苹果系统一般都 有