1. 业务开发

默认采用分片上传设计,开发者只需要在自己的业务中关心抽象类返回的状态码是什么。 状态码分为三类:200表示所有分片上传完毕,返回值是文件地址(相对路径);206表示分片上传成功,返回值为当前总分片数;500表示上传失败。其余情况均抛出异常处理。

1.1. 扩展第三方云存储

默认集成了七牛云存储。

集成第三方接口步骤:
1、新建对应云存储类,并继承IUploadChunkService类,重写upload方法及checkFileIfExist方法,自定义一个 beanName 名称
2、 配置文件上传类型,打开菜单 自定义->自定义字典->文件上传类型 添加字典,标签名为 '存储类型' ,值为存储类的beanName,如:七牛云 uploadQiNiuService
3、 修改存储配置,打开菜单 文件管理->存储设置下拉选择存储方式 ;

1.2. 七牛云范例

1.2.1. 创建存储类 UploadQiNiuServiceImpl

/**
 * 七牛云的上传实现类
 */
@Service("uploadQiNiuService")
public class UploadQiNiuServiceImpl extends IUploadChunkService {

    @Override
    public ResultData upload(UploadConfigBean bean) {
        ResultData resultData = super.upload(bean);
        // 分片未上传完毕则返回
        if (resultData.getCode()!= HttpStatus.OK.value()){
            return resultData;
        }
        LOG.debug("七牛云上传实现");
        String targetFile = resultData.getData(String.class);

        try {
            ImageUtil.imgWatermark(targetFile);
        } catch (IOException e) {
            LOG.debug("水印添加失败");
            e.printStackTrace();
        }
        /**
         * 获取七牛云配置
         */
        Map<String,String> map = ConfigUtil.getMap("七牛云配置");
        String qiniuAk = map.get("qiniuAk");
        String qiniuSk = map.get("qiniuSk");
        String qiniuDomain = map.get("qiniuDomain");
        String bucketName = map.get("bucketName");
        if(!qiniuDomain.endsWith("/")){
            qiniuDomain += "/";
        }
        String uploadPath = ConfigUtil.getString(Const.CONFIG_UPLOAD, "uploadPath", "upload");
        // 判断配置的上传路径是否绝对路径
        boolean isReal = new File(uploadPath).isAbsolute();
        String uploadMapping = ConfigUtil.getString(Const.CONFIG_UPLOAD, "uploadMapping", "/upload/**");
        targetFile = targetFile.replace(uploadMapping.replace("**", ""), "");
        if(isReal) {
            //源图片
            targetFile = uploadPath+"/"+targetFile.replace(uploadMapping.replace("**", ""), "");
        } else {
            targetFile = BasicUtil.getRealPath(uploadPath + File.separator +targetFile.replace(uploadMapping.replace("**", ""), ""));
        }
        /**
         * 七牛云上传逻辑
         */
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传
        String accessKey = qiniuAk;
        String secretKey = qiniuSk;
        String bucket =  bucketName;
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = bean.getFileName();
        String filePath = null;

        InputStream inputStream = FileUtil.getInputStream(targetFile);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(inputStream,key,upToken,null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            filePath = qiniuDomain + putRet.key;
            System.out.println("完整文件路径:\t"+ filePath);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        // 清除本地服务器文件
        if (FileUtil.file(targetFile).exists()){
            FileUtil.del(targetFile);
        }
        return ResultData.build().success(filePath);

    }

    @Override
    public boolean checkFileIfExist(String realPath) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region0());
        //...其他参数参考类注释
        Map<String,String> map = ConfigUtil.getMap("七牛云配置");
        String qiniuAk = map.get("qiniuAk");
        String qiniuSk = map.get("qiniuSk");
        String bucketName = map.get("bucketName");
        String key = FileUtil.getName(realPath);
        Auth auth = Auth.create(qiniuAk, qiniuSk);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            FileInfo fileInfo = bucketManager.stat(bucketName, key);
            LOG.debug(fileInfo.md5);
            // todo 判断方式
            return StringUtils.isNotBlank(fileInfo.md5);
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
        return false;
    }
}

[!tip] 代码中的七牛云配置信息通过自定义配置实现

beanName 名称为 uploadQiNiuService

1.2.2. 配置文件上传类型 字典

1.2.3. 存储设置

选择七牛云存储

[!tip] 通过自定义配置实现

Copyright © mingsoft.net 2012-2022 all right reserved,powered by Gitbook该文件修订时间: 2023-04-18 15:20:19

results matching ""

    No results matching ""

    results matching ""

      No results matching ""