七牛云 网络存储 文件上传

时间:2021-11-09 12:49:04
设置自定义域名:
如果你的域名没有备案,哪种云存储都用不了,就不要尝试了。 七牛云存储添加自定义域名和域名解析图文教程: http://boke112.com/3870.html  

上传文件到七牛

配置依赖:
implementation 'com.squareup.okhttp3:okhttp:3.8.0'
implementation 'com.squareup.okio:okio:1.13.0'
implementation 'com.qiniu:qiniu-android-sdk:7.3.12'
1
implementation 'com.squareup.okhttp3:okhttp:3.8.0'
2
implementation 'com.squareup.okio:okio:1.13.0'
3
implementation 'com.qiniu:qiniu-android-sdk:7.3.12'
权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1
<uses-permission android:name="android.permission.INTERNET"/>
2
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
工具类:
public class QiNiuUtils {
	private static final String ACCESSKEY = "m1-qhAIzfk6_rNdWfvf5ngaybQva5CmzHl9-pccg";
	private static final String SECRETKEY = "ZMFhkutPPZj86BbjV9O7RtDEs0XOQNlkPUYc-kIV";
	private static final String BUCKET_NAME = "temp";//存储空间的名字
	private static final String MAC_NAME = "HmacSHA1";
	private static final String ENCODING = "UTF-8";
	
	public static void upLoad(File file, String key,Configuration config,UpCompletionHandler handler) {
		String token = QiNiuUtils.getToken();
		new UploadManager(config).put(file, key, token, handler, null);
	}
	
