csdntest.java
上传用户:liangcc
上传日期:2019-05-24
资源大小:4412k
文件大小:3k
- package com.softeem.webmail.action.receivemail;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.util.Vector;
- import javax.mail.Message;
- import javax.mail.Multipart;
- import javax.mail.Part;
- import javax.servlet.http.HttpServletRequest;
- import org.omg.CORBA.Request;
- public class csdntest {
- public synchronized Vector getAtta(Message message,HttpServletRequest request) {
- try {
- Vector v_atta = new Vector();//用于存贮附件保存的路径和文件名
- String Content = "";
- String[] fileName = null;
- InputStream[] inputStream = null;
- Object o = message.getContent(); //当Message对象含有附件时,其内容会以Multipart的形式存在
- if (o instanceof String) {
- // return null;
- //不含附件
- } else if (o instanceof Multipart) {
- //可能含有附件
- //以Multipart的getBodyPart方法取得内容部分,并以getDisposition方法取得邮件的配置
- Multipart mp = (Multipart) o;
- fileName = new String[mp.getCount()];
- inputStream = new InputStream[mp.getCount()];
- // OnlineEdit oe = new OnlineEdit();
- // String Online_ID = oe.getOnline_ID();
- for (int i = 0, n = mp.getCount(); i < n; i++) {
- Part part = mp.getBodyPart(i);
- if (i == 0) {
- Content = part.getContent().toString();
- }
- String disposition = part.getDisposition();
- //判断Multipart中是否含有附件的方法.则是依据getDisposition所返回的内容
- if ((disposition != null)
- && (disposition.equals(Part.ATTACHMENT) || disposition
- .equals(Part.INLINE))) {
- fileName[i] = part.getFileName();
- System.out.println(fileName[i]);
- fileName[i] = fileName[i].valueOf(i)
- + fileName[i].substring(fileName[i]
- .indexOf("."), fileName[i].length()-2);//更换文件名
- fileName[i] = "E" + "ss" + fileName[i];
- //System.out.println(fileName[i]);
- inputStream[i] = part.getInputStream();
- System.out.println(inputStream[i].toString());
- // PropPath pp = new PropPath();
- String pathName = request.getContextPath();
- // pathName = pp.getAppPath(pathName);
- ////////////////存贮附件保存的路径和文件名
- v_atta.add(pathName + fileName[i]);
- ////////////////
- File file = new File(pathName + fileName[i]);
- FileOutputStream outfile = new FileOutputStream(file);
- BufferedInputStream br = new BufferedInputStream(
- inputStream[i]);
- BufferedOutputStream bw = new BufferedOutputStream(
- outfile);
- int b;
- while ((b = br.read()) != -1) {
- bw.write(b);
- }
- bw.flush();
- outfile.close();
- }
- }
- }
- Vector V_res = new Vector();
- V_res.add(fileName);
- V_res.add(Content);
- V_res.add(v_atta);
- return V_res;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }