base64 编码转png上传minio

 public static InputStream base64ConvertPNG(String base64){
        BASE64Decoder decoder = new BASE64Decoder();
        //解码
        base64 = base64.replaceAll(" ", "+");
        try {
            byte[] buffer = decoder.decodeBuffer(base64.replace("data:image/png;base64", "")
                    .replace("data:image/jpeg;base64", "")
                    .replace("data:image/jpeg;base64", ""));
            for(int i = 0;i
public SysFile uploadInputStream(String baseString) {
        try {
            InputStream inputStream = Base64ToPngUtils.base64ConvertPNG(baseString);
            // 上传并返回访问地址
            String url = uploadInputStream(inputStream);
            SysFile sysFile = new SysFile();
            sysFile.setUrl(url);
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

   /**
     * 文件上传接口
     *
     * @param stream 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    public String uploadInputStream(InputStream stream) throws Exception{
        String fileName = StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),IdUtils.fastUUID(),Seq.getId(Seq.uploadSeqType),"png");
        try(InputStream inputStream = stream){
            PutObjectArgs args = PutObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(fileName)
                .stream(stream, stream.available(), PutObjectArgs.MAX_PART_SIZE)
                .contentType(ViewContentType.PNG.getType())
                .build();
            client.putObject(args);
            IoUtils.closeQuietly(inputStream);
            return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 文件信息
 * 
 * @author taihang
 */
public class SysFile
{
    /**
     * 文件名称
     */
    private String name;

    /**
     * 文件地址
     */
    private String url;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("name", getName())
            .append("url", getUrl())
            .toString();
    }
}

你可能感兴趣的:(java,开发语言)