需要到依賴包
commons-codec-1.3.jar
/**
* 獲取簽名簽名字符串* @param URL 訪問的URL
* @param data josn格式的參數數據
* @param timestamp 時間戳
* @param methodName 方法名
* @return 簽名結果
*/
public static String getSign(String URL, String data ,String timestamp ,String methodName){
String params = "";
params = URL+data+timestamp+methodName;
return DigestUtils.md5Hex(getContentBytes(params, "ASCII"));
}
/**
* @param content
* @param charset
* @return
* @throws SignatureException
* @throws UnsupportedEncodingException
*/
private static byte[] getContentBytes(String content, String charset) {
if (charset == null || "".equals(charset)) {
return content.getBytes();
}
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("MD5簽名過程中出現錯誤,指定的編碼集錯誤:" + charset);
}
}