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

📄 pushutila8.java

📁 WAPPUSH的JAVA代码,已经通过了CMPP的测试,多种手机可以支持.
💻 JAVA
字号:
package com.wap;
import java.io.*;
import java.util.*;
public class PushUtilA8 {

	public static final int MAX_SMS_SIZE = 140;
	
	public byte[] getSL( String url ) { 
		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)0xB0,
                                        (byte)0x02,
                                        (byte)0x06,
                                        (byte)0x6A,
                                        (byte)0x00,
                                        (byte)0x85,
                                        (byte)0x09 };
        
        //java.util.GregorianCalendar calendar = new java.util.GregorianCalendar();
        //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;
        int allLen = header.length + urlBytes.length + 1;
        if ( allLen > MAX_SMS_SIZE ){
        	//throw new ExceedException("The sms is too long!");
//            throw new ConvertFailedException("The sms is too long!");
        }
        System.out.println("allLen = " + allLen);
        //组合
        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)0x01;
        return result;
	}
	
	public byte[] getSI(String url, String tip)  { //ExceedException {
		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 };
        
        //当前时间的字符串        
        java.util.GregorianCalendar calendar = new java.util.GregorianCalendar();
        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;
        if ( allLen > MAX_SMS_SIZE ){
        	//throw new ExceedException("The sms is too long!");
//            throw new ConvertFailedException("The sms is too long!");
        }
        System.out.println("allLen = " + allLen);
        //组合
        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;
	}

	/**
	 * @param a numerator
	 * @param b Denominator
	 * @return Maximum Multiple of a/b
	 */
	public static int mm(int a, int b) {
		return a%b==0 ? a/b : (a/b+1);
	}

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

    private String getTimeString(GregorianCalendar 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;
    }

    public 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;
	}
	public  String byteToHexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer(2 * buf.length);
		for(int i = 0; i < buf.length; i++) {
			int h = (buf[ i ] & 0xf0) >> 4;
		int l = (buf[ i ] & 0x0f);
		sb.append(new Character((char)((h > 9) ? 'a' + h - 10 : '0' + h)));
		sb.append(new Character((char)((l > 9) ? 'a' + l - 10 : '0' + l)));
		}
		return sb.toString().toUpperCase();
	}

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

		PushUtilA8 pushUtilA8=new PushUtilA8();
		System.out.println(pushUtilA8.byteToHexStr(pushUtilA8.getSI_url("wap.dg1860.ne/")));
		System.out.println(pushUtilA8.byteToHexStr(pushUtilA8.getSI_url("http://wap.dg1860.com")));
//		System.out.println(pushUtilA8.byteToHexStr(pushUtilA8.getSI("wap.dg1860.com", "好dg1860")));

	}

}

⌨️ 快捷键说明

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