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

📄 mimepart.java

📁 封装了SQL、Socket、WAP、MIME等功能的通用组件
💻 JAVA
字号:
package org.lazybug.mime;

import java.io.*;

import com.sun.mail.util.ASCIIUtility;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public abstract class MimePart extends MimeHeaders
{
    /*1.1*/
    public static final String WK_PARAMETER_Q = "q";
    public static final String WK_PARAMETER_CHARSET = "charset";
    public static final String WK_PARAMETER_LEVEL = "level";
    public static final String WK_PARAMETER_TYPE = "type";
    public static final String WK_PARAMETER_NAME = "name";
    public static final String WK_PARAMETER_FILENAME = "filename";
    public static final String WK_PARAMETER_DIFFERENCER = "deifferencer";
    public static final String WK_PARAMETER_Padding = "Padding";
    /*1.2*/
    //public static final String WK_PARAMETER_TYPE = "type";//when used as parameter of Content-Type:multipart/related 1.2
    public static final String WK_PARAMETER_START = "start";
    public static final String WK_PARAMETER_START_INFO = "start-info";
    /*1.3*/
    public static final String WK_PARAMETER_COMMENT = "comment";
    public static final String WK_PARAMETER_DOMAIN = "domain";
    public static final String WK_PARAMETER_MAX_AGE = "max_age";
    public static final String WK_PARAMETER_PATH = "path";
    public static final String WK_PARAMETER_SECURE = "secure";

    public static final byte NEW_LINE[] = {13, 10};
    /*唯一编号*/
    protected int mimei;
    /*被设置的内容*/
    protected byte content[];
    /*装载的内容*/
    protected byte payload[];
    /*父节点*/
    private MimePart parent;
    /*文件名*/
    private String filename;
    /*存放part的文件临时路径*/
    private String filepath;
    /*装载的数据*/
    protected boolean payloading;

    public MimePart()
    {
        payloading = false;
        mimei = 0;
    }

    public MimePart(InputStream inputstream) throws IOException
    {
        payloading = false;
        decode(inputstream);

        /*payload = new byte[inputstream.available()];
        int i = inputstream.read(payload, 0, payload.length);
        //payload = ASCIIUtility.getBytes(inputstream);*/
    }

    /**
     * getContent
     *
     * @return byte[]
     */
    public byte[] getPayload()
    {
        return payload;
    }

    public MimePart getParent()
    {
        return parent;
    }

    public boolean isPayloading()
    {
        return payloading;
    }

    public int getMimei()
    {
        return mimei;
    }

    public String getFilepath()
    {
        return filepath;
    }

    public String getPath()
    {
        if( this.filepath != null && this.filepath.length() > 0 &&
            this.filename != null && this.filename.length() > 0 )
        {
            return this.filepath + "\\" + filename;
        }
        return null;
    }
    /**
     * 得到边界
     * @return String
     */
    public String getBoundary()
    {
        if( this.getContentType() == null ) return null;

        String boundary = this.getContentType().getParameter("boundary");
        if( boundary == null )
        {
            boundary = UniqueValue.getUniqueBoundaryValue();
            this.getContentType().setParameter("boundary", boundary);
        }
        return boundary;
    }
    /**
     * getDescription
     *
     * @return String
     */
    public String getContentID()
    {
        return getHeader("Content-ID");
    }

    public String getContentLocation()
    {
        return getHeader("Content-Location");
    }


    /**
     * getDescription
     *
     * @return String
     */
    public String getDescription()
    {
        return getHeader("Content-Description");
    }

    /**
     * 得到编码类型
     * @return String
     */
    public String getEncoding() throws MimeException
    {
        String s = getHeader("Content-Transfer-Encoding");
        if( s == null || s.length() == 0 ) return null;

        s = s.trim();
        if(s.equalsIgnoreCase("7bit") || s.equalsIgnoreCase("8bit") || s.equalsIgnoreCase("quoted-printable") || s.equalsIgnoreCase("base64"))
            return s;

        HeaderTokenizer headertokenizer = new HeaderTokenizer(s, "()<>@,;:\\\"\t []/?=");
        do
        {
            HeaderTokenizer.Token token = headertokenizer.next();
            int i = token.getType();
            if(i != -4)
            {
                if(i == -1) return token.getValue();
                else return s;
            }
        }
        while(true);
    }

    protected String setReleayFileName()
    {
        filename = getContentType().getParameter("filename");
        if( filename == null )
        {
            String subtype = ct2ext(getContentType().getBaseType());
            filename = getContentType().getParameter("name");
            if( filename != null  )
            {
                if( filename.indexOf('.') == -1 )
                {
                    filename = filename + "." + subtype;
                }
            }

            if( filename == null )
            {
                filename = this.getContentID();
                if( filename == null || filename.length() == 0 )
                {
                    filename = this.getContentLocation();
                    if( filename == null || filename.length() == 0 )
                    {
                        filename = (System.currentTimeMillis()>>16) +"."+ subtype;
                    }
                    else if( filename.indexOf('.') == -1 )
                    {
                        filename = filename + "." + subtype;
                    }
                }
                else
                {
                    //System.out.println("FILENAME="+filename);
                    if( filename.indexOf('.') == -1 )
                    {
                        filename = filename + "." + subtype;
                    }
                }
            }
        }
        //if( parent != null ) filename = this.mimei + "$"+filename;
        return filename;
    }
    /**
     * 设置问文件名称
     * @return String
     */
    public String getFileName()
    {
        return filename;
    }
    /*是否存在临时文件*/
    public boolean isExistTempFile()
    {
        if( this.filepath != null && this.filepath.length() > 0 &&
            this.filename != null && this.filename.length() > 0 )
        {
            File file = new File( this.filepath + "\\" + filename );
            return file.exists();
        }
        return false;
    }
    /*得到文件路径*/
    public String getFileAndPath()
    {
        if( this.filepath != null && this.filepath.length() > 0 &&
            this.filename != null && this.filename.length() > 0 )
        {
            File file = new File( this.filepath + "\\" + filename );
            if( file.exists() ) return file.getPath();
            this.filepath = null;
            this.filename = null;
            return null;
        }
        return null;
    }
    /*设置内容*/
    public void setContent(byte[] buf, String encoding)
    {
        if( !this.isPayloading() )
        {
//            System.out.println("setContent("+buf.length+","+encoding+")");
            this.setEncoding(encoding);
            this.content = buf;
        }
    }
    /**
     * setContentID
     *
     * @param string String
     */
    public void setContentID(String value)
    {
        if( value != null )  this.setHeader("Content-ID", value);
    }

    public void setConetntLocation(String value)
    {
        if( value != null )  this.setHeader("Content-Location", value);
    }

    /**
     * setDescription
     *
     * @param string String
     */
    public void setDescription(String value)
    {
        this.setHeader("Content-Description", value);
    }

    /**
     * 设置编码类型
     * @param s String
     */
    public void setEncoding(String s)
    {
        if( s != null ) setHeader("Content-Transfer-Encoding", s);
    }
     /**
     * setFileName
     *
     * @param string String
     */
    public void setFileName(String string)
    {
        if( string == null ) return;
        this.filename = string;
        this.getContentType().setParameter("filename", string);
    }
    /**
     * getSize
     * @return int
     */
    public int getSize()
    {
        if( content == null ) return -1;
        return content.length;
    }


    public void setParent(MimePart parent)
    {
        this.parent = parent;
        this.mimei = parent.updateMimei(this.mimei);
        //System.out.println("["+this.mimei+"]"+this.getContentType().getBaseType());
    }

    public void setPayloading(boolean payloading)
    {
        this.payloading = payloading;
    }
    /*设置文件路径,判断路径是否包含\\/*/
    public void setFilepath( String filepath )
    {
        filepath = filepath.trim();
        if( filepath.lastIndexOf('\\') == filepath.length() - 1 ||
            filepath.lastIndexOf('/')  == filepath.length() - 1 )
        {
            this.filepath = filepath.substring(0, filepath.length() - 2);
        }
        this.filepath = filepath;
    }

    public int updateMimei(int sub_mimei)
    {
        if( sub_mimei > this.mimei )
        {
            this.mimei = sub_mimei;
        }

        if( this.parent != null )
        {
            this.mimei = this.parent.updateMimei(this.mimei);
            return this.mimei;
        }
        else
        {
            return this.mimei++;
        }
    }

    public abstract void writeTo(OutputStream out) throws IOException;

    /**
     * file ext name to ContentType
     */
    public static String ext2ct(String fileExt)
    {
        String ext = fileExt.toLowerCase();

        if ("gif".equals(ext))
            return "image/gif";
        if ("jpg".equals(ext))
            return "image/jpeg";
        if ("png".equals(ext))
            return "image/png";
        if ("tiff".equals(ext))
            return "image/tiff";
        if ("txt".equals(ext))
            return "text/plain";
        if ("wav".equals(ext))
            return "audio/wav";
        if ("mid".equals(ext))
            return "audio/midi";
        if ("amr".equals(ext))
            return "audio/amr";

        return "";
    }

    public static String ct2ext(String contentType)
    {
        String ct = contentType.toLowerCase();

        if (ct.indexOf("gif")!=-1)
            return "gif";
        if (ct.indexOf("jpg")!=-1 || ct.indexOf("jpeg")!=-1)
            return "jpg";
        if (ct.indexOf("text")!=-1)
            return "txt";
        if (ct.indexOf("amr")!=-1)
            return "amr";
        if (ct.indexOf("mid")!=-1)
            return "mid";
        if (ct.indexOf("wav")!=-1)
            return "wav";
        if (ct.indexOf("smil")!=-1)
            return "smil";
        return "";
    }

    public static void main(String[] args)
    {

    }
}

⌨️ 快捷键说明

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