问一个通过javamail保存附件的问题

chinagod 2002-08-22 06:39:24
我写了段程序用来存放邮件中的附件

1: File filename = new File(part.getFileName());
2: FileOutputStream myFileoutputstream = new FileOutputStream(filename);
3: int chunk = part.getSize();
4: byte [] buffer;
5: buffer = new byte[chunk];
6: InputStream instream;
7: instream = part.getInputStream();
8: instream.read(buffer,0,chunk);
9: myFileoutputstream.write(buffer,0,chunk);
10:instream.close();
11:myFileoutputstream.close();

我的附件大小为 2,508,822
邮件大小为 3,435,010

但是我在第3行取出来的附件的值为什么是3,433,127?

另外最后经过不断的测试,发现问题出在第8行,请问怎么解决呀?

...全文
264 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjmmml 2002-08-23
  • 打赏
  • 举报
回复
因为附件是采用特素编码的,所以可能你得到附件名,但是却无法解析,所以无法写到当前目录里面。MimeUtility.decodeText()能解吸outlook发送的附件,但是不能解吸163和sina的,
chinagod 2002-08-22
  • 打赏
  • 举报
回复
InputStream 什么情况下,会引发java.io.IOException错误?
chinagod 2002-08-22
  • 打赏
  • 举报
回复
在上面我说的位置附近,我捕获到一个错误java.io.IOException。
另外我想问下如果保存文件的时候,我不给路径,只给一个附件名,是不是就保存在了javabean所在的目录了?还是一定要给一个绝对路径名
chinagod 2002-08-22
  • 打赏
  • 举报
回复
不是啊,我是一步一步调试的,以目前的程序,我可以取到getFileName的,另外imap方式不能取得附件吗?
我现在换了一种方式:
FileOutputStream myFileoutputstream = new FileOutputStream(filename);
InputStream instream;
instream = part.getInputStream();
int c;
while ((c = instream.read()) != -1)
myFileoutputstream.write(c);
myFileoutputstream.flush();
myFileoutputstream.close();
instream.close();

但是还是不行,出问题的是:
while ((c = instream.read()) != -1)
myFileoutputstream.write(c);
我觉得是不是part.getInputStream()取得不对?导致传给myFileoutputstream或者buffer时出错,又解决的办法吗?
wjmmml 2002-08-22
  • 打赏
  • 举报
回复
另外你的store.connect("localhost",143,uid,"abc123");不对
该成store.connect("pop3.163.com",-1,uid,"abc123");
store = session.getStore("imap");
该成store = session.getStore("pop3");
只要你的uid和密码没错,一定可以的。
wjmmml 2002-08-22
  • 打赏
  • 举报
回复
“然后你用outlook发封不带邮件试试,最好不是中文文件名,如果可以,你在试中文的。”
写错了,是:然后你用outlook发封邮件试试,最好附件先是英文名,如果可以,你在试中文的。
wjmmml 2002-08-22
  • 打赏
  • 举报
回复
把File filename = new File(part.getFileName());该成
File filename = new File(MimeUtility.decodeText(part.getFileName()));
然后你用outlook发封不带邮件试试,最好不是中文文件名,如果可以,你在试中文的。
chinagod 2002-08-22
  • 打赏
  • 举报
回复
try {
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props,null);
Store store = null;
store = session.getStore("imap");
store.connect("localhost",143,uid,"abc123");
Folder folder = store.getDefaultFolder();
folder = folder.getFolder("INBOX");
try {
folder.open(Folder.READ_WRITE);
}catch (MessagingException e) {
return "Fail-open";
}
int totalMessages = folder.getMessageCount();
int newMessages = folder.getUnreadMessageCount();
Message nowMsg = null;
try {
nowMsg = folder.getMessage(currMsgId);
} catch (IndexOutOfBoundsException e) {
return "Out bound in Folder";
}

Part p = nowMsg;
Multipart multipart = (Multipart) p.getContent();
String contType = "";
String attEx = "N";
int partCount = 0;
partCount = multipart.getCount();
ContentType attType = null;
for (int j=0; j < partCount; j++) {
Part part = multipart.getBodyPart(j);

contType = part.getContentType();
attType = new ContentType(contType);
if((contType != null) &&
(attType.match("audio/x-wav")))
{
File filename = new File(part.getFileName());
for (int k=0;filename.exists();k++)
{
filename = new File(part.getFileName()+k);
}
FileOutputStream myFileoutputstream = new FileOutputStream(filename);
int chunk = part.getSize();
byte [] buffer;
buffer = new byte[chunk];
InputStream instream;
instream = part.getInputStream();
instream.read(buffer,0,chunk);
myFileoutputstream.write(buffer,0,chunk);
instream.close();
myFileoutputstream.close();
attEx = part.getFileName()+"<br>"+chunk;
}
}
folder.close(false);
store.close();
return attEx;
} catch (Exception e) {
return "Fail";
}

这时我得到附件的程序,我的整个程序是个Java bean,JSP调用它,然后它将附件存放在服务器端的当前目录中。
现在程序执行到instream.read(buffer,0,chunk);就出错,然后返回“Fail”。能帮忙看看这段代码吗?谢谢尤其感谢 wjmmml
getnull 2002-08-22
  • 打赏
  • 举报
回复

int chunk = part.getSize();//获得附件的大小,不一定很准确。
看到这行了吗???
出什么错啊,你铁出来,不可能的,我的好用啊。
chinagod 2002-08-22
  • 打赏
  • 举报
回复
可我一执行
instream.read(buffer,0,chunk);
就出错呀
另外我取得的附件大小存放在chunk中,可比我的真实附件大
wjmmml 2002-08-22
  • 打赏
  • 举报
回复
我这个方法是在读取邮件列表时调用的,返回的是第i封邮件做有带的所有附件,把名字存在数组里。
你可以根据你的需要修改一下。
wjmmml 2002-08-22
  • 打赏
  • 举报
回复
public String[] getpart(int i){
String[] partname=null;
String m_uploadpath="c:\\upload\\";
try{
folder.open(Folder.READ_WRITE);//READ_ONLY为打开方式
message=folder.getMessages();
Multipart mp = (Multipart)message[i].getContent();
int m=mp.getCount();
partname = new String[m-1];

for (int j=0;j<m;j++)
{
System.out.println("***"+m+"***");
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
System.out.println("disposition="+disposition);
if ((disposition != null) && ((disposition.equalsIgnoreCase(part.ATTACHMENT)) || (disposition.equalsIgnoreCase(part.INLINE))))
{

//以下代码将获得的附件保存到当前目录下,以part.getFileName()为文件名,也既是附件的名称。
System.out.println("partname="+part.getFileName());
String strname=MimeUtility.decodeText(part.getFileName());
String strname0="";
String strname1="";
File filename =null;
System.out.println("defaultencoding="+MimeUtility.getDefaultJavaCharset());
/*if (strname.indexOf(".")!=-1){
strname0=strname.substring(0,strname.lastIndexOf("."));
strname1=strname.substring(strname.lastIndexOf(".")+1);
}else{
strname0=strname;
strname1="";
}*/

//System.out.println("======strname0="+strname0+"strname1="+strname1);
System.out.println("======"+m_uploadpath+gettime());
String sep=System.getProperty("file.separator");
File filepath=new File(m_uploadpath+gettime()+sep);
boolean is= filepath.mkdir();
System.out.println("is="+is);
if (filepath.exists()){
filename = new File(filepath,strname);
}
partname[j-1]=filename.toString();
System.out.println("filename="+filename);
FileOutputStream myFileoutputstream= new FileOutputStream(filename);
int chunk = part.getSize();//获得附件的大小,不一定很准确。

byte [] buffer = new byte[chunk];
InputStream instream = part.getInputStream();
instream.read(buffer,0,chunk);
myFileoutputstream.write(buffer,0,chunk);
instream.close();
myFileoutputstream.close();
}
}
}catch(Exception e){
setInfo("取得附件时异常:"+e.toString());
System.out.println("取得附件时异常:"+e.toString());
}
return partname;

}
我的程序一直用这个,你要在c盘建一个upload目录。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