public class UploadToken {
public static String getUploadToken(Object object){
Gson gson=new Gson();
String jsonObject=gson.toJson(object);
String encodedPutPolicy = UrlSafeBase64.encodeToString(jsonObject);
String encodedSign="";
try {
encodedSign =hmacSha1(encodedPutPolicy);
}catch (Exception e){
}
String uploadToken = QiNiuConstant.ACCESSKEY + ":" + encodedSign + ":" + encodedPutPolicy;
return uploadToken;
}
public static String hmacSha1(final String base)
throws NoSuchAlgorithmException, InvalidKeyException {
String type = "HmacSHA1";
SecretKeySpec secret = new SecretKeySpec(QiNiuConstant.SECRETKEY.getBytes(), type);
Mac mac = Mac.getInstance(type);
mac.init(secret);
byte[] digest = mac.doFinal(base.getBytes());
return Base64.encodeToString(digest, Base64.URL_SAFE);
}
其中Object的为:
public class ImagePutPolicy implements Serializable {
private String scope;
private long deadline;
private ReturnBody returnBody;
public ImagePutPolicy(String fileName) {
scope = QiNiuConstant.SAVESPACE+":"+fileName;
}
public long getDeadline() {
return deadline;
}
public void setDeadline(long deadline) {
this.deadline = System.currentTimeMillis()/1000+deadline;
}
public ReturnBody getReturnBody() {
return returnBody;
}
public void setReturnBody(ReturnBody returnBody) {
this.returnBody = returnBody;
}
}
上传代码为
private void simpleUpload() {
ImagePutPolicy putPolicy=new ImagePutPolicy(key);
putPolicy.setDeadline(3600);
token=UploadToken.getUploadToken(putPolicy);
byte[] data=new byte[]{ 0, 1, 2, 3};
湖上湖
德玛西亚99
相关分类