ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 组件源码 ``` /** * <b>方法描述:</b> Base64编码 <br/> * <b>创建者:</b> admin <br/> * <b>创建时间:</b> 2020-12-17 10:49:25 <br/> * * @param contants * 入参|待编码数据(byte数组或字符串,文件名)|{@link Object} * @param isfile * 入参|是否文件|boolean * @param codeset * 入参|字符串编码(UTF-8或GBK)|{@link java.lang.String} * @param base64txt * 出参|编码后的内容|{@link java.lang.String} * @return -1 异常<br/> * 0 失败<br/> * 1 成功<br/> */ @Component(label = "Base64编码", style = "判断型", type = "同步组件", comment = "对输入内容进行编码,如果是文件,输入文件名,根据文件名读取文件内容后对其编码。如果是字符串,需要指定字符串的编码(默认utf-8).", version = "1.0.0", deprecated = false, author = "admin", date = "2020-12-17 10:49:25") @InParams(param = {@Param(name = "contants", comment = "待编码数据(byte数组或字符串,文件名)", type = Object.class), @Param(name = "isfile", comment = "是否文件", type = boolean.class), @Param(name = "codeset", comment = "字符串编码(UTF-8或GBK)", type = java.lang.String.class)}) @OutParams(param = {@Param(name = "base64txt", comment = "编码后的内容", type = java.lang.String.class)}) @Returns( returns = {@Return(id = "-1", desp = "异常"), @Return(id = "0", desp = "失败"), @Return(id = "1", desp = "成功")}) @Order(value = 2) public static ResultBase P_base64Enc(Object contants, boolean isfile, String codeset) { String strErr = ""; if (contants == null) { return ResultBase.newFailureResult("TPTC0001", "base64Enc-待编码内容不能为空"); } try { if (isfile) { String fileName = contants.toString(); File file = new File(fileName); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; fis.read(buffer); fis.close(); ResultBase.newSuccessResult(new BASE64Encoder().encode(buffer)); } else { if(StrUtil.isBlank(codeset)) codeset="utf-8"; if (contants instanceof String) { return ResultBase .newSuccessResult(new BASE64Encoder().encode(((String)contants).getBytes(codeset))); } else if (contants instanceof byte[]) { return ResultBase.newSuccessResult(new BASE64Encoder().encode((byte[])contants)); } strErr = "不支持的数据类型"; } } catch (Exception ex) { strErr = AppLog.errorMsg(ex); } return ResultBase.newExceptionResult("TPTC0002", "base64Enc-编码错误:" + strErr); } ``` # 交易中组件使用方式 ![](https://img.kancloud.cn/74/88/7488ab7165d17f001313b83eb93e96ce_1868x867.png) # 参数说明及示例 ## 入口参数 待编码数据(byte数组或字符串,文件名):准备进行 Base64 编码的数据,示例: `"abc"` 是否文件:数据是否是文件,示例: `false` 字符串编码(UTF-8或GBK):设置字符串的编码格式,示例: `"utf-8"` ## 出口参数 编码后的内容:经过 Base64 编码后的数据,示例: `__INNER__["result"]`