import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import com.huateng.common.util.Constant;
/**
* 功能: 短信发送
* 创建人:
* 创建时间: 2012-7-24 上午11:18:11
*/
public class MessageSender {
/**
*
* 功能: 发送短信(手机号列表(最多100个,最好小于100,例如80),短信内容)
* 时间: 2012-7-24 上午11:18:33
*/
public static boolean sendMessage(List<String> cellNos,String content) throws Exception{
//获得短信前置IP和端口
String server_host = Constant.SMS_REMOTE_ADDR;
String server_port = Constant.SMS_REMOTE_PORT;
Socket socket = new Socket(server_host, Integer.parseInt(server_port));
//组装发送报文
StringBuffer realContent = new StringBuffer();
for(String cellNo : cellNos){
realContent.append(cellNo).append(",");
}
realContent.deleteCharAt(realContent.length()-1);
realContent.append(";").append(content);
OutputStream out = socket.getOutputStream();
byte[] bcontent = realContent.toString().getBytes("GBK");
int length = bcontent.length;
byte[] len = new byte[4];
for (int i = 0; i < 4; i++) {
int offset = (len.length - 1 - i) * 8;
len[i] = (byte) ((length >>> offset) & 0xFF);
}
byte[] total = new byte[len.length+bcontent.length];
System.arraycopy(len, 0, total, 0, len.length);
System.arraycopy(bcontent, 0, total, len.length, bcontent.length);
//发送
out.write(total);
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"GBK"));
StringBuffer buffer = new StringBuffer();
String line = null;
while((line = br.readLine()) != null){
buffer.append(line);
}
out.close();
in.close();
socket.close();
//结果为00010即为发送成功0001是长度,0表示成功
return "00010".equals(buffer.toString());
}
public static void main(String[] args) throws Exception{
List<String> cellNos = new ArrayList<String>();
cellNos.add("13585665400");
String content = "该短信为系统发送,请勿回复!";
sendMessage(cellNos, content);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 功能: 短信controller
* 创建人:
* 创建时间: 2012-7-24 上午11:27:38
*/
@Controller
@RequestMapping("/message")
public class MessageController {
/**
*
* 功能: 群发短信
* 时间: 2012-7-25 下午01:00:20
*/
@RequestMapping(value="/messageSend",method=RequestMethod.POST)
@ResponseBody
public boolean messageSend(@RequestParam("message_user") String[] message_user
,@RequestParam("message_content") String message_content) throws Exception{
Map<String, String> map = new HashMap<String, String>();
boolean success = true;
try {
int max_num = 80;//群发一次最多80个号码(多余80的分多次发)
List<String> cellNos = new ArrayList<String>();
for(String user : message_user){
cellNos.add(user);
if(cellNos.size() == max_num){
boolean result = MessageSender.sendMessage(cellNos, message_content);
System.out.println("inside result: "+result);
success = success&&result;
cellNos.clear();
}
}
boolean result = MessageSender.sendMessage(cellNos, message_content);
System.out.println("outside result: "+result);
success = success&&result;
map.put("mes", success?"发送成功":"发送失败");
} catch (Exception e) {
e.printStackTrace();
map.put("mes", "发送失败");
}
return success;
}
}