117 lines
4.0 KiB
Java
117 lines
4.0 KiB
Java
/**
|
||
* 项目名称: 系统名称
|
||
* 包名: com.message
|
||
* 文件名: SendMessage.java
|
||
* 版本信息: 1.0.0
|
||
* 创建日期: 2013-12-2-下午12:29:53
|
||
* Copyright:2013XX公司-版权所有
|
||
**/
|
||
|
||
package com.tenwa.lease.util;
|
||
|
||
import java.net.Inet4Address;
|
||
import java.net.InetAddress;
|
||
import java.net.NetworkInterface;
|
||
import java.net.SocketException;
|
||
import java.util.ArrayList;
|
||
import java.util.Enumeration;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
import jbo.com.tenwa.lease.comm.LB_MESSAGE_CONFIG;
|
||
|
||
import org.apache.commons.httpclient.HttpClient;
|
||
import org.apache.commons.httpclient.HttpStatus;
|
||
import org.apache.commons.httpclient.NameValuePair;
|
||
import org.apache.commons.httpclient.methods.PostMethod;
|
||
|
||
import com.amarsoft.are.jbo.BizObject;
|
||
import com.amarsoft.are.jbo.JBOFactory;
|
||
|
||
public class SendMessageUtil{
|
||
/**
|
||
* @method sendMessageByHttpClient 发送短信到指定号码
|
||
* @param receiverMobilePhone 接收方手机号码
|
||
* @param msg 消息内容
|
||
* @return
|
||
* @throws Exception
|
||
* @returnType String
|
||
* @exception
|
||
* @since 1.0.0
|
||
**/
|
||
public static String sendMessageByHttpClient(String receiverMobilePhone, String msg) throws Exception {
|
||
List<BizObject> messageConfigList = JBOFactory.getBizObjectManager(LB_MESSAGE_CONFIG.CLASS_NAME).createQuery("1=1").getResultList(false);
|
||
BizObject messageConfig = messageConfigList.get(0);
|
||
String allowSendIps = messageConfig.getAttribute("ALLOW_SEND_IPS").getString();
|
||
//判断本机电脑所有配置好的IP是否与短信配置中的符合
|
||
if("N".equals(messageConfig.getAttribute("IS_SEND").getString()) || !getServerIsAllowSendMsg(allowSendIps)){
|
||
System.out.println("短信设置为不发送短信或本机IP未在[允许发送短信的主机IP]之中!");
|
||
return "当前系统IP不在允许发送的IP地址之中";
|
||
}
|
||
String sendMsg = "【安鹏租赁】"+msg;
|
||
//从短信配置中根据对应字段获取要发送的地址和用户名,密码等信息
|
||
String sendPassword = messageConfig.getAttribute("SENDER_PASSWORD_").getString();
|
||
String sendMessageUrl = messageConfig.getAttribute("SEND_MESSAGE_URL_").getString();
|
||
String sendUsername = messageConfig.getAttribute("SENDER_").getString();
|
||
String content = java.net.URLEncoder.encode(sendMsg, "gbk");
|
||
//通过HttpClient发送请求信息
|
||
HttpClient httpClient = new HttpClient();
|
||
String url = sendMessageUrl;
|
||
PostMethod postMethod = new PostMethod(url);
|
||
//发送的参数
|
||
NameValuePair[] data = {
|
||
new NameValuePair("username", sendUsername),
|
||
new NameValuePair("p", receiverMobilePhone),
|
||
new NameValuePair("pwd", sendPassword),
|
||
new NameValuePair("msg", content)
|
||
//new NameValuePair("charSetStr","utf") 如果未来编码模式为utf-8可以设置此参数
|
||
};
|
||
postMethod.setRequestBody(data);
|
||
//返回的状态码
|
||
int statusCode = httpClient.executeMethod(postMethod);
|
||
System.out.println("状态码:"+statusCode);
|
||
String result = "";
|
||
if (statusCode == HttpStatus.SC_OK) {
|
||
result = postMethod.getResponseBodyAsString();
|
||
}else{
|
||
result = statusCode + "";
|
||
}
|
||
postMethod.releaseConnection();
|
||
return result;
|
||
}
|
||
|
||
public static List<String> getServerIpList() throws SocketException{
|
||
List<String> hostAddrList = new ArrayList<String>();
|
||
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
||
InetAddress ip = null;
|
||
while (allNetInterfaces.hasMoreElements()) {
|
||
NetworkInterface netInterface = (NetworkInterface)allNetInterfaces.nextElement();
|
||
Enumeration addresses = netInterface.getInetAddresses();
|
||
while (addresses.hasMoreElements()) {
|
||
ip = (InetAddress)addresses.nextElement();
|
||
if (ip != null && ip instanceof Inet4Address) {
|
||
hostAddrList.add(ip.getHostAddress());
|
||
System.out.println("本机的IP = " + ip.getHostAddress());
|
||
}
|
||
}
|
||
}
|
||
return hostAddrList;
|
||
}
|
||
|
||
public static boolean getServerIsAllowSendMsg(String allowIps) throws SocketException {
|
||
boolean flag = false;
|
||
List<String> hostAddrList = getServerIpList();
|
||
if(hostAddrList.size() > 0){
|
||
for(String ipStr : hostAddrList){
|
||
if(allowIps.indexOf(ipStr) != -1){
|
||
flag = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
}
|