Search in sources :

Example 1 with QiniuApiException

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());
    }
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) QiniuApiException(com.zyd.blog.file.exception.QiniuApiException)

Example 2 with QiniuApiException

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());
    }
}
Also used : Response(com.qiniu.http.Response) VirtualFile(com.zyd.blog.file.entity.VirtualFile) QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) QiniuApiException(com.zyd.blog.file.exception.QiniuApiException) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) Date(java.util.Date) UploadManager(com.qiniu.storage.UploadManager)

Example 3 with QiniuApiException

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());
    }
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) QiniuApiException(com.zyd.blog.file.exception.QiniuApiException)

Aggregations

QiniuApiException (com.zyd.blog.file.exception.QiniuApiException)3 QiniuException (com.qiniu.common.QiniuException)2 Response (com.qiniu.http.Response)2 Configuration (com.qiniu.storage.Configuration)2 Auth (com.qiniu.util.Auth)2 VirtualFile (com.zyd.blog.file.entity.VirtualFile)2 BucketManager (com.qiniu.storage.BucketManager)1 UploadManager (com.qiniu.storage.UploadManager)1 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)1 GlobalFileException (com.zyd.blog.file.exception.GlobalFileException)1 Date (java.util.Date)1