www.pudn.com > mobileMms.rar > SendThread.java


package cn.netjava.mmsclient; 
 
import java.sql.*; 
import java.util.*; 
import java.io.*; 
import java.util.logging.Level; 
 
import com.cmcc.mm7.vasp.service.*; 
import com.cmcc.mm7.vasp.message.*; 
import com.cmcc.mm7.vasp.conf.*; 
import com.cmcc.mm7.vasp.common.*; 
 
import cn.netjava.mmsclient.util.*; 
 
 
/** 
 *  
 * 

Title:中国移动mm7协议客户端 V0.1

*

Description: 发送线程

*

Company:蓝杰实训

* @author NetJava.cn * @version 0.1 */ public class SendThread extends Thread { public SendThread() { } static DataBase db = DataBase.getInstance(); static LinkedList list = new LinkedList(); static String errSubmit = "-100"; public void run() { while (true) { try { Thread.sleep(100); // System.out.println("run............."); get4db(); sendMMS(); } catch (Exception ex) { LogManage.ins().logger.severe("SendThread : " + ex.getMessage()); } } } private void get4db() { int size = 10; int tmpid[] = new int[size]; try { String sql = "select top " + size + " * from t_mms_interface_submit where flag=0"; ResultSet rs = db.getRs(sql); int i = 0; while (rs.next()) { SendBean send = new SendBean(); send.id = rs.getInt("id"); tmpid[i] = send.id; i++; send.msg_id = rs.getString("msg_id"); send.mobile = rs.getString("mobile"); send.feemobile = rs.getString("feemobile"); send.feetype = rs.getInt("feetype"); send.infofee = rs.getInt("infofee"); send.serviceid = rs.getString("serviceid"); send.subject = rs.getString("subject"); send.infoFile = rs.getString("infoFile"); list.addLast(send); } } catch (Exception ex) { LogManage.ins().logger.severe("get4db : " + ex.getMessage()); } finally { for (int j = 0; j < tmpid.length; j++) { if (tmpid[j] > 0) { updateFlag(tmpid[j], 1); } } } } private void sendMMS() { try { while (list.size() > 0) { SendBean send = (SendBean) list.removeFirst(); MM7SubmitReq submit = new MM7SubmitReq(); submit.setTransactionID(String.valueOf(send.id)); submit.addTo(send.mobile); submit.setVASID(SysConfig.getInstance().hashmap.get("spId"). toString()); submit.setVASPID(SysConfig.getInstance().hashmap.get("spCode"). toString()); submit.setServiceCode(send.serviceid); submit.setSenderAddress(send.src_sp); if (send.subject == null) { send.subject = "MMS"; } submit.setSubject(send.subject); if (send.feemobile == null) { send.feemobile = send.mobile; } submit.setChargedPartyID(send.feemobile); submit.setChargedParty((byte) send.feetype); submit.setDeliveryReport(true); submit.setReadReply(true); submit.setLinkedID(send.link_id); boolean hasContent = false; if (send.infoFile != null) { ArrayList array = this.getSendFileList(send.infoFile); if (!array.isEmpty()) { hasContent = true; MMContent content = new MMContent(); content.setContentType(MMConstants.ContentType. MULTIPART_MIXED); content.setContentID("MM7"); for (int i = 0; i < array.size(); i++) { String filename = (String) array.get(i); MMContent sub1 = MMContent.createFromFile( filename); MMContentType contentType = MMConstants.ContentType. TEXT; if (filename.endsWith(".smil")) { content.setContentType(MMConstants.ContentType. MULTIPART_RELATED); contentType = MMConstants.ContentType.SMIL; } else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) { contentType = MMConstants.ContentType.JPEG; } else if (filename.endsWith(".gif")) { contentType = MMConstants.ContentType.GIF; } else if (filename.endsWith(".midi") || filename.endsWith(".mid")) { contentType = MMConstants.ContentType.MIDI; } else if (filename.endsWith(".amr")) { contentType = MMConstants.ContentType.AMR; } else if (filename.endsWith(".png")) { contentType = MMConstants.ContentType.PNG; } else if (filename.endsWith(".wbmp")) { contentType = MMConstants.ContentType.WBMP; } sub1.setContentType(contentType); sub1.setContentID(this.getFileName(filename)); content.addSubContent(sub1); } submit.setContent(content); } } if (!hasContent) { MMContent content = new MMContent(); content.setContentType(MMConstants.ContentType.TEXT); content.setContentID("MMS"); byte[] bb = null; bb = send.subject.getBytes(); InputStream input = new ByteArrayInputStream(bb); MMContent sub2 = MMContent.createFromStream(input); sub2.setContentID("main.txt"); sub2.setContentType(MMConstants.ContentType.TEXT); content.addSubContent(sub2); submit.setContent(content); } MM7Sender mm7Sender = new MM7Sender(MmsMaster.config); MM7RSRes rsRes; rsRes = (MM7RSRes) mm7Sender.send(submit); if (rsRes instanceof MM7SubmitRes) { MM7SubmitRes submitRes = (MM7SubmitRes) rsRes; send.msg_id = submitRes.getMessageID(); send.submit_rsp = String.valueOf(rsRes.getStatusCode()); //System.out.println(rsRes.getTransactionID()); LogManage.ins().logger.logp(Level.INFO, " |Submit| ", " ", " " + submit.getTo() + " : " + submit.getSubject() + " : " + rsRes.getStatusCode()); } else { send.msg_id = errSubmit; LogManage.ins().logger.logp(Level.INFO, " |Submit| ", " ", " " + submit.getTo() + " : " + submit.getSubject() + " : 不正确消息! " + rsRes.getStatusCode()); } saveSend(send); } } catch (Exception ex) { LogManage.ins().logger.severe("sendMMS : " + ex.getMessage()); } } private String getFileName(String path) { int i = path.lastIndexOf(File.separator); return path.substring(i + 1); } private ArrayList getSendFileList(String path) { ArrayList array = new ArrayList(); File file = new File(path); if (file.exists()) { if (file.isFile()) { //是文件 array.add(path); } else { //是目录 File[] allfile = file.listFiles(); for (int i = 0; i < allfile.length; i++) { File tmp = allfile[i]; if (tmp.isFile()) { String name = tmp.getName().toLowerCase(); if (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".gif") || name.endsWith(".midi") || name.endsWith(".mid") || name.endsWith(".amr") // || name.endsWith(".bmp") || name.endsWith(".txt") || name.endsWith(".xml") || name.endsWith(".smil") || name.endsWith(".png") || name.endsWith(".wbmp")) { array.add(tmp.getAbsolutePath()); } } } } } return array; } private void updateFlag(int id, int flag) { try { db.exeSql("update t_mms_interface_submit set flag = " + flag + " where id = " + id); } catch (Exception ex) { LogManage.ins().logger.severe("updateFlag : " + ex.getMessage()); } } private void saveSend(SendBean send) { try { String sql = "delete t_mms_interface_submit where id = " + send.id; db.exeSql(sql); sql = "INSERT INTO t_mms_submit (msg_id ,mobile ,feemobile ,feetype ,infofee ,serviceid ,subject ,infoFile ,src_sp ,link_id ,submit_rsp) VALUES('" + send.msg_id + "','" + send.mobile + "','" + send.feemobile + "','" + send.feetype + "','" + send.infofee + "','" + send.serviceid + "','" + send.subject + "','" + send.infoFile + "','" + send.src_sp + "','" + send.link_id + "','" + send.submit_rsp + "')"; db.exeSql(sql); } catch (Exception ex) { LogManage.ins().logger.severe("saveSend : " + ex.getMessage()); } } // private String getspid(){ // // } }