JAVA-Mobile scan two-dimensional code, page response (not scan code login)
I have just been a few months of Meng New, these months, encountered a lot of no contact with the function, although the code is very simple, but also for a while let me worry, thinking impassability.
In order not to let oneself learn a little new things forget, so, began to write my first blog. I hope that the people who help to the need, can also let me keep these memories.
Put the picture below. is a function of my needs, I am in the company's OA project group.
I'll pull this feature out and make a simple demo.
the function needs: 1, scan code verification login; 2, if refresh the page, skip scan code, directly to the "query page"
This feature is in the form of polling. It should be called polling (first contact, not clear). It is said that the code is simple, but it is more cost-performance, if the use of fewer people, is OK, but many people are not very good.
Fortunately, this feature is used for specific groups of people.
************ based on ssm*****************
If you need to change to a servlet, just write each method in a separate servlet
on the Code
Code required download address for 2 jar packages: http://download.csdn.net/detail/cj_zyz/9821271
Certificationcontroller.java
Package Com.ssm.controller;
Import Java.io.File;
Import java.io.IOException;
Import Java.util.HashMap;
Import java.util.Hashtable;
Import Java.util.Map;
Import Java.util.UUID;
Import Javax.servlet.http.HttpServletRequest;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatWriter;
Import com.google.zxing.WriterException;
Import Com.google.zxing.client.j2se.MatrixToImageWriter;
Import Com.google.zxing.common.BitMatrix;
Import Com.ssm.pojo.User; /** * Certification Certification/@Controller @RequestMapping ("/certification") public class Certificationcontroller {/** * Step one: Jump to two-dimensional code page <br> * Access url:http://localhost:8080/project name/certification/qrcode * * @return * * @RequestMapping (value = "QRCode") public String Form2 (HttpServletRequest request) {//each user login, user information will be saved to the session inside,//This session ID is unique, get sessionId//Judge whether has been certified, certified, skipped two-dimensional code page String sessionId = request.getses
Sion (True). GetId ();
String value = User.getsessionidmap (). get (SessionId); if (value = = null) {return "QRCode";//Jump to two-dimensional code page for sweep code} else {return "success";//Skip two-dimensional code page, direct success}}/** * Step two: Generate a two-dimensional code, and return the two-dimensional code picture address * @return/@RequestMapping (value= "Getqrcode") @ResponseBody public map<string, STRING&G T Getqrcode () {int width = 150;//two D code picture width of int height = 150;//height of the String format = "JPG";//Picture format//JDK from
The band UUID is generated by random numbers, with no-split in the middle.
String uuid = Uuid.randomuuid (). toString (). ReplaceAll ("-", "");
Two-dimensional code content, written to the UUID, you can let the phone scan after the UUID to the background String content = "uuid=" + uuid;
Hashtable<encodehinttype, string> hints = new Hashtable<encodehinttype, string> ();
Hints.put (Encodehinttype.character_set, "utf-8");
Bitmatrix Bitmatrix = null; try {//encoding Bitmatrix = new Multiformatwriter (). Encode (content, BarcodeFormat.qr_code, width, height, hints);
}/*catch (Writerexception E1) {}*/catch (Exception E1) {e1.printstacktrace ();
String fileName = "Qrcode.png";
Absolute path, the generated two-dimensional stacking into the C-packing directory under String path = "c:/" + fileName;
Relative path, used to display picture String photo = path;
File outputfile = new file (path);
if (!outputfile.exists () &&!outputfile.isdirectory ()) {outputfile.mkdirs ());
try {//Output two-dimensional code picture Matrixtoimagewriter.writetofile (Bitmatrix, format, outputfile);
catch (IOException e) {e.printstacktrace ();
} map<string, string> Map = new hashmap<> ();
Map.put ("photo", photo);
Map.put ("uuid", uuid);
return map; /** * Step 3:2 Dimension code page Ajax Loop Call this method * @return 1 successful, 0 failure/@RequestMapping (value= "login") @ResponseBody public inte
GER Login (String uuid, httpservletrequest request) {int count = 1;
Boolean result = true; while (true) {try {thread.sleep (1000);/sleep 1 sec} catch (Interruptedexception e) {e.printstacktRace ();
} System.out.println ("UUID for page passing:" + uuid);
Detect login, detect whether the phone has been swept code String value = User.getloginusermap (). get (UUID); if (value!= null) {result = true;//authentication succeeded//To save the authenticated sessionId String sessionId = request.getsession (t
Rue). GetId ();
User.getsessionidmap () put (sessionId, "here casually fill in, just to judge whether it is null only");
Break
else {if (count = = 5) {result = false;//authentication failed, two-dimensional code break not scanned; }}//end If.. Else count++;//Count +1}//end while return result?
1:0; /** * Step four: Mobile phone scan two-dimensional code after,<br> * Call Appscanner interface, and pass the UUID/@RequestMapping (value= "Appscanner") @ResponseBo
Dy public string Appscanner (string uuid) {System.out.println ("UUID delivered by the handset side:" + uuid);
The UUID is deposited in map String value = User.getloginusermap (). get (UUID);
if (value = = null) {User.getloginusermap (). Put (UUID, "random value, the value is not null judgment");
//To return to the mobile phone authentication success message, although not really verified, but told the phone end has been swept code has returned "certification success"; }/** * Empty getsessIonidmap * @return/@RequestMapping (value= "clear") public String Clear () {User.getsessionidmap (). Clear ();
return "Redirect:qrcode";
}
}
index.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%> <%@ taglib prefix="
C "uri=" Http://java.sun.com/jsp/jstl/core " %>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path;
%>
<c:set var= "base" value= "<%=basepath%>"/>
qrcode.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> &L
t;% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path; %> <c:set var= "base" value= "<%=basepath%>"/> <c:set var= "ctx" value= "${" PageContext.request.contextPath} "/>