1. 程式人生 > >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不顯示,如若需要,請留言