Servlethtml
package com.dongbin.sendphone; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.alibaba.fastjson.JSON; public class SendMailServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private static String Message ="驗證碼發送失敗"; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String phone = request.getParameter("phone"); String codeUniqueId = ""; Random random = new Random(); int numb = random.nextInt(899999)+100000; codeUniqueId=numb+""; String codeContent="本次的驗證碼爲:"+codeUniqueId+",(爲了您的帳戶安全,請勿將驗證碼轉告他人)"; if(checkTimeCode(1, request)){ Phone.sentPhone(phone, codeContent); Message = "驗證碼已經發送,請注意查收"; } Map<String, String> map = new HashMap<String, String>(); map.put("data", Message); HttpSession session = request.getSession(); session.setAttribute("code", codeUniqueId); response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print(JSON.toJSONString(map).toString()); out.flush(); out.close(); } protected boolean checkTimeCode(long seconds,HttpServletRequest request){ Object codeInteceptor = request.getSession().getAttribute("times"); if(codeInteceptor != null){ long currTime = System.currentTimeMillis(); long remainTime = currTime -Long.parseLong(codeInteceptor.toString()); long get_time = seconds*60*1000; if(remainTime < get_time){ Message = "請您"+ (get_time - remainTime)/(1000) + " 秒後,從新獲取驗證碼"; return false; }else{ request.getSession(true).setAttribute("times", System.currentTimeMillis()); return true; } }else{ request.getSession(true).setAttribute("times", System.currentTimeMillis()); return true; } } }
Phone類java
package com.dongbin.sendphone; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; public class Phone { /** * @param phone:手機號碼 * @param content:內容 * @return * @throws UnsupportedEncodingException */ public static String sentPhone(String phone,String content) throws UnsupportedEncodingException{ String username = ""; //發送短信帳戶 String password = ""; //發送短信密碼 String newContent = URLEncoder.encode(content, "UTF-8"); newContent =URLDecoder.decode(content,"UTF-8")+"【Test】"; String url = ""; String result = sendSms(url,username,password,phone,newContent); return result; } /** * 短信發送 * @param url * @param username * @param password * @param mobile * @param content * @return 發送響應結果 */ public static String sendSms(String url, String username, String password, String mobile, String content){ String response = null; PostMethod postMethod = new PostMethod(url); postMethod.getParams().setContentCharset("UTF-8"); postMethod.addParameter("account", username); postMethod.addParameter("password",password); postMethod.addParameter("destmobile", mobile); postMethod.addParameter("sendDateTime",""); postMethod.addParameter("msgText", content); HttpClient httpClient = new HttpClient(); int statusCode=0; try { statusCode=httpClient.executeMethod(postMethod); if(statusCode== HttpStatus.SC_OK){ response = postMethod.getResponseBodyAsString(); }else { System.out.println("=====statusCode===="+statusCode); } } catch (Exception e) { e.printStackTrace(); }finally{ postMethod.releaseConnection(); } System.out.println("響應結果爲:"+response); return response; } public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { } finally { try { is.close(); } catch (IOException e) { } } return sb.toString(); } }
CheckPhoneServlet類apache
package com.dongbin.sendphone; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 檢查短信驗證碼 * @author dongbin * */ public class CheckPhoneServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(false); String codeUniqueId = String.valueOf(session.getAttribute("code")); String code = request.getParameter("code"); String message = "短信驗證碼錯誤"; if(codeUniqueId.equals(code)){ message = "短信驗證碼正確"; } response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>驗證結果:</TITLE></HEAD>"); out.println(" <BODY>"); out.println(message); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }