发送短信-java

来源:互联网 发布:淘宝win10激活码重装 编辑:程序博客网 时间:2024/06/07 21:05
package com.njxdqx.framework.modules.sys.service;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * <p>
 * <date>2012-03-01</date><br/>
 * <span>软维提供的JAVA接口信息(短信,彩信)调用API</span><br/>
 * <span>----------基础访问方法-------------</span>
 * </p>
 * 
 * @author LIP
 * @version 1.0.1
 */
public class SmsClientAccessTool {


private static SmsClientAccessTool smsClientToolInstance;


/**
* 采用单列方式来访问操作

* @return
*/
public static synchronized SmsClientAccessTool getInstance() {


if (smsClientToolInstance == null) {
smsClientToolInstance = new SmsClientAccessTool();
}
return smsClientToolInstance;
}


/**
* <p>
* POST方法
* </p>

* @param sendUrl
*            :访问URL
* @param paramStr
*            :参数串
* @param backEncodType
*            :返回的编码
* @return
*/
public String doAccessHTTPPost(String sendUrl, String sendParam,
String backEncodType) {


StringBuffer receive = new StringBuffer();
BufferedWriter wr = null;
try {
if (backEncodType == null || backEncodType.equals("")) {
backEncodType = "UTF-8";
}


URL url = new URL(sendUrl);
HttpURLConnection URLConn = (HttpURLConnection) url
.openConnection();


URLConn.setDoOutput(true);
URLConn.setDoInput(true);
((HttpURLConnection) URLConn).setRequestMethod("POST");
URLConn.setUseCaches(false);
URLConn.setAllowUserInteraction(true);
HttpURLConnection.setFollowRedirects(true);
URLConn.setInstanceFollowRedirects(true);


URLConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
URLConn.setRequestProperty("Content-Length", String
.valueOf(sendParam.getBytes().length));


DataOutputStream dos = new DataOutputStream(URLConn
.getOutputStream());
dos.writeBytes(sendParam);


BufferedReader rd = new BufferedReader(new InputStreamReader(
URLConn.getInputStream(), backEncodType));
String line;
while ((line = rd.readLine()) != null) {
receive.append(line).append("\r\n");
}
rd.close();
} catch (java.io.IOException e) {
receive.append("访问产生了异常-->").append(e.getMessage());
e.printStackTrace();
} finally {
if (wr != null) {
try {
wr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
wr = null;
}
}


return receive.toString();
}


public String doAccessHTTPGet(String sendUrl, String backEncodType) {


StringBuffer receive = new StringBuffer();
BufferedReader in = null;
try {
if (backEncodType == null || backEncodType.equals("")) {
backEncodType = "UTF-8";
}


URL url = new URL(sendUrl);
HttpURLConnection URLConn = (HttpURLConnection) url
.openConnection();


URLConn.setDoInput(true);
URLConn.setDoOutput(true);
URLConn.connect();
URLConn.getOutputStream().flush();
in = new BufferedReader(new InputStreamReader(URLConn
.getInputStream(), backEncodType));


String line;
while ((line = in.readLine()) != null) {
receive.append(line).append("\r\n");
}


} catch (IOException e) {
receive.append("访问产生了异常-->").append(e.getMessage());
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (java.io.IOException ex) {
ex.printStackTrace();
}
in = null;


}
}


return receive.toString();
}

}



package com.njxdqx.framework.modules.sys.service;


import java.net.URLEncoder;


/**
 * <p>
 * <date>2012-03-01</date><br/>
 * <span>软维提供的JAVA接口信息(短信,彩信)调用API</span><br/>
 * <span>----------发送短信-------------</span>
 * </p>
 * 
 * @author LIP
 * @version 1.0.1
 */
public class SmsClientSend {


/**
* <p>
* <date>2012-03-01</date><br/>
* <span>发送信息方法1--必须传入必填内容</span><br/>
* <p>
* 其一:发送方式,默认为POST<br/>
* 其二:发送内容编码方式,默认为UTF-8
* </p>
* <br/>
* </p>

* @param url
*            :必填--发送连接地址URL--比如>http://118.145.30.35/sms.aspx
* @param userid
*            :必填--用户ID,为数字
* @param account
*            :必填--用户帐号
* @param password
*            :必填--用户密码
* @param mobile
*            :必填--发送的手机号码,多个可以用逗号隔比如>13512345678,13612345678
* @param content
*            :必填--实际发送内容,
* @return 返回发送信息之后返回字符串
*/
public static String sendSms(String url, String userid, String account,
String password, String mobile, String content) {


return sendSms(url, userid, account, password, mobile, content, null,
null, null, null, null, null, null, "POST", "UTF-8", "UTF-8");
}


/**
* <p>
* <date>2012-03-01</date><br/>
* <span>发送信息方法--暂时私有化,这里仅仅是提供用户接口而已。其实用不了那么复杂</span><br/>
* <span>发送信息最终的组合形如:http://118.145.30.35/sms.aspx?action=send</span>
* </p>

* @param url
*            :必填--发送连接地址URL--比如>http://118.145.30.35/sms.aspx

* @param userid
*            :必填--用户ID,为数字
* @param account
*            :必填--用户帐号
* @param password
*            :必填--用户密码
* @param mobile
*            :必填--发送的手机号码,多个可以用逗号隔比如>13512345678,13612345678
* @param content
*            :必填--实际发送内容,
* @param action
*            :选填--访问的事件,默认为send
* @param sendTime
*            :选填--定时发送时间,不填则为立即发送,时间格式如>2011-11-11 11:11:11
* @param checkContent
*            :选填--检查是否包含非法关键字,1--表示需要检查,0--表示不检查
* @param taskName
*            :选填--任务名称,本次任务描述,100字内
* @param countNumber
*            :选填--提交号码总数
* @param mobileNumber
*            :选填--手机号码总数
* @param telephoneNumber
*            :选填--小灵通(和)或座机总数
* @param sendType
*            :选填--发送方式,默认为POST
* @param codingType
*            :选填--发送内容编码方式,默认为UTF-8
* @param backEncodType
*            :选填--返回内容编码方式,默认为UTF-8
* @return 返回发送之后收到的信息
*/
private static String sendSms(String url, String userid, String account,
String password, String mobile, String content, String action,
String sendTime, String checkContent, String taskName,
String countNumber, String mobileNumber, String telephoneNumber,
String sendType, String codingType, String backEncodType) {


try {
if (codingType == null || codingType.equals("")) {
codingType = "UTF-8";
}
if (backEncodType == null || backEncodType.equals("")) {
backEncodType = "UTF-8";
}
StringBuffer send = new StringBuffer();
if (action != null && !action.equals("")) {
send.append("action=").append(action);
} else {
send.append("action=send");
}


send.append("&userid=").append(userid);
send.append("&account=").append(
URLEncoder.encode(account, codingType));
send.append("&password=").append(
URLEncoder.encode(password, codingType));
send.append("&mobile=").append(mobile);
send.append("&content=").append(
URLEncoder.encode(content, codingType));
if (sendTime != null && !sendTime.equals("")) {
send.append("&sendTime=").append(
URLEncoder.encode(sendTime, codingType));
}
if (checkContent != null && !checkContent.equals("")) {
send.append("&checkContent=").append(checkContent);
}
if (taskName != null && !taskName.equals("")) {
send.append("&taskName=").append(
URLEncoder.encode(taskName, codingType));
}
if (countNumber != null && !countNumber.equals("")) {
send.append("&countNumber=").append(countNumber);
}
if (mobileNumber != null && !mobileNumber.equals("")) {
send.append("&mobileNumber=").append(mobileNumber);
}
if (telephoneNumber != null && !telephoneNumber.equals("")) {
send.append("&telephoneNumber=").append(telephoneNumber);
}


if (sendType != null && (sendType.toLowerCase()).equals("get")) {
return SmsClientAccessTool.getInstance().doAccessHTTPGet(
url + "?" + send.toString(), backEncodType);
} else {
return SmsClientAccessTool.getInstance().doAccessHTTPPost(url,
send.toString(), backEncodType);
}
} catch (Exception e) {
e.printStackTrace();
return "未发送,编码异常";
}
}


}



//发送短信
 double money = chiefUser.getMonery();
String userName=chiefUser.getUserName();
loginName=chiefUser.getLoginName();
String phone=chiefUser.getPhone();
 String message="尊敬的"+userName+"您好,您已成功开设账户,登录名:"+loginName+",登录密码:"+newPassword+",账户余额:"+money+"元。";
System.out.println(message);
SmsClientSend scs = new SmsClientSend();
String returnStr=scs.sendSms("http://www.lcqxt.com/sms.aspx", "24051", "wanneng", "wanneng", phone, message);