java modem 短信发送

前一段时间公司用到短信modem发送信息,我通过参考网络资料,写了一个完整的例子供大家一块学习。代码
如下:

package cn.thunderbird.lm.media.util; 
import java.io.*; 
import java.util.Enumeration; 

import org.smslib.helper.CommPortIdentifier; 
import org.smslib.helper.SerialPort; 
public class ATCommandUtil { 
private String ATTEST = "AT\r"; 
private String ATSMSSET = "AT+CMGF=1\r"; 
private int COMMANDDELAYTIME = 500; //命令间隔延迟时间ms 
private int SMSTIME = 5000; //发送短信延迟时间ms 
boolean rs; 
public boolean sendMsg(String phoneNumber,String content,String portNO,int brandate){ 
String ATMSG ="AT+CMGS=\""+phoneNumber+"\"\r"; 
CommPortIdentifier portid=CommPortIdentifier.getPortIdentifier(portNO); 
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); 
SerialPort com = null; 
if(portid.getPortType()==CommPortIdentifier.PORT_SERIAL){//判断是否为串口 
if(portid.getName().equals(portNO)){//获取指定串口号 
try{ 
com = portid.open("ATCommand", 2000); 
println("COM OK!"); 
int c; 
String response = ""; 
com.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN); 
com.setSerialPortParams(brandate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);//串口设置 
InputStream in = com.getInputStream(); 
OutputStream out = com.getOutputStream(); 
com.enableReceiveTimeout(1000); 
out.write(ATTEST.getBytes());//测试AT指令 
Thread.sleep(COMMANDDELAYTIME); 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 
if(response.indexOf("OK")>=0){//返回OK,代表AT指令正常 
print(new String(response.getBytes("iso-8859-1"),"GBK")); 
println("AT指令正常!\n"); 
} else{ 
println("AT指令出现异常,请检查端口号和波特率是否匹配!"); 
return false; 
} 
out.write(ATSMSSET.getBytes());//设置短信发送模式 
Thread.sleep(COMMANDDELAYTIME); 
response = ""; 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 

if(response.indexOf("OK")>=0){ 
print(response); 
println("短信设置TEXT模式成功!\n"); 
} else{ 
println("短信设置TEXT模式失败!"); 
return false; 
} 
out.write(ATMSG.getBytes());//发送指令AT+CMGS 
Thread.sleep(COMMANDDELAYTIME); 
response = ""; 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 
if(response.indexOf(">")>=0){ 
println(response); 
println("准备发送短信:"); 
println("短信内容:"+content); 
}else{ 
println("发送短信失败!"); 
return false; 
} 
out.write(content.getBytes());//发送短信内容 
println("发送短信..."); 
Thread.sleep(100); 
out.write(26);//发送CTRL+Z 
Thread.sleep(SMSTIME); 
response = ""; 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 
if(response.indexOf("OK")>=0){ 
println("短信发送成功!"); 
}else{ 
println("发送短信失败!"); 
return false; 
} 
}catch(Exception e){ 
println("端口号或者波特率不一致!"); 
println(e.getCause().toString()); 
return false; 
}finally{ 
if(com!=null){ 
com.close();//关闭端口 
println("COM is closed!"); 
} 
} 
}else{ 
return false; 
} 
}else{ 
return false; 
} 
return true; 
} 
public static void print(String msg){ 
System.out.print(msg); 
} 
public static void println(String msg){ 
System.out.println(msg); 
} 

 配置信息如下:

comm.jarwin32com.dll文件复制到JDK下面的jre/lib/ext.中, javax.comm.properties文件复制到JDK下面的jre/lib中。(文件都在短信modem所需文件配置.rar

资源下载:

 

你可能感兴趣的:(java modem 短信发送)