天天打酱油,闲来无事把现有项目上的一些对以后开发有用的实例进行提取!
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不显示,如若需要,请留言