WpsUtils.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package cn.com.lzt.message.wps.util;
  2. import cn.com.lzt.message.wps.model.UrlModel;
  3. import org.jeecgframework.core.util.ResourceUtil;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import javax.crypto.Mac;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.URLEncoder;
  9. import java.security.InvalidKeyException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.util.*;
  12. public class WpsUtils {
  13. public static String DOMAIN="https://wwo.wps.cn";
  14. public static Object getapp_Token(String fileId,String fileName) throws UnsupportedEncodingException {
  15. String APPID = ResourceUtil.getConfigByName("wps_app_id");
  16. String APPKEY = ResourceUtil.getConfigByName("wps_app_key");
  17. if (fileName == null || fileName.isEmpty()) {
  18. return null;
  19. }
  20. String url =DOMAIN+ "/office";
  21. int dotIndex = fileName.lastIndexOf('.');
  22. String suffix="";
  23. if(dotIndex!=-1)
  24. suffix = fileName.substring(dotIndex+1);
  25. suffix =suffix.toLowerCase();
  26. if(suffix.equals("xls") || suffix.equals("xlsx")){
  27. url += "/s/"+fileId+"?";
  28. }else if(suffix.equals("pdf")){
  29. url += "/f/"+fileId+"?";
  30. }else if(suffix.equals("ppt")||suffix.equals("pptx")){
  31. url += "/p/"+fileId+"?";
  32. }else {
  33. url += "/w/"+fileId+"?";
  34. }
  35. fileName = URLEncoder.encode(fileName, "UTF-8");
  36. String params = "_w_appid=" + APPID + "&_w_fname=" + fileId;
  37. String signature = getSignature(paramToMap(params),APPKEY);
  38. url += params + "&_w_signature=" + signature;
  39. UrlModel urlModel = new UrlModel();
  40. urlModel.wpsUrl = url;
  41. urlModel.token = "1";
  42. return urlModel;
  43. }
  44. private static String getSignature(Map<String, String> params, String appSecret) {
  45. List<String> keys=new ArrayList();
  46. for (Map.Entry<String, String> entry : params.entrySet()) {
  47. keys.add(entry.getKey());
  48. }
  49. // 将所有参数按key的升序排序
  50. Collections.sort(keys, new Comparator<String>() {
  51. public int compare(String o1, String o2) {
  52. return o1.compareTo(o2);
  53. }
  54. });
  55. // 构造签名的源字符串
  56. StringBuilder contents=new StringBuilder("");
  57. for (String key : keys) {
  58. if (key=="_w_signature"){
  59. continue;
  60. }
  61. contents.append(key+"=").append(params.get(key));
  62. }
  63. contents.append("_w_secretkey=").append(appSecret);
  64. // 进行hmac sha1 签名
  65. byte[] bytes= hmacSha1(appSecret.getBytes(),contents.toString().getBytes());
  66. //字符串经过Base64编码
  67. String sign= Base64.getEncoder().encode(bytes).toString();
  68. try {
  69. sign = URLEncoder.encode( sign, "UTF-8");
  70. } catch (UnsupportedEncodingException e) {
  71. e.printStackTrace();
  72. }
  73. System.out.println(sign);
  74. return sign;
  75. }
  76. public static Map<String, String> paramToMap(String paramStr) {
  77. String[] params = paramStr.split("&");
  78. Map<String, String> resMap = new HashMap<String, String>();
  79. for (int i = 0; i < params.length; i++) {
  80. String[] param = params[i].split("=");
  81. if (param.length >= 2) {
  82. String key = param[0];
  83. String value = param[1];
  84. for (int j = 2; j < param.length; j++) {
  85. value += "=" + param[j];
  86. }
  87. resMap.put(key, value);
  88. }
  89. }
  90. return resMap;
  91. }
  92. public static byte[] hmacSha1(byte[] key, byte[] data) {
  93. try {
  94. SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
  95. Mac mac = Mac.getInstance(signingKey.getAlgorithm());
  96. mac.init(signingKey);
  97. return mac.doFinal(data);
  98. }
  99. catch (NoSuchAlgorithmException e) {
  100. e.printStackTrace();
  101. } catch (InvalidKeyException e) {
  102. e.printStackTrace();
  103. }
  104. return null;
  105. }
  106. }