WEBP转换过程,同时储存到BOS服务器

第一步创建WEBP存在数据库的信息实体类

package com.cn.data.entity.register;

/**
 * @author huangqiming
 * B端用户会员扩展信息
 * 设计表结构时,字符串属性默认为空字符串。
 * 通过code与商户关联
 */
import com.cn.data.base.*;
import javax.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "BusinessExtend",indexes={@Index(name="be_code_index",columnList="code")})
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL,region="distribution")

public class BusinessExtend extends BasePageEntity {

    private String jpgpath="";                     // JPG的图片路径
    private String jpgobjectKey="";                // 记录图片在BOS里的objectKey
    private String webppath="";                    // webp的图片路径
    private String webpobjectKey="";               // 记录图片在BOS里的objectKey
    private String md5hashValue="";                // 图片的哈希值的MD5压缩值,可用这个值判断图片是否重复

    public BusinessExtend() {
        super();
    }

    public String getJpgpath() {
        return jpgpath;
    }

    public void setJpgpath(String jpgpath) {
        this.jpgpath = jpgpath;
    }

    public String getJpgobjectKey() {
        return jpgobjectKey;
    }

    public void setJpgobjectKey(String jpgobjectKey) {
        this.jpgobjectKey = jpgobjectKey;
    }

    public String getWebppath() {
        return webppath;
    }

    public void setWebppath(String webppath) {
        this.webppath = webppath;
    }

    public String getWebpobjectKey() {
        return webpobjectKey;
    }

    public void setWebpobjectKey(String webpobjectKey) {
        this.webpobjectKey = webpobjectKey;
    }

    public String getMd5hashValue() {
        return md5hashValue;
    }

