md5加密结合json使用异步提交iframe

 

jsp页面

 

[javascript][/javascript] view plaincopyprint?

  1. <%@ page language=”java” contentType=”text/html; charset=UTF-8″
  2.     pageEncoding=”UTF-8″%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
  7.     <title>API上传例子</title>
  8.   <script src=”http://code.jquery.com/jquery-1.10.1.min.js”></script>
  9.   <script src=”http://code.jquery.com/jquery-migrate-1.2.1.min.js”></script>
  10. </head>
  11. <script type=”text/javascript”>
  12. function doFileUpload(){
  13.     form = document.getElementById(“create_video_sample”);
  14.     buildJSONRequest(form);
  15.     form.action = document.getElementById(“yourAPILocation”).value;
  16.     form.submit();
  17. }
  18. function buildJSONRequest(form){
  19.         json = form.JSONRPC
  20.         //Construct the JSON request:
  21.         json.value = ‘{“writetoken”: “‘+ document.getElementById(“yourWriteToken”).value + ‘”,”title”: “‘+ document.getElementById(“name”).value + ‘”,”describ”:”‘+ document.getElementById(“describ”).value + ‘”,”tag”:”‘+ document.getElementById(“shortDescription”).value + ‘”,”Filedata.filename”:”‘+ document.getElementById(“Filedata”).value + ‘”}’;
  22.         form.JSONView.value = json.value;
  23.         form.writetoken.value=document.getElementById(“yourWriteToken”).value;
  24.         form.title.value=document.getElementById(“name”).value;
  25.         form.describ.value=document.getElementById(“describ”).value;
  26.         form.tag.value=document.getElementById(“shortDescription”).value;
  27.         form.filename.value=document.getElementById(“Filedata”).value;
  28. }
  29. </script>
  30. <div style=”float:left; width:450px;”>
  31. <div style=”font-weight:bold;font-size:24″>API上传示例</div> <br/>
  32. <table style=”width:50%”>
  33. <tr><td style=”width:20%”>Write Token: </td><td style=”width:100%”><input id=”yourWriteToken” size=”55″ value=”Le1haiIrp60WacW-s5Hg60RTtFkbTf9q”/><br/>
  34. </td></tr>
  35. <tr><td>API Location: </td><td><input id=”yourAPILocation” size=”55″ value=”http://fgdf/uc/services/rest?&method=upload” /></td></tr>
  36. <tr><td>标题: </td><td><input type=”text” id=”name” name=”title2″ size=”55″ /></td>
  37. <tr><td>标签: </td><td><input type=”text” id=”shortDescription” size=”55″ /></td></tr>
  38. <tr><td>描述: </td><td><input id=”describ” size=”55″ type=”text”/></td></tr>
  39. <br/> <br/>
  40. <form id=”create_video_sample” method=”POST” ENCTYPE=”multipart/form-data” target=”postFrame”>
  41.     <!–This is where the actual json request lives, it has to come before the file input –>
  42.     <input type=”hidden” name=”JSONRPC” />
  43.     <input type=”hidden” name=”writetoken” />
  44.     <input type=”hidden” name=”title” />
  45.     <input type=”hidden” name=”describ” />
  46.     <input type=”hidden” name=”tag” />
  47.     <input type=”hidden” name=”filename” />
  48.     <input type=”hidden” id=”time” name=”time”/>
  49.     <input type=”hidden” id=”clientHash” name=”clientHash”/>
  50.   <tr><td>File:</td><td><input type=”file” name=”Filedata” id=”Filedata”/></td></tr>
  51. </table><br/><br/>
  52.     <button type=”submit” id=”md5bt”>MD5加密</button>
  53.     <br/>
  54.     <button id=”submitButton” onClick=”doFileUpload()”>Upload</button> <br/>
  55.     <!–display the json request –>
  56.     <fieldset>
  57.     <legend>JSON Request</legend>
  58.         <input name=”JSONView” id=”JSONView” style=”width:100%;border:none”/>
  59.     </fieldset><br/>
  60. </form>
  61. <br/>
  62. <fieldset>
  63. <legend>Response:</legend>
  64.     <iframe id=”postFrame” name=”postFrame” style=”width:100%;border:none”></iframe>
  65. </fieldset>
  66. <br/><br/>
  67. </div>
  68. </html>
  69. <script type=”text/javascript”>
  70. <!–
  71.  $(“#md5bt”).click(function(){
  72.      //alert(“sisisi”);
  73.      $.post(“clientMD5/work”,
  74.              {writetoken:$(“#yourWriteToken”).val(),
  75.               title:$(“#name”).val(),
  76.               label:$(“#shortDescription”).val(),
  77.               describ:$(“#describ”).val()
  78.              },function(data,statusText){
  79.                  //alert(data);
  80.                  //var jsonObj=data;//返回JSON数据
  81.                  var jsonObj=eval(“(“+data+”)”);
  82.                   for(var i=0;i<jsonObj.length;i++){
  83.                      $(“#clientHash”).attr(“value”,jsonObj[i].clientHash);
  84.                      $(“#time”).attr(“value”,jsonObj[i].clientTime);
  85.                   }
  86.              });
  87.  });
  88. //–>
  89. </script>

 

Java类:

 

[java][/java] view plaincopyprint?

  1. package com.app.controller;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.sql.Date;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import net.sf.json.JSONArray;
  11. import net.sf.json.JSONObject;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.client.HttpServerErrorException;
  16. import org.springframework.web.servlet.ModelAndView;
  17. import com.app.tools.MD5Tool;
  18. /**
  19.  *
  20.  * @author Qixuan.Chen
  21.  */
  22. @Controller
  23. public class VideoController {
  24.     ////客户端加密处理
  25.     @RequestMapping(value=”clientMD5/work”,method=RequestMethod.POST)
  26.     public ModelAndView md5encrypt(HttpServletRequest request,HttpServletResponse response) throws Exception{
  27.         //ModelAndView mav=new ModelAndView();
  28.         response.setContentType(“text/html,charset=UTF-8”);
  29.         PrintWriter out=response.getWriter();
  30.        String writetoken= request.getParameter(“writetoken”);
  31.        String title= request.getParameter(“title”);
  32.        String label= request.getParameter(“label”);
  33.        String depicte= request.getParameter(“describ”);
  34.        java.util.Date date=new java.util.Date();
  35.        SimpleDateFormat sdf=new SimpleDateFormat(“yyyyMMddhhmmss”);
  36.        String clientTime= sdf.format(date);
  37.        String secretkey= “HN098fDVJN”;
  38.        String result=writetoken+title+depicte+label+clientTime+secretkey;
  39.        MD5Tool md5=new MD5Tool();
  40.        String clientHash=md5.MD5Encrypt(result);
  41.        //mav.addObject(“clientHash”, clientHash);
  42.        //mav.addObject(“clientTime”, clientTime);
  43.        System.out.println(“===writetoken=====”+writetoken);
  44.        System.out.println(“====title====”+title);
  45.        System.out.println(“====label====”+label);
  46.        System.out.println(“====secretkey====”+secretkey);
  47.        System.out.println(“====clientTime=====”+clientTime);
  48.        System.out.println(“========”+clientHash);
  49.        JSONObject json = new JSONObject();
  50.        JSONArray array = new JSONArray();
  51.        JSONObject member = null;
  52.        //for (int i = 0; i < 5; i++)
  53.        //{
  54.            member = new JSONObject();
  55.            member.put(“clientTime”, clientTime);
  56.            member.put(“clientHash”, clientHash);
  57.            array.add(member);
  58.        //}
  59.        //json.put(“mydata”,array);
  60.        out.print(array.toString());
  61.        out.close();
  62.        System.out.println(“======”+member.toString());
  63.        //out.print(clientTime);
  64.        //out.print(clientHash);
  65.         return null;
  66.     }
  67. }

加密类:

 

 

[java][/java] view plaincopyprint?

  1. package com.app.tools;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. public class MD5Tool {
  5.   //该方法将你输入的字符串,通过md5加密,返回一个加密後的字符串
  6.     public static String MD5Encrypt(String inStr) {
  7.         MessageDigest md = null;
  8.         String outStr = null;
  9.         try {
  10.          md = MessageDigest.getInstance(“MD5”);         //可以选中其他的算法如SHA
  11.          byte[] digest = md.digest(inStr.getBytes());
  12.          //返回的是byet[],要转化为String存储比较方便
  13.          outStr = bytetoString(digest);
  14.         }
  15.         catch (NoSuchAlgorithmException nsae) {
  16.          nsae.printStackTrace();
  17.         }
  18.         return outStr;
  19.     }
  20.     public static String bytetoString(byte[] digest) {
  21.         String str = “”;
  22.         String tempStr = “”;
  23.         for (int i = 1; i < digest.length; i++) {
  24.          tempStr = (Integer.toHexString(digest[i] & 0xff));
  25.          if (tempStr.length() == 1) {
  26.           str = str + “0” + tempStr;
  27.          }
  28.          else {
  29.           str = str + tempStr;
  30.          }
  31.         }
  32.         return str.toLowerCase();
  33.     }
  34. }

标签