Search in sources :

Example 1 with OssApiException

use of com.zyd.blog.file.exception.OssApiException in project OneBlog by zhangyd-c.

the class AliyunOssApiClient method uploadImg.

@Override
public VirtualFile uploadImg(InputStream is, String imageUrl) {
    this.check();
    String key = FileUtil.generateTempFileName(imageUrl);
    this.createNewFileName(key, this.pathPrefix);
    Date startTime = new Date();
    try (InputStream uploadIs = StreamUtil.clone(is);
        InputStream fileHashIs = StreamUtil.clone(is)) {
        ossApi.uploadFile(uploadIs, this.newFileName, bucketName);
        return new VirtualFile().setOriginalFileName(FileUtil.getName(key)).setSuffix(this.suffix).setUploadStartTime(startTime).setUploadEndTime(new Date()).setFilePath(this.newFileName).setFileHash(DigestUtils.md5DigestAsHex(fileHashIs)).setFullFilePath(this.url + this.newFileName);
    } catch (IOException e) {
        throw new OssApiException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) InputStream(java.io.InputStream) OssApiException(com.zyd.blog.file.exception.OssApiException) IOException(java.io.IOException) Date(java.util.Date)

Example 2 with OssApiException

use of com.zyd.blog.file.exception.OssApiException in project OneBlog by zhangyd-c.

the class BaseApiClient method uploadImg.

@Override
public VirtualFile uploadImg(MultipartFile file) {
    this.check();
    if (file == null) {
        throw new OssApiException("[" + this.storageType + "]文件上传失败:文件不可为空");
    }
    try {
        VirtualFile res = this.uploadImg(file.getInputStream(), file.getOriginalFilename());
        VirtualFile imageInfo = ImageUtil.getInfo(file);
        return res.setSize(imageInfo.getSize()).setOriginalFileName(file.getOriginalFilename()).setWidth(imageInfo.getWidth()).setHeight(imageInfo.getHeight());
    } catch (IOException e) {
        throw new GlobalFileException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
    }
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) OssApiException(com.zyd.blog.file.exception.OssApiException)

Example 3 with OssApiException

use of com.zyd.blog.file.exception.OssApiException in project OneBlog by zhangyd-c.

the class OssApi method createFolder.

/**
 * 创建模拟文件夹本质上来说是创建了一个名字以“/”结尾的文件;<br>
 * 对于这个文件照样可以上传下载,只是控制台会对以“/”结尾的文件以文件夹的方式展示;<br>
 * 多级目录创建最后一级即可,比如dir1/dir2/dir3/,创建dir1/dir2/dir3/即可,dir1/、dir1/dir2/不需要创建;
 *
 * @param folder     目录名
 * @param bucketName 存储空间
 */
public void createFolder(String folder, String bucketName) throws OssApiException {
    try {
        if (null == bucketName) {
            throw new OssApiException("[阿里云OSS] 尚未指定Bucket!");
        }
        if (!this.client.doesBucketExist(bucketName)) {
            throw new OssApiException("[阿里云OSS] 无法创建目录!Bucket不存在:" + bucketName);
        }
        folder = folder.endsWith("/") ? folder : folder + "/";
        this.client.putObject(bucketName, folder, new ByteArrayInputStream(new byte[0]));
    } finally {
        this.shutdown();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OssApiException(com.zyd.blog.file.exception.OssApiException)

Aggregations

OssApiException (com.zyd.blog.file.exception.OssApiException)3 VirtualFile (com.zyd.blog.file.entity.VirtualFile)2 GlobalFileException (com.zyd.blog.file.exception.GlobalFileException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1