    //获取Token
	private static String getToken() {
		JSONObject json = new JSONObject();
		long deadline = System.currentTimeMillis() / 1000 + 3600 * 12;
		try {
			json.put("deadline", deadline);
			json.put("scope", BUCKET_NAME);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		String encodedPutPolicy = UrlSafeBase64.encodeToString(json.toString());
		
		byte[] sign = null;
		try {
			sign = hmacsha1encrypt(encodedPutPolicy, SECRETKEY);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String encodedSign = UrlSafeBase64.encodeToString(sign);
		return ACCESSKEY + ':' + encodedSign + ':' + encodedPutPolicy;
	}
	
	private static byte[] hmacsha1encrypt(String encryptText, String encryptKey) throws Exception {
		byte[] data = encryptKey.getBytes(ENCODING);
		SecretKeySpec secretKey = new SecretKeySpec(data, MAC_NAME);
		Mac mac = Mac.getInstance(MAC_NAME);
		mac.init(secretKey);
		byte[] text = encryptText.getBytes(ENCODING);
		return mac.doFinal(text);
	}
}
x
 
1
public class QiNiuUtils {
2
 private static final String ACCESSKEY = "m1-qhAIzfk6_rNdWfvf5ngaybQva5CmzHl9-pccg";
3
 private static final String SECRETKEY = "ZMFhkutPPZj86BbjV9O7RtDEs0XOQNlkPUYc-kIV";
4
 private static final String BUCKET_NAME = "temp";//存储空间的名字
5
 private static final String MAC_NAME = "HmacSHA1";
6
 private static final String ENCODING = "UTF-8";
7
 
8
 public static void upLoad(File file, String key,Configuration config,UpCompletionHandler handler) {
9
  String token = QiNiuUtils.getToken();
10
  new UploadManager(config).put(file, key, token, handler, null);
11
 }
12
 
13
    //获取Token
14
 private static String getToken() {
15
  JSONObject json = new JSONObject();
16
  long deadline = System.currentTimeMillis() / 1000 + 3600 * 12;
17
  try {
18
   json.put("deadline", deadline);
19
   json.put("scope", BUCKET_NAME);
20
  } catch (JSONException e) {
21
   e.printStackTrace();
22
  }
23
  
24
  String encodedPutPolicy = UrlSafeBase64.encodeToString(json.toString());
25
  
26
  byte[] sign = null;
27
  try {
28
   sign = hmacsha1encrypt(encodedPutPolicy, SECRETKEY);
29
  } catch (Exception e) {
30
   e.printStackTrace();
31
  }
32
  
33
  String encodedSign = UrlSafeBase64.encodeToString(sign);
34
  return ACCESSKEY + ':' + encodedSign + ':' + encodedPutPolicy;
35
 }
36
 
37
 private static byte[] hmacsha1encrypt(String encryptText, String encryptKey) throws Exception {
38
  byte[] data = encryptKey.getBytes(ENCODING);
39
  SecretKeySpec secretKey = new SecretKeySpec(data, MAC_NAME);
40
  Mac mac = Mac.getInstance(MAC_NAME);
41
  mac.init(secretKey);
42
  byte[] text = encryptText.getBytes(ENCODING);
43
  return mac.doFinal(text);
44
 }
45
}
上传案例:
Configuration config = new Configuration.Builder().build();
String fileName = file.getName();
QiNiuUtils.upLoad(file, fileName, config, (key, info, response) -> {
   //用户自定义的内容上传完成后处理动作必须实现的方法,建议用户自己处理异常。若未处理,抛出的异常被直接丢弃。
   Log.i("bqt", "文件上传保存名称:" + key);
   Log.i("bqt", "上传完成返回日志信息:\n" + new Gson().toJson(info));
   Log.i("bqt", "上传完成的回复内容:\n" + response);
});
x
 
1
Configuration config = new Configuration.Builder().build();
2
String fileName = file.getName();
3
QiNiuUtils.upLoad(file, fileName, config, (key, info, response) -> {
4
   //用户自定义的内容上传完成后处理动作必须实现的方法,建议用户自己处理异常。若未处理,抛出的异常被直接丢弃。
5
   Log.i("bqt", "文件上传保存名称:" + key);
6
   Log.i("bqt", "上传完成返回日志信息:\n" + new Gson().toJson(info));
7
   Log.i("bqt", "上传完成的回复内容:\n" + response);
8
});
打印日志
文件上传保存名称:sms_2018.09.08-16_02_09.txt
上传完成返回日志信息
{
    "duration": 923,
    "host": "upload.qiniup.com",
    "id": "1536393734861932",
    "ip": "111.177.9.136",
    "path": "/",
    "port": 80,
    "reqId": "BwMAAOKFEJb2XVIV",
    "response": {
        "nameValuePairs": {
            "hash": "FhX-pWNc1ThcCCVIryzKZQyRQhsq",
            "key": "sms_2018.09.08-16_02_09.txt"
        }
    },
    "sent": 422819,
    "statusCode": 200,
    "timeStamp": 1536393736,
    "totalSize": 422098,
    "upToken": {
        "accessKey": "m1-qhAIzfk6_rNdWfvf5ngaybQva5CmzHl9-pccg",
        "returnUrl": "",
        "token": "..."
    },
    "xlog": "...",
    "xvia": "vdn-hbxy-tel-1-1"
}
上传完成的回复内容
{
    "hash": "FhX-pWNc1ThcCCVIryzKZQyRQhsq",
    "key": "sms_2018.09.08-16_02_09.txt"
}
1
文件上传保存名称:sms_2018.09.08-16_02_09.txt
2
上传完成返回日志信息
3
{
4
    "duration": 923,
5
    "host": "upload.qiniup.com",
6
    "id": "1536393734861932",
7
    "ip": "111.177.9.136",
8
    "path": "/",
9
    "port": 80,
10
    "reqId": "BwMAAOKFEJb2XVIV",
11
    "response": {
12
        "nameValuePairs": {
13
            "hash": "FhX-pWNc1ThcCCVIryzKZQyRQhsq",
14
            "key": "sms_2018.09.08-16_02_09.txt"
15
        }
16
    },
17
    "sent": 422819,
18
    "statusCode": 200,
19
    "timeStamp": 1536393736,
20
    "totalSize": 422098,
21
    "upToken": {
22
        "accessKey": "m1-qhAIzfk6_rNdWfvf5ngaybQva5CmzHl9-pccg",
23
        "returnUrl": "",
24
        "token": "..."
25
    },
26
    "xlog": "...",
27
    "xvia": "vdn-hbxy-tel-1-1"
28
}
29
上传完成的回复内容
30
{
31
    "hash": "FhX-pWNc1ThcCCVIryzKZQyRQhsq",
32
    "key": "sms_2018.09.08-16_02_09.txt"
33
}

MPic-图床神器

一款支持多种上传方式且自动生成MarkDown链接的图床工具
sago
m1-qhAIzfk6_rNdWfvf5ngaybQva5CmzHl9-pccg
ZMFhkutPPZj86BbjV9O7RtDEs0XOQNlkPUYc-kIV
oxns24xcm.bkt.clouddn.com
如:http://oxns24xcm.bkt.clouddn.com/bqt.jpg
x
1
sago
2
m1-qhAIzfk6_rNdWfvf5ngaybQva5CmzHl9-pccg
3
ZMFhkutPPZj86BbjV9O7RtDEs0XOQNlkPUYc-kIV
4
oxns24xcm.bkt.clouddn.com
5
如:http://oxns24xcm.bkt.clouddn.com/bqt.jpg
七牛云 网络存储 文件上传
2018-9-8