    public void setMd5hashValue(String md5hashValue) {
        this.md5hashValue = md5hashValue;
    }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

第二步,调用方法将传入的文件由JPG转为WEBP同时存入BOS服务器,返回过来的参数转为MAP集合,用作实体类信息

@RequestMapping(value = "/insertBusinessRegister",method = RequestMethod.POST)
public CommonPackMap insertBusinessRegister(MultipartFile avatar_file,Businessregister businessregister) throws Exception {
    Map<String, String> stringStringMap = this.saveAttachFile(avatar_file);
    return businessdetailsFace.insertbusinessregister(businessregister,stringStringMap);
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

第三步,写文件由JPG转为WEBP,同时存入BOS服务器的方法

package com.cn.controller;

/**
 * @author huangqiming
 * @所有控制层的父类:公共方法可放在此类中
 * @增加BOS图片操作功能
 */
import java.io.*;
import java.util.*;
import com.cn.tools.*;
import com.cn.util.*;
import javax.imageio.*;
import org.springframework.web.multipart.*;

public class ParentBosController extends ParentController {

    public ParentBosController() {
        super();
    }

    /**
     * 图片上传到BOS,返回webp和jpg两种图片的路径、图片的哈希值的MD5压缩值:md5hashValue
     * sRand:验证码的值
     * jpg:验证码的图片路径
     */
    protected Map<String,String> createVerifiCode() throws Exception {
        String sRand = "";                   // 验证码值
        String bucketName="verificode";      // BOS验证码图片存储Bucket名称
        Map<String,String> map=new HashMap<>();
        String rootPath=getRootPath();
        String filename=UUID.randomUUID() +".jpg";
        rootPath=rootPath+filename;
        sRand=VerifiCodeTool.createVerifiCode(rootPath);
        map.put("sRand",sRand);
        BosClientTool.getBosClientTool().putObject(bucketName,filename,rootPath);               // 把对象存储在BOS
        map.put("jpg",BosClientTool.getBosClientTool().generateBosdUrl(bucketName,filename));  // 获取路径
        File file=new File(rootPath);
        file.delete();         // 删除临时文件
        return map;
    }

    /**
     * 获取运行环境的bucketName
     */
    private String getBucketName() throws Exception {
        String bucketName=null;
        String activeProfile=SpringContextUtil.getActiveProfile();
        switch(activeProfile) {      // 根据运行的平台,获取文件存储的路径和BOS的bucketName。
            case "dev" :
                bucketName=BlockAttribute.devbucketName;
                break;
            case "test" :
                 bucketName=BlockAttribute.devbucketName;
                break;
            case "prod" :
                bucketName=BlockAttribute.prodbucketName;
                break;
        }
        return bucketName;
    }

    /**
     * 获取运行环境的rootPath
     */
    private String getRootPath() throws Exception {
        String rootPath=null;
        String activeProfile=SpringContextUtil.getActiveProfile();
        switch(activeProfile) {      // 根据运行的平台,获取文件存储的路径和BOS的bucketName。
            case "dev" :
                rootPath= BlockAttribute.devfilepath;
                break;
            case "test" :
                rootPath= BlockAttribute.testfilepath;
                break;
            case "prod" :
                rootPath=BlockAttribute.prodfilepath;
                break;
        }
        return rootPath;
    }

    /**
     * 图片上传到BOS,返回webp和jpg两种图片的路径、图片的哈希值的MD5压缩值:md5hashValue
     */
    protected Map<String,String> saveAttachFile(MultipartFile file) throws Exception {
        Map<String,String> map=new HashMap<>();
        String rootPath=getRootPath();
        String bucketName=getBucketName();
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filename = UUID.randomUUID() + suffix;
        rootPath=rootPath+filename;
        File serverFile = new File(rootPath);
        file.transferTo(serverFile);
        BosClientTool.getBosClientTool().putObject(bucketName,filename,rootPath);               // 把对象存储在BOS
        map.put("jpgobjectKey",filename);
        map.put("jpg",BosClientTool.getBosClientTool().generateBosdUrl(bucketName,filename));  // 获取路径
        File webFile=ImageConverterWebp.toWebpFile(rootPath);
        filename=filename+".webp";
        BosClientTool.getBosClientTool().putObject(bucketName,filename,webFile.getPath());      // 把对象存储在BOS
        map.put("webpobjectKey",filename);
        map.put("webp",BosClientTool.getBosClientTool().generateBosdUrl(bucketName,filename));  // 获取路径
        FingerPrint fp1 = new FingerPrint(ImageIO.read(new File(rootPath)));
        map.put("md5hashValue",MD5Encode.md5Encode(fp1.toString(true)));
        webFile.delete();     // 删除临时文件
        serverFile.delete();  // 删除临时文件
        return map;
    }

    /**
     * 删除BOS上的图片
     * objectKey:保存在图片表的objectKey值
     */
    protected void delAttachFile(String objectKey) throws Exception {
        String bucketName=getBucketName();
        BosClientTool.getBosClientTool().deleteObject(bucketName,objectKey);
    }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

第四步,将传进来的Map参数传入到方法中然后set注入成完整的实体类,然后通过Hibrnate直接传如数据库

@CacheEvict(value = "bregistercache",allEntries=true)
public CommonPackMap insertbusinessregister(Businessregister businessregister,Map<String, String> HashMap) throws Exception {
    if (businessregister.getOperatorid()!=null&&businessregister.isFrozen()==false){
        Businessregister businessregister1 = this.dao.unique("select mv from Businessregister mv where mv.id='" + businessregister.getId() + "'", Businessregister.class);
        if (businessregister1 == null){
            businessregister = this.createBusinessregister(null, businessregister);
            BusinessExtend businessExtend = this.createbusinessExtend(HashMap,businessregister.getCode(),null);
            this.dao.save(businessExtend);
            this.dao.save(businessregister);
            HashMap<String, String> data = new HashMap<>();
           data.put("message", "save success");
           return this.returnCommonPackMap("", data);
        } else {
           throw this.createLzhException(ExceptionCode.error10017);
        }
    }
   else {
      throw this.createLzhException(ExceptionCode.error10008);
   }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

第5步,传入参数Map封装WEBP实体类的方法
private BusinessExtend createbusinessExtend(Map<String,String> hashMap,String code,BusinessExtend unique){
    BusinessExtend businessExtend = new BusinessExtend();
    businessExtend.setWebppath(hashMap.get("webp"));
    businessExtend.setWebpobjectKey(hashMap.get("webpobjectKey"));
    businessExtend.setJpgpath(hashMap.get("jpg"));
    businessExtend.setJpgobjectKey(hashMap.get("jpgobjectKey"));
    businessExtend.setMd5hashValue(hashMap.get("md5hashValue"));
    businessExtend.setCode(code);
    if (unique!=null) {
        unique.setWebppath(businessExtend.getWebppath());
        unique.setMd5hashValue(businessExtend.getMd5hashValue());
        unique.setWebpobjectKey(businessExtend.getWebpobjectKey());
        unique.setJpgobjectKey(businessExtend.getJpgobjectKey());
        unique.setJpgpath(businessExtend.getJpgpath());
        unique.setUpdatedate(new Date());
        return unique;
    }else {
        return businessExtend;
    }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

insert success

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值