All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingxun.javasdkapi.request.OcrUploadTransRequest Maven / Gradle / Ivy

package com.qingxun.javasdkapi.request;

import cn.hutool.crypto.SecureUtil;
import com.qingxun.javasdkapi.utils.InputStreamUtil;
import com.qingxun.javasdkapi.utils.Md5BytesUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.util.TreeMap;


/**
 * 文件上传请求参数
 *
 * @author cedric
 */
public class OcrUploadTransRequest extends BaseRequest {

    private String from;


    /**
     * md5小写
     */
    private String md5;

    private File file;

    private byte[] fileBytes;
    private  String fileName;

    public String getFrom() {
        return from;
    }

    public String getMd5() {
        return md5;
    }



    public File getFile() {
        return file;
    }

    public byte[] getFileBytes() {
        return fileBytes;
    }

    public String getFileName() {
        return fileName;
    }

    private OcrUploadTransRequest() {

    }

    @Override
    public TreeMap transToMap() {
        TreeMap map = new TreeMap();
        map.put("from", this.from);
        map.put("md5", this.md5);
        return map;
    }

    public OcrUploadTransRequest(Builder builder) {
        if (StringUtils.isEmpty(builder.from)) {
            throw new NullPointerException("from 字段为空");
        }
        if (builder.file == null) {
            if (StringUtils.isEmpty(builder.fileName)) {
                throw new NullPointerException("file为空");
            }
            if (builder.fileStream == null ){
                throw new NullPointerException("file为空");
            }
        }
        if (StringUtils.isEmpty(builder.md5)) {
            if (null != builder.file) {
                builder.setMd5(SecureUtil.md5(builder.file));
            }else if (null != builder.fileStream){
                byte[] bytes = InputStreamUtil.transToBytes(builder.fileStream);
                this.fileBytes = bytes;
                builder.setMd5(Md5BytesUtil.md5(bytes));
            }
        }
        if (StringUtils.isEmpty(builder.md5)) {
            throw new NullPointerException("md5 字段为空");
        }
        this.from = builder.from;
        this.md5 = builder.md5;
        this.file = builder.file;
        this.fileName = builder.fileName;
    }


    public static class Builder {

        private String from;

        /**
         * md5小写
         */
        private String md5;

        private File file;

        private InputStream fileStream;

        private String fileName;

        public Builder setFrom(String from) {
            this.from = from;
            return this;
        }

        public Builder setMd5(String md5) {
            this.md5 = md5;
            return this;
        }

        public Builder setFile(File file) {
            this.file = file;
            return this;
        }

        public Builder setFileStream(InputStream fileStream) {
            this.fileStream = fileStream;
            return this;
        }

        public Builder setFileName(String  filename) {
            this.fileName = filename;
            return this;
        }

        public OcrUploadTransRequest builder() {
            return new OcrUploadTransRequest(this);
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy