public String upload2(MultipartFile file, String prefix, ImageVEntity isImage) {
String accessKey = "";
//构造一个带指定 Region 对象的配置类,指定存储区域,和存储空间选择的区域一致
Configuration cfg = new Configuration(Region.huabei());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String secretKey = "";
String bucket = "";
String url = "";
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = UUID.randomUUID().toString()+"."+prefix;
try {
//得到本地文件的字节数组
byte[] bytes = IOUtils.toByteArray(file.getInputStream());
//认证
Auth auth = Auth.create(accessKey, secretKey);
//认证通过后得到token(令牌)
String upToken = auth.uploadToken(bucket);
try {
//上传文件,参数:字节数组,key,token令牌
//key: 建议我们自已生成一个不重复的名称
Response response = uploadManager.put(bytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
String imageUrl = url + putRet.key;
if (isImage.isImage()) {
// checkResult(imageUrl);
}
return imageUrl;
} catch (QiniuException ex) {
log.error("上传文件至OSS[异常]",ex);
}
} catch (IOException ex) {
log.error("上传文件至OSS[异常]",ex);
}
return null;
}