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

📄 pushutil.java

📁 WAPPUSH的JAVA代码,已经通过了CMPP的测试,多种手机可以支持.
💻 JAVA
字号:
package com.wap;



import java.io.*;
import java.util.*;

/**
 * push format
 *
 * @author
 * @version 1.0 2002.9.12
 */
public class PushUtil {
    public static final int MAX_SMS_SIZE = 140;

    public byte[] getSI(String url, String tip) throws IOException
    {

//        
        byte[] header = new byte[]
        {
            (byte)0x06,
            (byte)0x05,
            (byte)0x04,
            (byte)0x0B,
            (byte)0x84,
            (byte)0x23,
            (byte)0xF0,
            (byte)getRandomInt(),
            (byte)0x06,
            (byte)0x01,
            (byte)0xAE,
            (byte)0x02,
            (byte)0x05,
            (byte)0x6A,
            (byte)0x00,
            (byte)0x45,
            (byte)0xC6,
            (byte)0x08,
            (byte)0x0C
        };

        //当前时间的字符串
        //log("***********************getSI****************");
//        java.util.GregorianCalendar calendar = new java.util.GregorianCalendar();
        Calendar calendar = Calendar.getInstance();
        String currentTime = getTimeString(calendar);
        calendar.add(calendar.MONTH, 1); //一个月以后的时间
        String nextTime = getTimeString(calendar);
        byte[] current = getTimeBytes(currentTime);
        byte[] next = getTimeBytes(nextTime);
        byte[] tipBytes = chString2UTF_8_byte(tip);
        byte[] urlBytes = getSI_url(url);

        int allLen = header.length+urlBytes.length+current.length+next.length+tipBytes.length+5;
        //log("***********************getSI****************"+allLen);
        if ( allLen > MAX_SMS_SIZE ){
            //throw new ExceedException("The sms is too long!");
            throw new IOException("The sms is too long!");
        }
        System.out.println("allLen = " + allLen);
        //组合
       // log("***********************getSI2****************"+);
        byte[] result = new byte[allLen];
        int i = 0;
        int k = 0;
        for (i=0; i<header.length; i++){
            result[k++] = header[i];
        }
        for (i=0; i<urlBytes.length; i++){
            result[k++] = urlBytes[i];
        }
        result[k++] = (byte)0x0A;
        for (i=0; i<current.length; i++){
            result[k++] = current[i];
        }
        result[k++] = (byte)0x10;
        for (i=0; i<next.length; i++){
            result[k++] = next[i];
        }
        result[k++] = (byte)0x01;
        for (i=0; i<tipBytes.length; i++){
            result[k++] = tipBytes[i];
        }
        result[k++] = (byte)0x01;
        result[k++] = (byte)0x01;
        return result;
    }

    private int getRandomInt() {
        return ( (new Random()).nextInt(255) );
    }

    private String getTimeString(Calendar calendar)
    {
        return String.valueOf(calendar.get(calendar.YEAR))
                + int2str(calendar.get(calendar.MONTH)+1)
                + int2str(calendar.get(calendar.DATE))
                + int2str(calendar.get(calendar.HOUR_OF_DAY))
                + int2str(calendar.get(calendar.MINUTE))
                + "00" ;
                //+ int2str(calendar.get(calendar.SECOND)) ;
    }

    //timeStr的格式: yyyymmddhhmiss
    private byte[] getTimeBytes(String timeStr)
    {
        Vector by = new Vector();
        int temp = 0;
        boolean skip = true;
        for (int i=timeStr.length()/2-1; i>=0; i--){
            temp = Integer.parseInt(timeStr.substring(i*2, i*2+2));
            if (temp > 0 || temp==0 && !skip){
                if ( skip ){
                    skip = false;
                }
                by.add(new Byte( (byte)(Integer.decode("0x" + String.valueOf(temp)).intValue()) ));
            }
        }
        byte[] bytes = new byte[by.size() + 2];
        bytes[0] = (byte)0xC3;
        bytes[1] = (byte)(by.size());
        for (int i=0; i<by.size(); i++){
            bytes[i+2] = ((Byte)by.elementAt(by.size()-1-i)).byteValue();
        }
        return bytes;
    }

    private byte[] getSI_url(String url) {
        Vector by = new Vector();
        if ( url.startsWith("http://") ){
            url = url.substring(7);
        }
        int midByte = 0;
        byte[] front = null;
        byte[] after = null;
        String[] suffix = new String[]{ ".com", ".edu", ".net", ".org"};
        int[] token = new int[]{ 133, 134, 135, 136}; //85,86,87,88
        for (int i=0; i<suffix.length; i++){
            if ( url.endsWith( suffix[i] ) || url.endsWith( suffix[i]+"/" ))
            {
                midByte = token[i];
                front = enString2byte( url.substring(0, url.indexOf(suffix[i]) ) );
                break;
            }
        }
        if ( midByte == 0 ){
            for (int i=0; i<suffix.length; i++){
                if ( url.indexOf( suffix[i]+"/" )!=-1 && !url.endsWith( suffix[i]+"/" ) ){
                    midByte = token[i];
                    front = enString2byte( url.substring(0, url.indexOf(suffix[i]+"/")) );
                    after = enString2byte( url.substring(url.indexOf(suffix[i]+"/")+5) );
                    break;
                }
            }
            //没有包含".com", ".edu", ".net", ".org"等
            if ( front == null){
                front = enString2byte( url );
            }
        }

        int allLen = front.length;
        if ( midByte != 0){
            allLen ++;
        }
        if ( after != null ){
            allLen += after.length;
        }


        byte[] bytes = new byte[allLen];
        int i = 0;
        for (i=0; i<front.length; i++){
            bytes[i] = front[i];
        }
        if ( midByte != 0){
            bytes[ front.length ] = (byte)midByte;
        }
        if ( after != null ){
            for (i=0; i<after.length; i++){
                bytes[i+front.length+1] = after[i];
            }
        }
        return bytes;
    }

    private byte[] enString2byte(String str) {
        byte[] tmp = str.getBytes();
        byte[] result = new byte[tmp.length + 2];
        result[0] = (byte)0x03;
        for (int i=0; i<tmp.length; i++){
            result[i+1] = tmp[i];
        }
        result[result.length - 1] = (byte)0x00;
        return result;
    }

    private byte[] chString2UTF_8_byte(String str) {
        byte[] tmp = null;
        try{
            str = new String(str.getBytes(),"GB2312");
            tmp = str.getBytes("UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        byte[] result = new byte[tmp.length + 2];
        result[0] = (byte)0x03;
        for (int i=0; i<tmp.length; i++){
            result[i+1] = tmp[i];
        }
        result[result.length - 1] = (byte)0x00;
        return result;
    }

    private String int2str(int i){
        String s = String.valueOf(i);
        if (s.length()<2){
            s = "0" + s;
        }
        return s;
    }
    

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 

		PushUtil pushUtil=new PushUtil();
//		try {
////			System.out.println(pushUtil.byteToHexStr(pushUtil.getSI("wap.dg1860.com", "好dg1860")));
//		} catch (IOException e) {
//			// TODO 自动生成 catch 块
//			e.printStackTrace();
//		}

	}
	
	
}

⌨️ 快捷键说明

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