| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package cn.com.lzt.message.wps.util;
- import cn.com.lzt.message.wps.model.UrlModel;
- import org.jeecgframework.core.util.ResourceUtil;
- import org.springframework.web.bind.annotation.RequestParam;
- import javax.crypto.Mac;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.util.*;
- public class WpsUtils {
- public static String DOMAIN="https://wwo.wps.cn";
- public static Object getapp_Token(String fileId,String fileName) throws UnsupportedEncodingException {
- String APPID = ResourceUtil.getConfigByName("wps_app_id");
- String APPKEY = ResourceUtil.getConfigByName("wps_app_key");
- if (fileName == null || fileName.isEmpty()) {
- return null;
- }
- String url =DOMAIN+ "/office";
- int dotIndex = fileName.lastIndexOf('.');
- String suffix="";
- if(dotIndex!=-1)
- suffix = fileName.substring(dotIndex+1);
- suffix =suffix.toLowerCase();
- if(suffix.equals("xls") || suffix.equals("xlsx")){
- url += "/s/"+fileId+"?";
- }else if(suffix.equals("pdf")){
- url += "/f/"+fileId+"?";
- }else if(suffix.equals("ppt")||suffix.equals("pptx")){
- url += "/p/"+fileId+"?";
- }else {
- url += "/w/"+fileId+"?";
- }
- fileName = URLEncoder.encode(fileName, "UTF-8");
- String params = "_w_appid=" + APPID + "&_w_fname=" + fileId;
- String signature = getSignature(paramToMap(params),APPKEY);
- url += params + "&_w_signature=" + signature;
- UrlModel urlModel = new UrlModel();
- urlModel.wpsUrl = url;
- urlModel.token = "1";
- return urlModel;
- }
- private static String getSignature(Map<String, String> params, String appSecret) {
- List<String> keys=new ArrayList();
- for (Map.Entry<String, String> entry : params.entrySet()) {
- keys.add(entry.getKey());
- }
- // 将所有参数按key的升序排序
- Collections.sort(keys, new Comparator<String>() {
- public int compare(String o1, String o2) {
- return o1.compareTo(o2);
- }
- });
- // 构造签名的源字符串
- StringBuilder contents=new StringBuilder("");
- for (String key : keys) {
- if (key=="_w_signature"){
- continue;
- }
- contents.append(key+"=").append(params.get(key));
- }
- contents.append("_w_secretkey=").append(appSecret);
- // 进行hmac sha1 签名
- byte[] bytes= hmacSha1(appSecret.getBytes(),contents.toString().getBytes());
- //字符串经过Base64编码
- String sign= Base64.getEncoder().encode(bytes).toString();
- try {
- sign = URLEncoder.encode( sign, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- System.out.println(sign);
- return sign;
- }
- public static Map<String, String> paramToMap(String paramStr) {
- String[] params = paramStr.split("&");
- Map<String, String> resMap = new HashMap<String, String>();
- for (int i = 0; i < params.length; i++) {
- String[] param = params[i].split("=");
- if (param.length >= 2) {
- String key = param[0];
- String value = param[1];
- for (int j = 2; j < param.length; j++) {
- value += "=" + param[j];
- }
- resMap.put(key, value);
- }
- }
- return resMap;
- }
- public static byte[] hmacSha1(byte[] key, byte[] data) {
- try {
- SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
- Mac mac = Mac.getInstance(signingKey.getAlgorithm());
- mac.init(signingKey);
- return mac.doFinal(data);
- }
- catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
|