1: Import the relevant packages (Note 4 OH)
Compile files (' Libs/android-async-http-1.4.9.jar ')
Compile files (' Libs/qiniu-android-sdk-7.0.9.jar ')
Compile files (' Libs/happy-dns-0.2.4.jar ')
Compile files (' Libs/httpclient-4.3.6.jar ')
2: See the code below to invoke the method.
Package cn.haodehaode.utils;
Import Android.util.Log;
Import Com.qiniu.android.http.ResponseInfo;
Import Com.qiniu.android.storage.UpCompletionHandler;
Import Com.qiniu.android.storage.UploadManager;
Import com.qiniu.android.utils.UrlSafeBase64;
Import Org.json.JSONObject;
Import Javax.crypto.Mac;
Import Javax.crypto.SecretKey;
Import Javax.crypto.spec.SecretKeySpec;
/**
* Seven Cattle Upload Tool class
*
* @author Jalen [email protected]
* @version V1.0
* @Title: ${file_name}
* @Package Cn.haodehaode.utils
* @Description: ${todo}
* @date 15/10/31 15:32
*/
public class Lxqiniuuploadutils {
Seven Bull backstage key
private static String AccessKey = Hdconstants.qn_accesskey;
Secret in the back of seven cows
private static String Secretkey = Hdconstants.qn_secretkey;
private static final String Mac_name = "HmacSHA1";
private static final String ENCODING = "UTF-8";
public static void Uploadpic () {
String keys = Commonutils.gettodaydate18 ();
String bucketname = hdconstants.bucket_authfile;
try {
1: The first way to construct an upload strategy
Jsonobject _json = new Jsonobject ();
Long _dataline = System.currenttimemillis ()/1000 + 3600;
_json.put ("Deadline", _dataline);//valid time is one hours
_json.put ("Scope", bucketname);
String _encodedputpolicy = urlsafebase64.encodetostring (_json
. toString (). GetBytes ());
byte[] _sign = Hmacsha1encrypt (_encodedputpolicy, Secretkey);
String _encodedsign = urlsafebase64.encodetostring (_sign);
String _uploadtoken = AccessKey + ': ' + _encodedsign + ': '
+ _encodedputpolicy;
Here is a picture of the simulated local album
String Picpath = "/storage/emulated/0/dcim/camera/img_20150924_214351.jpg";
UploadManager UploadManager = new UploadManager ();
The following APIs can be modified as image addresses or file or byte[]
Uploadmanager.put (Picpath, keys, _uploadtoken,
New Upcompletionhandler () {
@Override
public void complete (String key, Responseinfo info,
Jsonobject response) {
LOG.E ("Qiniu", info.tostring ());
}
}, NULL);
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* This signature method for half a day to find out the 、、、、 program ape hard AH 、、、 use HMAC-SHA1 signature method to sign the Encrypttext
*
* @param Encrypttext signed string
* @param encryptkey Key
* @return
* @throws Exception
*/
public static byte[] Hmacsha1encrypt (String encrypttext, String encryptkey)
Throws Exception {
byte[] data = Encryptkey.getbytes (ENCODING);
Constructs a key based on a given byte array, and the second parameter specifies the name of a key algorithm
Secretkey Secretkey = new Secretkeyspec (data, mac_name);
Generates a Mac object that specifies the MAC algorithm
Mac Mac = Mac.getinstance (mac_name);
Initializes a Mac object with a given key
Mac.init (Secretkey);
byte[] Text = encrypttext.getbytes (ENCODING);
Complete MAC operation
return mac.dofinal (text);
}
}
?
Android on the introduction of seven cattle upload images or file final finishing version (available)