?? sendmms.java
字號:
/**
* 基于中移動MM7api開發的短信服務程序
* 作者:馬亮
* 日期:2007-12-06
* com.huawei.bass.gansu.mms.client.SendMms 提供給其他系統調用的彩信發送類
*/
package com.huawei.bass.gansu.mms.client;
import java.io.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.cmcc.mm7.vasp.common.MMConstants;
import com.cmcc.mm7.vasp.common.MMContentType;
import com.huawei.bass.gansu.mms.util.*;
public class SendMms
{
static MmsClient mmsClient;
static {
ApplicationContext factory = new FileSystemXmlApplicationContext(
"./beanClient.xml");
mmsClient= (MmsClient) factory.getBean("mmsClient");
}
static final int TYPE_NORMAL = 0; //普通類型彩信(純文字彩信)
static final int TYPE_SMIL = 1; //通過SMIL文件組織內容的彩信
static final int TYPE_FILE = 2; //通過媒體文件組織內容的彩信
static final int MAX_BYTE = 131072;//彩信內容的最大容量(128K)
static final String IMG = "gif,jpg,jpeg,wbmp,png";//彩信中支持的圖片格式
static final String AUDIO = "arm,mid,midi,mmf,wav,3gp,mp4";//彩信中支持的媒體格式
static int type;
static String[] numbers;
public static void main(String[] args)
{
try
{
if(checkParamWrong(args))//檢查參數規范,避免后續過程發生錯誤
{
System.out.println("參數錯誤:SendMms [-s 使用smil文件組織內容 | -f 使用媒體文件組織內容] 號碼 彩信標題 [[smil文件名(絕對路徑+文件名) | 媒體文件1 第一幀播放時長 t:文字內容 第二幀播放時長 媒體文件2 第三幀播放時長……]{0 | 1 是否自動播放}]");
System.exit(1);
}
if(type == TYPE_NORMAL)//簡單模式彩信
{
sendMms(numbers,args[1],args[2]);
}
if(type == TYPE_SMIL)//smil組織的彩信
{
sendMms(numbers,args[2],args[3],args[4]);
}
if(type == TYPE_FILE)//媒體文件組織的彩信
{
String[] files = new String[args.length - 4];//文件參數個數為總個數減去4
for(int i = 3; i < args.length-1; i++)//提取出文件參數
{
files[i-3] = args[i];
}
sendMms(numbers,args[2],files,args[args.length-1]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 檢查命令行參數是否正確,并根據參數選擇不同的彩信內容組裝發送方式
* @param args 參數列表
* @return
*/
public static boolean checkParamWrong(String[] args)
{
boolean wrong = false;
if(args.length < 3)
wrong = true;
else if(args[0].equals("-s"))//第一個參數是-s,表示通過smil文件生成彩信
{
type = TYPE_SMIL;
numbers = args[1].split(",");
for(int i = 0; i < numbers.length; i++)//第二個參數不是逗號分割的手機號碼時,返回參數錯誤
{
if(!(numbers[i].startsWith("1") && numbers[i].length() == 11))
wrong = true;
}
if(args.length != 5)//參數數目不是5個,返回參數錯誤
wrong = true;
else
{
Matcher matcher = Pattern.compile("\\D").matcher(args[4]);//用正則表達式匹配第五個參數(是否自動播放),如果是非數字,返回參數錯誤
if(matcher.find())
wrong = true;
}
}
else if(args[0].equals("-f"))//如第一個參數是-f,表示通過媒體文件生成彩信
{
type = TYPE_FILE;
numbers = args[1].split(",");
for(int i = 0; i < numbers.length; i++)//第二個參數不是逗號分割的手機號碼時,返回參數錯誤
{
if(!(numbers[i].startsWith("1") && numbers[i].length() == 11))
wrong = true;
}
if(args.length%2 != 0)//如果參數數目不是偶數個,返回參數錯誤
wrong = true;
else
{
for(int i = 5; i < args.length; i+=2)
{
Matcher matcher = Pattern.compile("\\D").matcher(args[i-1]);//用正則表達式匹配每個幀延遲時間參數,如果是非數字,返回參數錯誤
if(matcher.find())
wrong = true;
}
if(Pattern.compile("\\D").matcher(args[args.length-1]).find())//用正則表達式匹配最后一個參數(是否自動播放),如果是非數字,返回參數錯誤
wrong = true;
}
}
else//第一個參數不是-s、-f
{
if(args.length != 3)
wrong = true;
type = TYPE_NORMAL;
numbers = args[0].split(",");
for(int i = 0; i < numbers.length; i++)//第一個參數不是逗號分割的手機號碼時,返回參數錯誤
{
if(!(numbers[i].startsWith("1") && numbers[i].length() == 11))
wrong = true;
}
}
return wrong;
}
/**
* 簡單文字彩信發送方法,自動拼裝smil傳遞給服務端
* @param numbers 接收人號碼
* @param titel 彩信標題
* @param mms 彩信文字內容
* @throws Exception
*/
public static void sendMms(String[] numbers, String titel, String mms) throws Exception
{
if(mms.getBytes().length > MAX_BYTE)//判斷內容是否超出了彩信內容最大長度
throw new Exception("彩信內容超出最大長度限制(128K),請精簡內容");
MmsContent content = new MmsContent();
content.setNumbers(numbers);
content.setTitle(titel);
content.setAutoPlay(false);
Element root = new Element("smil");//拼裝SMIL文件的XML DOM
Document doc = new Document(root);
Element body = new Element("body");
root.addContent(body);
Element par = new Element("par");
par.addAttribute("dur","100000ms");
body.addContent(par);
Element text = new Element("text");
text.addAttribute("src", "1.txt");
par.addContent(text);
XMLOutputter outputter = new XMLOutputter(" ", true);//設置四個空格縮進
ByteArrayOutputStream bos = new ByteArrayOutputStream();
outputter.output(doc, bos);//將DOM輸出到輸出流
bos.close();
MediaContent mediaContent1 = new MediaContent();//添加smil的subContent
mediaContent1.setType(MMConstants.ContentType.SMIL);
mediaContent1.setContent(bos.toByteArray());
mediaContent1.setId("1.smil");
content.addContent(mediaContent1);
MediaContent mediaContent2 = new MediaContent();//添加文本的subContent
mediaContent2.setType(MMConstants.ContentType.TEXT);
mediaContent2.setContent(mms.getBytes());
mediaContent2.setId("1.txt");
content.addContent(mediaContent2);
mmsClient.sendMms(content);
}
/**
* 發送SMIL文件組織的彩信
* @param numbers 接收人號碼
* @param titel 彩信標題
* @param smil smil文件絕對路徑+文件名
* @param autoPlay 是否自動播放
* @throws Exception
*/
public static void sendMms(String[] numbers, String titel, String smil, String autoPlay) throws Exception
{
int size = 0;//彩信內容大小計數器
if(smil.lastIndexOf(".") == -1 || smil.endsWith("."))//如果smil文件中沒有.或者.在最后 拋錯
throw new Exception("smil文件名不正確");
String extendName = smil.substring(smil.lastIndexOf(".")+1);//smil文件擴展名
if(!extendName.equalsIgnoreCase("smil"))//如果不是smil,拋錯
throw new Exception("smil文件擴展名不正確,應為.smil");
File smilFile = new File(smil);
if(!smilFile.exists() || !smilFile.isFile())//如果文件不存在或者不是一個有效的文件,拋錯
throw new Exception("smil文件不存在,請檢查路徑和文件名");
String absolutePath = smilFile.getParent()+File.separator;
MmsContent content = new MmsContent();//設置彩信頭
content.setNumbers(numbers);
content.setTitle(titel);
content.setAutoPlay(autoPlay.equals("0")?false:true);
MediaContent mediaContent = new MediaContent();//向彩信中添加smil文件
mediaContent.setType(MMConstants.ContentType.SMIL);
mediaContent.setContent(fileToByte(smil));//調用fileToByte,將文件讀為字節數組
mediaContent.setId("1.smil");
content.addContent(mediaContent);
DOMBuilder db = new DOMBuilder();//解析smil文件,對里面的內容做檢查,并依次添加媒體文件
Document doc = db.build(smilFile);
Element root = doc.getRootElement();
Element body = root.getChild("body");
List pars = body.getChildren();
String filename;
for(int i = 0; i < pars.size(); i++)//循環par幀節點
{
Element par = (Element)pars.get(i);
Element txt = par.getChild("text");
if(txt != null)//如果有text子節點
{
filename = absolutePath + txt.getAttributeValue("src");//在smil文件所在目錄下尋找文本文件
checkFile(filename,"txt");//檢查文件名是否正確,文件格式是否支持,文件是否存在
MediaContent txtContent = new MediaContent();//向彩信中添加文本文件
txtContent.setType(MMConstants.ContentType.TEXT);
byte[] byteArray = fileToByte(filename);//調用fileToByte,將文件讀為字節數組
if(byteArray.length > MAX_BYTE - size)
throw new Exception("彩信內容超出最大長度限制(128K),請精簡內容");
else
{
txtContent.setContent(byteArray);
size+=byteArray.length;//彩信內容大小計數器累加
txtContent.setId(txt.getAttributeValue("src"));
content.addContent(txtContent);
}
}
Element img = par.getChild("img");
if(img != null)//如果有img子節點
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -