java信息费_java 免费发送手机短信功能 | 学步园

天天打酱油,闲来无事把现有项目上的一些对以后开发有用的实例进行提取!

package com;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLEncoder;

import java.util.ResourceBundle;

import javax.servlet.ServletException;

/*

* 短信发布

*/

public class SendNotice {

/**

* 短信发布

* @param notice

* @return

*/

public static int sendNotice(Sms sms) throws ServletException, IOException {

URL url = null;

HttpURLConnection connection = null;

// InputStream inStream = SendNotice.class.getClassLoader().getResourceAsStream("notice.properties");

ResourceBundle sysSetting = ResourceBundle.getBundle("com/notice");

try {

url = new URL(sysSetting.getString("haier-sms-url"));

connection = (HttpURLConnection) url.openConnection();//新建连接实例

connection.setDoOutput(true);//是否打开输出流 true|false

connection.setDoInput(true);//是否打开输入流true|false

connection.setRequestMethod("POST");//提交方法POST|GET

connection.setUseCaches(false);//是否缓存true|false

connection.connect();//打开连接端口

DataOutputStream out = new DataOutputStream(connection.getOutputStream());//打开输出流往对端服务器写数据

String haierPhone = sysSetting.getString("haier-sms-phone");

String haierMSG = sysSetting.getString("haier-sms-msg");

String haierClass = sysSetting.getString("haier-sms-class");

String haierDep = sysSetting.getString("haier-sms-dep");

String content = haierPhone + "=" + sms.getMobile() + "&" + haierMSG + "=" + URLEncoder.encode(sms.getMsg(), "GB2312") + "&" + haierClass + "=" +haierDep;

out.writeBytes(content);//写数据

out.flush();//刷新

out.close();//关闭输出流

BufferedReader reader = new BufferedReader(new InputStreamReader(connection

.getInputStream(), "utf-8"));

StringBuffer buffer = new StringBuffer();

String line = "";

while ((line = reader.readLine()) != null) {

buffer.append(line);

}

reader.close();

System.out.println("phoneBackId="+buffer.toString());

if ("0".equals(buffer.toString())) {

return 0;

} else {

return 1;

}

} catch (IOException e) {

e.printStackTrace();

return 1;

} finally {

if (connection != null) {

connection.disconnect();//关闭连接

}

}

}

/**

* @param args

* @throws IOException

* @throws ServletException

*/

public static void main(String[] args) throws ServletException, IOException {

Sms sms = new Sms();

sms.setMobile("139174***94");

sms.setMsg("黄海1");

System.out.println(SendNotice.sendNotice(sms));

}

}

package com;

public class Sms {

/**

* 电话号码

*/

private String mobile;

/**

* 短信内容

*/

private String msg;

/**

* 客户ID

*/

private Integer customerId;

private String sendTime;

public String getSendTime() {

return sendTime;

}

public void setSendTime(String sendTime) {

this.sendTime = sendTime;

}

public Integer getCustomerId() {

return customerId;

}

public void setCustomerId(Integer customerId) {

this.customerId = customerId;

}

public String getMobile() {

return mobile;

}

public void setMobile(String mobile) {

this.mobile = mobile;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

@Override

public String toString() {

return "Sms [mobile=" + mobile + ", msg=" + msg + ", customerId=" + customerId + ", sendTime=" + sendTime + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = super.hashCode();

result = prime * result + ((customerId == null) ? 0 : customerId.hashCode());

result = prime * result + ((mobile == null) ? 0 : mobile.hashCode());

result = prime * result + ((msg == null) ? 0 : msg.hashCode());

result = prime * result + ((sendTime == null) ? 0 : sendTime.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (!super.equals(obj))

return false;

if (getClass() != obj.getClass())

return false;

Sms other = (Sms) obj;

if (customerId == null) {

if (other.customerId != null)

return false;

} else if (!customerId.equals(other.customerId))

return false;

if (mobile == null) {

if (other.mobile != null)

return false;

} else if (!mobile.equals(other.mobile))

return false;

if (msg == null) {

if (other.msg != null)

return false;

} else if (!msg.equals(other.msg))

return false;

if (sendTime == null) {

if (other.sendTime != null)

return false;

} else if (!sendTime.equals(other.sendTime))

return false;

return true;

}

}

notice.properties属性文件,当然怎么读取可以自己写,或者自己写字类中

# NOTICE

haier-sms-url = ********

haier-sms-phone = phone

haier-sms-msg = msg

haier-sms-class = class

haier-sms-dep = HSW

亲,为避免骚扰他人!决定将URL不显示,如若需要,请留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值