- @param fileType 文件类型例如jpg,mp4等
- @param callback 回调函数我这里用于传cancelToken,方便后期关闭请求
- @param onSendProgress 上传的进度事件
*/
static Future upload({ File file , String rootDir = ‘moment’, String fileType, Function callback, Function onSendProgress}) async {
String policyText = ‘{“expiration”: “KaTeX parse error: Expected '}', got 'EOF' at end of input: …: [{"bucket": "bucket” },[“content-length-range”, 0, 1048576000]]}’;
// 获取签名
String signature = getSignature(policyText);
BaseOptions options = new BaseOptions();
options.responseType = ResponseType.plain;
//创建dio对象
Dio dio = new Dio(options);
// 生成oss的路径和文件名我这里目前设置的是moment/20201229/test.mp4
String pathName = ‘
r
o
o
t
D
i
r
/
rootDir/
rootDir/{getDate()}/
g
e
t
R
a
n
d
o
m
(
12
)
.
{getRandom(12)}.
getRandom(12).{fileType == null ? getFileType(file.path) : fileType}’;
// 请求参数的form对象
FormData data = new FormData.fromMap({
‘key’: pathName,
‘policy’: getSplicyBase64(policyText),
‘OSSAccessKeyId’: ossAccessKeyId,
‘success_action_status’: ‘200’, //让服务端返回200,不然,默认会返回204
‘signature’: signature,
‘contentType’: ‘multipart/form-data’,
‘file’: MultipartFile.fromFileSync(file.path),
});
Response response;
CancelToken uploadCancelToken = CancelToken();
callback ?? callback(uploadCancelToken);
try {
// 发送请求
response = await dio.post(url, data: data, cancelToken: uploadCancelToken, onSendProgress: (int count, int data) {
onSendProgress(count, data);
});
// 成功后返回文件访问路径
return ‘
u
r
l
/
url/
url/pathName’;
} catch(e) {
throw(e.message);
}
}
/*
- 生成固定长度的随机字符串
- /
static String getRandom(int num) {
String alphabet = ‘qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM’;
String left = ‘’;
for (var i = 0; i < num; i++) {
// right = right + (min + (Random().nextInt(max - min))).toString();
left = left + alphabet[Random().nextInt(alphabet.length)];
}
return left;
}
/ - 根据图片本地路径获取图片名称
- /
static String getImageNameByPath(String filePath) {
// ignore: null_aware_before_operator
return filePath?.substring(filePath?.lastIndexOf(“/”)+1,filePath?.length);
}
/* - 获取文件类型
*/
static String getFileType(String path) {
print(path);
List array = path.split(‘.’);
return array[array.length -1];
}
/// 获取日期
static String getDate() {
DateTime now = DateTime.now();
return ‘ n o w . y e a r {now.year} now.year{now.month}${now.day}’;
}
// 获取plice的base64
static getSplicyBase64(String policyText) {
//进行utf8编码
List policyText_utf8 = utf8.encode(policyText);
//进行base64编码
String policy_base64 = base64.encode(policyText_utf8);
return policy_base64;
}
/// 获取签名
static String getSignature(String policyText) {
//进行utf8编码
List policyText_utf8 = utf8.encode(policyText);
//进行base64编码
String policy_base64 = base64.encode(policyText_utf8);
//再次进行utf8编码
List policy = utf8.encode(policy_base64);
//进行utf8 编码
List key = utf8.encode(ossAccessKeySecret);
//通过hmac,使用sha1进行加密
List signature_pre = Hmac(sha1, key).convert(policy).bytes;
//最后一步,将上述所得进行base64 编码
String signature = base64.encode(signature_pre);
return signature;
}
}
最后
文章所有资料全部已经打包整理好,另外小编手头上整理了大量Android架构师全套学习资料,Android核心高级技术PDF文档+全套高级学习资料+视频+2021 BAT 大厂面试真题解析
资料展示:
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
g-nYKBjtCe-1714744981073)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!