use of com.zyd.blog.file.exception.QiniuApiException in project OneBlog by zhangyd-c.
the class BaseApiClient method uploadImg.
@Override
public VirtualFile uploadImg(File file) {
this.check();
if (file == null) {
throw new QiniuApiException("[" + this.storageType + "]文件上传失败:文件不可为空");
}
try {
InputStream is = new BufferedInputStream(new FileInputStream(file));
VirtualFile res = this.uploadImg(is, "temp" + FileUtil.getSuffix(file));
VirtualFile imageInfo = ImageUtil.getInfo(file);
return res.setSize(imageInfo.getSize()).setOriginalFileName(file.getName()).setWidth(imageInfo.getWidth()).setHeight(imageInfo.getHeight());
} catch (FileNotFoundException e) {
throw new GlobalFileException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
}
}
use of com.zyd.blog.file.exception.QiniuApiException in project OneBlog by zhangyd-c.
the class QiniuApiClient method uploadImg.
/**
* 上传图片
*
* @param is 图片流
* @param imageUrl 图片路径
* @return 上传后的路径
*/
@Override
public VirtualFile uploadImg(InputStream is, String imageUrl) {
this.check();
String key = FileUtil.generateTempFileName(imageUrl);
this.createNewFileName(key, this.pathPrefix);
Date startTime = new Date();
// Zone.zone0:华东
// Zone.zone1:华北
// Zone.zone2:华南
// Zone.zoneNa0:北美
Configuration cfg = new Configuration(Region.autoRegion());
UploadManager uploadManager = new UploadManager(cfg);
try {
Auth auth = Auth.create(this.accessKey, this.secretKey);
String upToken = auth.uploadToken(this.bucket);
Response response = uploadManager.put(is, this.newFileName, upToken, null, null);
// 解析上传成功的结果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
return new VirtualFile().setOriginalFileName(key).setSuffix(this.suffix).setUploadStartTime(startTime).setUploadEndTime(new Date()).setFilePath(putRet.key).setFileHash(putRet.hash).setFullFilePath(this.path + putRet.key);
} catch (QiniuException ex) {
throw new QiniuApiException("[" + this.storageType + "]文件上传失败:" + ex.error());
}
}
use of com.zyd.blog.file.exception.QiniuApiException in project OneBlog by zhangyd-c.
the class QiniuApiClient method removeFile.
/**
* 删除七牛空间图片方法
*
* @param key 七牛空间中文件名称
*/
@Override
public boolean removeFile(String key) {
this.check();
if (StringUtils.isNullOrEmpty(key)) {
throw new QiniuApiException("[" + this.storageType + "]删除文件失败:文件key为空");
}
Auth auth = Auth.create(this.accessKey, this.secretKey);
Configuration config = new Configuration(Region.autoRegion());
BucketManager bucketManager = new BucketManager(auth, config);
try {
Response re = bucketManager.delete(this.bucket, key);
return re.isOK();
} catch (QiniuException e) {
Response r = e.response;
throw new QiniuApiException("[" + this.storageType + "]删除文件发生异常:" + r.toString());
}
}
Aggregations