⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attachmentaction.java

📁 mail sent and revesive system jsp+mysql
💻 JAVA
字号:
package fengyun.Fastmail.beans;

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import javax.servlet.ServletInputStream;
import javax.mail.*;
import javax.activation.DataHandler;
import javax.mail.internet.*;
import fengyun.Fastmail.Maildir.*;
import java.util.*;
import javax.activation.FileDataSource;
import javax.activation.MimeTypeParameterList;
import fengyun.Fastmail.util.ToolKit;

/**
 * 附件相关动作
 * @author fengyun
 * @version 1.01
 */
public class AttachmentAction {
	
	private	static BeansConstants CONST = BeansConstants.getInstance();

	/**
	 * 上载文件
	 */
	public static int Upload(HttpServletRequest request) {		
		int status = CONST.OK;

		String strContentType = request.getContentType();
		if (strContentType == null) return CONST.HTTPREQUEST_ERROR;

		int ContentLength = request.getContentLength();
		HttpSession httpsession = request.getSession(false);
		if (httpsession == null) {
			return CONST.HTTPSESSION_ERROR;
		}
	    Attachment cInfo = null;											//附件信息
        try
           {
                ServletInputStream sis = request.getInputStream();
				if(sis == null)
				    return CONST.HTTPREQUEST_ERROR;
				if(ContentLength > CONST.attachmaxsize)
				{
				    sis.skip(ContentLength);
				    sis.close();
				    return CONST.ATTACHSIZE_BIG;
				}
			
				String tmpOutFileName = CONST.attachmentpool + File.separator + System.currentTimeMillis()/1000 + ";" + httpsession.getAttribute(CONST.FastmailUserid);

                File tmpOutFile = new File(tmpOutFileName);
                FileOutputStream fos = new FileOutputStream(tmpOutFile);
				strContentType = "Content-type: multipart/mixed; " + strContentType.substring(strContentType.indexOf("boundary")) + "\n\n";
                fos.write(strContentType.getBytes());

				ToolKit.StreamCopy(sis,fos,ContentLength);
                sis.close();
                fos.close();

				if(ContentLength > CONST.attachmaxsize){
					tmpOutFile.delete();
					return CONST.ATTACHSIZE_BIG;
				}

                FileInputStream fis = new FileInputStream(tmpOutFile);
				
			    Properties prop = new Properties();

			    Session mailsession = Session.getInstance(prop, null);
                MimeMessage mimemessage = new MimeMessage(mailsession,fis);
                MimeMultipart mimemultipart = (MimeMultipart)mimemessage.getContent();
				
                for(int j = 0; j < mimemultipart.getCount(); j++)
                {
                    MimeBodyPart mimebodypart = (MimeBodyPart)mimemultipart.getBodyPart(j);
					String[] strArray = mimebodypart.getHeader("Content-Disposition");
                    String strDisposition = strArray[0].substring(9);
                    String tmpfullfilepath = new String();
                    if("".equals(mimebodypart.getFileName()))
                    {
                        tmpfullfilepath = strDisposition;
                    }
                    else
                    {
                        for(int k = 0; k < strDisposition.length(); k++)
                        {
                            char c = strDisposition.charAt(k);
                            if(c == '\\')
                                tmpfullfilepath = tmpfullfilepath + "/";
                            else
                                tmpfullfilepath = tmpfullfilepath + c;
                        }

                    }
                    MimeTypeParameterList mimetypeparameterlist = new MimeTypeParameterList(tmpfullfilepath);
                    String strUpload = mimetypeparameterlist.get("name");
                    if(strUpload.equals(CONST.upload))
                    {
                        String paraFileName = mimetypeparameterlist.get("filename");
						if(paraFileName != null) {
							String strFileName = (new File(paraFileName)).getName();
							Vector vtrAttach = (Vector)httpsession.getAttribute(CONST.FastmailAttach);
							if (vtrAttach == null) {
								vtrAttach = new Vector();
								httpsession.setAttribute(CONST.FastmailAttach,vtrAttach);
							}
							boolean isExists = false;
							Attachment tmpInfo = null;
							for(int i=0;i < vtrAttach.size();i++) {
								tmpInfo = (Attachment)vtrAttach.elementAt(i);
								if (tmpInfo.getFullName().equals(paraFileName)) {
									isExists = true;
									break;
								}
							}
							if(strFileName != null && strFileName.length() > 0 && !isExists){
								cInfo = new Attachment();
								cInfo.setUpload(true);
								cInfo.setAttached(true);
								cInfo.setContentType(mimebodypart.getContentType());
								cInfo.setFileName(strFileName);
								cInfo.setFullName(paraFileName);
								//add by fengyun
								String tmpFullFileName = CONST.attachmentpool + File.separator + System.currentTimeMillis() /1000 + ";" + httpsession.getAttribute(CONST.FastmailUserid)  + ";" + strFileName;
								File tmpFile = new File(tmpFullFileName);
								if (!tmpFile.exists()) {
									cInfo.setTempAbsolute(tmpFullFileName);
									fos = new FileOutputStream(tmpFile);
									BufferedInputStream bis = new BufferedInputStream(mimebodypart.getInputStream());
									byte[] cByte = new byte[1024];
									int read = 0;
									int size = 0;
									while(true) {
										read = bis.read(cByte);
										if(read==-1) break;
										size += read;
										fos.write(cByte,0,read);
									}
									bis.close();
									fos.close();
									cInfo.setSize(size);
								}
								vtrAttach.add(cInfo);
							}
						}
                    }
		        }
                fis.close();
                tmpOutFile.delete();
				status = CONST.OK;
            }
            catch(Exception e)
            {
                e.printStackTrace();
				return CONST.UNEXPECTED;
            }
		return status;
	}
	/**
	 * 删除附件
	 */
	public static int Delete(HttpServletRequest request){
		int status = CONST.OK;
		
		HttpSession httpsession = request.getSession(false);
		if (httpsession == null) {
			return CONST.HTTPSESSION_ERROR;
		}

		Vector vAttach = (Vector) httpsession.getAttribute(CONST.FastmailAttach);
		if (vAttach == null && vAttach.size() < 0) {
			return CONST.HTTPREQUEST_ERROR;
		}
		int index = -1;
		try {
			index = Integer.parseInt(request.getParameter(CONST.index));
		}
		catch(Exception e) {
			e.printStackTrace();
			return CONST.HTTPREQUEST_ERROR;
		}
		try {
			Attachment aInfo = (Attachment)vAttach.get(index);
			File tmpFile = new File(aInfo.getTempAbsolute());
			if (tmpFile.exists()) tmpFile.delete();
			vAttach.remove(aInfo);
		}catch(Exception e){
			e.printStackTrace();
			return CONST.ATTACHDEL_ERROR;
		}finally{
			return status;
		}
	}
	/**
	 * 表单
	 */
	public static void Compose(HttpServletRequest request) {
		HttpSession httpsession = request.getSession(false);
		String strTemp = request.getParameter(CONST.subject);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.subject,strTemp);
		}
       	strTemp = request.getParameter(CONST.to);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.to,strTemp);
		}
       	strTemp = request.getParameter(CONST.cc);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.cc,strTemp);
		}
       	strTemp = request.getParameter(CONST.bcc);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.bcc,strTemp);
		}
       	strTemp = request.getParameter(CONST.body);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.body,strTemp);
		}
		strTemp = request.getParameter(CONST.save);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.save,strTemp);
		}
		strTemp = request.getParameter(CONST.folderid);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.folderid,strTemp);
		}
		strTemp = request.getParameter(CONST.messageid);
		if (strTemp!=null) {
			httpsession.setAttribute(CONST.messageid,strTemp);
		}
	}
	/**
	 * 粘贴
	 */
	public static int Attach(HttpServletRequest request) {
		int status = CONST.OK;
  		String[] attached = request.getParameterValues(CONST.attach);
		HttpSession httpsession = request.getSession(false);
		if (httpsession == null) {
			return CONST.HTTPSESSION_ERROR;
		}

		Vector vAttach = (Vector) httpsession.getAttribute(CONST.FastmailAttach);
		if (vAttach == null && vAttach.size() < 0) {
			return CONST.HTTPREQUEST_ERROR;
		}
  		if (attached !=null && attached.length > 0) {
			for(int i = 0; i < vAttach.size(); i++) {
  				Attachment ai = (Attachment)vAttach.get(i);
				if (ai.isUpload()) ai.setAttached(false);
			}
  			for(int i = 0; i < attached.length; i++) {
  				int index = Integer.parseInt(attached[i]);
  				Attachment ai = (Attachment)vAttach.get(index);
  				if (ai != null) ai.setAttached(true);
  			}
  		}
		return status;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -