diff --git a/gradle.properties b/gradle.properties index 77d04cae..efd66953 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,8 +6,8 @@ minecraft_version=1.19.3 loader_version=0.14.13 # Mod Properties version_prefix=1.19.3 -mod_version=2.1.4 -client_version=0.3.2-light +mod_version=2.1.5 +client_version=0.3.3-light jankson_version=1.2.1 parchement_version=2022.12.18 maven_group=cn.evolvefield.mods diff --git a/src/main/java/cn/evolvefield/mods/botapi/BotApi.java b/src/main/java/cn/evolvefield/mods/botapi/BotApi.java index 67076cdf..e54b12a1 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/BotApi.java +++ b/src/main/java/cn/evolvefield/mods/botapi/BotApi.java @@ -13,7 +13,6 @@ import java.io.File; import java.nio.file.Path; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; @@ -34,8 +33,8 @@ public class BotApi implements ModInitializer { public static ConnectFactory service; public static EventBus dispatchers; public static Bot bot; - public static ExecutorService app = Executors.newFixedThreadPool(1); - ; + public static Thread app; + public BotApi() { @@ -71,10 +70,11 @@ private void onServerStarted(MinecraftServer server) { blockingQueue = new LinkedBlockingQueue<>();//使用队列传输数据 if (ConfigHandler.cached().getCommon().isAutoOpen()) { try { - app.submit(() -> { + app = new Thread(() -> { service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), blockingQueue);//创建websocket连接 bot = service.ws.createBot();//创建机器人实例 - }); + }, "BotServer"); + app.start(); } catch (Exception e) { Const.LOGGER.error("§c机器人服务端未配置或未打开"); } @@ -89,7 +89,7 @@ private void onServerStopping(MinecraftServer server) { Const.LOGGER.info("▌ §c正在关闭群服互联 §a┈━═☆"); dispatchers.stop();//分发器关闭 service.stop(); - app.shutdownNow(); + app.interrupt(); } diff --git a/src/main/java/cn/evolvefield/mods/botapi/Const.java b/src/main/java/cn/evolvefield/mods/botapi/Const.java index 06c5d651..b5911f20 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/Const.java +++ b/src/main/java/cn/evolvefield/mods/botapi/Const.java @@ -11,7 +11,7 @@ */ public class Const { public static final String MODID = "botapi"; - public static final Logger LOGGER = LogManager.getLogger(MODID); + public static final Logger LOGGER = LogManager.getLogger("BotConnect"); public static boolean isShutdown = false; diff --git a/src/main/java/cn/evolvefield/mods/botapi/api/cmd/CmdApi.java b/src/main/java/cn/evolvefield/mods/botapi/api/cmd/CmdApi.java index 387642af..ef33f3d8 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/api/cmd/CmdApi.java +++ b/src/main/java/cn/evolvefield/mods/botapi/api/cmd/CmdApi.java @@ -1,7 +1,6 @@ package cn.evolvefield.mods.botapi.api.cmd; import cn.evolvefield.mods.botapi.BotApi; -import cn.evolvefield.mods.botapi.Const; import cn.evolvefield.mods.botapi.init.handler.CustomCmdHandler; import cn.evolvefield.mods.botapi.util.onebot.BotUtils; import cn.evolvefield.onebot.client.core.Bot; @@ -37,8 +36,7 @@ private static void GuildCmd(Bot bot, String guildId, String channelId, String c public static void invokeCommandGroup(GroupMessageEvent event) { String commandHead = event.getMessage().split(" ")[0].substring(1); String command = event.getMessage().substring(1);//去除前缀 - Const.LOGGER.info(event); - Const.LOGGER.info(BotUtils.groupAdminParse(event)); + if (BotUtils.groupAdminParse(event)) { CustomCmdHandler.INSTANCE.getCustomCmds().stream() .filter(customCmd -> customCmd.getCmdAlies().equals(commandHead)) diff --git a/src/main/java/cn/evolvefield/mods/botapi/api/listener/GroupCmdListener.java b/src/main/java/cn/evolvefield/mods/botapi/api/listener/GroupCmdEventListener.java similarity index 72% rename from src/main/java/cn/evolvefield/mods/botapi/api/listener/GroupCmdListener.java rename to src/main/java/cn/evolvefield/mods/botapi/api/listener/GroupCmdEventListener.java index 09e6e000..15c323a3 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/api/listener/GroupCmdListener.java +++ b/src/main/java/cn/evolvefield/mods/botapi/api/listener/GroupCmdEventListener.java @@ -1,7 +1,7 @@ package cn.evolvefield.mods.botapi.api.listener; import cn.evolvefield.mods.botapi.api.cmd.CmdApi; -import cn.evolvefield.onebot.client.listener.DefaultHandlerListener; +import cn.evolvefield.onebot.client.handler.DefaultEventHandler; import cn.evolvefield.onebot.sdk.event.message.GroupMessageEvent; @@ -11,7 +11,7 @@ * Date: 2022/10/2 15:58 * Version: 1.0 */ -public class GroupCmdListener extends DefaultHandlerListener { +public class GroupCmdEventListener extends DefaultEventHandler { @Override public void onMessage(GroupMessageEvent event) { //处理逻辑 diff --git a/src/main/java/cn/evolvefield/mods/botapi/common/command/ConnectCommand.java b/src/main/java/cn/evolvefield/mods/botapi/common/command/ConnectCommand.java index 058130b9..31c17004 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/common/command/ConnectCommand.java +++ b/src/main/java/cn/evolvefield/mods/botapi/common/command/ConnectCommand.java @@ -28,10 +28,10 @@ public static int cqhttpExecute(CommandContext context) thro ConfigHandler.cached().getBotConfig().setMiraiHttp(false); try { - BotApi.app.submit(() -> { + BotApi.app = new Thread(() -> { BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接 BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例 - }); + }, "BotServer"); } catch (Exception e) { Const.LOGGER.error("§c机器人服务端配置不正确"); } @@ -56,10 +56,10 @@ public static int miraiExecute(CommandContext context) throw context.getSource().sendSuccess(Component.literal("尝试链接框架" + ChatFormatting.LIGHT_PURPLE + "mirai"), true); ConfigHandler.cached().getBotConfig().setMiraiHttp(true); try { - BotApi.app.submit(() -> { + BotApi.app = new Thread(() -> { BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接 BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例 - }); + }, "BotServer"); } catch (Exception e) { Const.LOGGER.error("§c机器人服务端配置不正确"); } @@ -79,10 +79,10 @@ public static int cqhttpCommonExecute(CommandContext context context.getSource().sendSuccess(Component.literal("尝试链接框架" + ChatFormatting.LIGHT_PURPLE + "cqhttp"), true); ConfigHandler.cached().getBotConfig().setMiraiHttp(false); try { - BotApi.app.submit(() -> { - BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接 - BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例 - }); + BotApi.app = new Thread(() -> { + BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接 + BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例 + }, "BotServer"); } catch (Exception e) { Const.LOGGER.error("§c机器人服务端配置不正确"); } @@ -99,10 +99,10 @@ public static int miraiCommonExecute(CommandContext context) context.getSource().sendSuccess(Component.literal("尝试链接框架" + ChatFormatting.LIGHT_PURPLE + "mirai"), true); ConfigHandler.cached().getBotConfig().setMiraiHttp(true); try { - BotApi.app.submit(() -> { - BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接 - BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例 - }); + BotApi.app = new Thread(() -> { + BotApi.service = new ConnectFactory(ConfigHandler.cached().getBotConfig(), BotApi.blockingQueue);//创建websocket连接 + BotApi.bot = BotApi.service.ws.createBot();//创建机器人实例 + }, "BotServer"); } catch (Exception e) { Const.LOGGER.error("§c机器人服务端配置不正确"); } diff --git a/src/main/java/cn/evolvefield/mods/botapi/init/handler/BotEventHandler.java b/src/main/java/cn/evolvefield/mods/botapi/init/handler/BotEventHandler.java index fa5d53ee..a9d2736a 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/init/handler/BotEventHandler.java +++ b/src/main/java/cn/evolvefield/mods/botapi/init/handler/BotEventHandler.java @@ -4,11 +4,13 @@ import cn.evolvefield.mods.botapi.api.cmd.CmdApi; import cn.evolvefield.mods.botapi.util.onebot.CQUtils; import cn.evolvefield.onebot.client.handler.EventBus; -import cn.evolvefield.onebot.client.listener.SimpleListener; +import cn.evolvefield.onebot.client.listener.SimpleEventListener; import cn.evolvefield.onebot.sdk.event.message.GroupMessageEvent; import cn.evolvefield.onebot.sdk.event.message.GuildMessageEvent; +import cn.evolvefield.onebot.sdk.event.meta.LifecycleMetaEvent; import cn.evolvefield.onebot.sdk.event.notice.group.GroupDecreaseNoticeEvent; import cn.evolvefield.onebot.sdk.event.notice.group.GroupIncreaseNoticeEvent; +import lombok.val; /** * Description: @@ -21,21 +23,21 @@ public static void init(EventBus dispatchers) { GroupChatHandler(dispatchers); GroupCmdsHandler(dispatchers); - GroupMemberNotice(dispatchers); + GroupNoticeHandler(dispatchers); GuildChatHandler(dispatchers); GuildCmdsHandler(dispatchers); - dispatchers.start(4);//线程组处理任务 + LifeCycleHandler(dispatchers); } private static void GroupChatHandler(EventBus dispatchers) { - dispatchers.addListener(new SimpleListener() { + dispatchers.addListener(new SimpleEventListener() { @Override public void onMessage(GroupMessageEvent event) { if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId())//判断是否是配置中的群 && !event.getMessage().startsWith(ConfigHandler.cached().getCmd().getCommandStart())//过滤命令前缀 && ConfigHandler.cached().getStatus().isRECEIVE_ENABLED()//总接受开关 && ConfigHandler.cached().getStatus().isR_CHAT_ENABLE()//接受聊天开关 - && event.getUserId() != ConfigHandler.cached().getCommon().getBotId() + && event.getUserId() != ConfigHandler.cached().getCommon().getBotId()//过滤机器人 ) { String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片 @@ -48,15 +50,13 @@ public void onMessage(GroupMessageEvent event) { } String toSend = String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ConfigHandler.cached().getCmd().getQqPrefix(), event.getGroupId(), event.getSender().getNickname(), send); TickEventHandler.getToSendQueue().add(toSend); - - } } }); } private static void GroupCmdsHandler(EventBus dispatchers) { - dispatchers.addListener(new SimpleListener() { + dispatchers.addListener(new SimpleEventListener() { @Override public void onMessage(GroupMessageEvent event) { if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId()) @@ -70,8 +70,8 @@ public void onMessage(GroupMessageEvent event) { }); } - private static void GroupMemberNotice(EventBus dispatchers) { - dispatchers.addListener(new SimpleListener() { + private static void GroupNoticeHandler(EventBus dispatchers) { + dispatchers.addListener(new SimpleEventListener() { @Override public void onMessage(GroupDecreaseNoticeEvent event) { if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId()) @@ -82,7 +82,7 @@ public void onMessage(GroupDecreaseNoticeEvent event) { } }); - dispatchers.addListener(new SimpleListener() { + dispatchers.addListener(new SimpleEventListener() { @Override public void onMessage(GroupIncreaseNoticeEvent event) { if (ConfigHandler.cached().getCommon().getGroupIdList().contains(event.getGroupId()) @@ -96,7 +96,7 @@ public void onMessage(GroupIncreaseNoticeEvent event) { } private static void GuildChatHandler(EventBus dispatchers) { - dispatchers.addListener(new SimpleListener() { + dispatchers.addListener(new SimpleEventListener() { @Override public void onMessage(GuildMessageEvent event) { if (event.getGuildId().equals(ConfigHandler.cached().getCommon().getGuildId()) @@ -123,7 +123,7 @@ public void onMessage(GuildMessageEvent event) { } private static void GuildCmdsHandler(EventBus dispatchers) { - dispatchers.addListener(new SimpleListener() { + dispatchers.addListener(new SimpleEventListener() { @Override public void onMessage(GuildMessageEvent event) { if (ConfigHandler.cached().getCommon().getChannelIdList().contains(event.getChannelId()) @@ -138,4 +138,26 @@ public void onMessage(GuildMessageEvent event) { } + private static void LifeCycleHandler(EventBus dispatchers) { + dispatchers.addListener(new SimpleEventListener() { + @Override + public void onMessage(LifecycleMetaEvent event) { + if (!event.getSubType().equals("connect")) return; + if (!ConfigHandler.cached().getCommon().getGroupIdList().isEmpty() + ) { + for (val id : ConfigHandler.cached().getCommon().getGroupIdList()){ + BotApi.bot.sendGroupMsg(id, "▌ 群服互联已连接 ┈━═☆", true); + } + } + if (!ConfigHandler.cached().getCommon().getChannelIdList().isEmpty() + ) { + for (val id : ConfigHandler.cached().getCommon().getChannelIdList()){ + BotApi.bot.sendGuildMsg(ConfigHandler.cached().getCommon().getGuildId() , id, "▌ 群服互联已连接 ┈━═☆"); + } + } + } + }); + } + + } diff --git a/src/main/java/cn/evolvefield/mods/botapi/util/locale/Translation.java b/src/main/java/cn/evolvefield/mods/botapi/util/locale/Translation.java index 147c9d98..1484d8a4 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/util/locale/Translation.java +++ b/src/main/java/cn/evolvefield/mods/botapi/util/locale/Translation.java @@ -28,7 +28,7 @@ public abstract class Translation { private static final Gson GSON = new Gson(); private static final Pattern UNSUPPORTED_FORMAT_PATTERN = Pattern.compile("%(\\d+\\$)?[\\d.]*[df]"); - public static final String DEFAULT_LANGUAGE = "en_us"; + public static final String DEFAULT_LANGUAGE = "zh_cn"; private static volatile Translation instance = loadDefault(ConfigHandler.cached().getCommon().getLanguageSelect()); public Translation() { @@ -44,7 +44,7 @@ private static Translation loadDefault(String langId) { InputStream inputStream = Translation.class.getResourceAsStream(resourceLocation); if (inputStream == null) { - Const.LOGGER.info(String.format("No BotApi lang file for the language '%s' found. Make it to 'en_us' by default.", langId)); + Const.LOGGER.info(String.format("'%s'的语言文件未找到,将使用默认zh_cn", langId)); inputStream = I18n.class.getResourceAsStream(String.format(resourceFString, DEFAULT_LANGUAGE)); } diff --git a/src/main/java/cn/evolvefield/mods/botapi/util/onebot/BotUtils.java b/src/main/java/cn/evolvefield/mods/botapi/util/onebot/BotUtils.java index 3241b393..8d947bcd 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/util/onebot/BotUtils.java +++ b/src/main/java/cn/evolvefield/mods/botapi/util/onebot/BotUtils.java @@ -6,6 +6,11 @@ import cn.evolvefield.onebot.sdk.event.message.GroupMessageEvent; import cn.evolvefield.onebot.sdk.event.message.GuildMessageEvent; import cn.evolvefield.onebot.sdk.event.message.MessageEvent; +import lombok.AllArgsConstructor; +import lombok.Data; +import net.minecraft.ChatFormatting; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; import java.util.Arrays; import java.util.concurrent.atomic.AtomicBoolean; @@ -91,4 +96,37 @@ private static int getSubStr(String str) { } + public static MutableComponent msgBuild(UserMessage msg){ + // 拼接要发送对消息 + return Component.literal(ChatFormatting.DARK_GREEN + "[") + .append(ChatFormatting.AQUA + msg.getPlatform()) + .append(ChatFormatting.WHITE + "(") + .append(ChatFormatting.DARK_PURPLE + msg.getId()) + .append(ChatFormatting.WHITE + ")") + .append(ChatFormatting.DARK_GREEN + "]") + .append(ChatFormatting.GOLD + " <" + msg.getName() + "> ") + .append(ChatFormatting.WHITE + msg.getMessage() + " ") + ; + } + + @Data + @AllArgsConstructor + public static class UserMessage { + private String platform; + private String id; + private String name; + private String message; + + @Override + public String toString() { + return "UserMessage{" + + "platform='" + platform + '\'' + + "id='" + id + '\'' + + "name='" + name + '\'' + + ", message='" + message + '\'' + + '}'; + } + + } + } diff --git a/src/main/java/cn/evolvefield/mods/botapi/util/onebot/CQUtils.java b/src/main/java/cn/evolvefield/mods/botapi/util/onebot/CQUtils.java index 6e64ba8d..76d75666 100644 --- a/src/main/java/cn/evolvefield/mods/botapi/util/onebot/CQUtils.java +++ b/src/main/java/cn/evolvefield/mods/botapi/util/onebot/CQUtils.java @@ -2,9 +2,10 @@ import cn.evolvefield.mods.botapi.init.handler.ConfigHandler; import cn.evolvefield.onebot.sdk.util.BotUtils; -import cn.evolvefield.onebot.sdk.util.RegexUtils; +import lombok.val; import java.util.Arrays; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -17,52 +18,76 @@ public class CQUtils { private final static String CQ_CODE_SPLIT = "(?<=\\[CQ:[^]]{1,99999}])|(?=\\[CQ:[^]]{1,99999}])"; - private final static String CQ_CODE_REGEX = "\\[CQ:([^,\\[\\]]+)((?:,[^,=\\[\\]]+=[^,\\[\\]]*)*)]"; + private final static String CQ_CODE_REGEX = "\\[CQ:(.*?),(.*?)]"; public static boolean hasImg(String msg) { String regex = "\\[CQ:image,[(\\s\\S)]*\\]"; - var p = Pattern.compile(regex); - var m = p.matcher(msg); + val p = Pattern.compile(regex); + val m = p.matcher(msg); return m.find(); } public static String replace(String msg) { if (msg.indexOf('[') == -1) return BotUtils.unescape(msg); - var message = new StringBuilder(); - var matcher = RegexUtils.regexMatcher(CQ_CODE_REGEX, msg); - while (matcher.find()) { - var type = matcher.group(1); - var replacement = switch (type) { - case "image" -> { + String message = ""; + Pattern pattern = Pattern.compile(CQ_CODE_REGEX); + Matcher matcher = pattern.matcher(msg); + while (matcher.find()) {//全局匹配 + val type = matcher.group(1); + val data = matcher.group(2); + switch (type) { + case "image": { if (ConfigHandler.cached().getCommon().isImageOn()) { - var url = Arrays.stream(matcher.group(2).split(","))//具体数据分割 + val url = Arrays.stream(data.split(","))//具体数据分割 .filter(it -> it.startsWith("url"))//非空判断 - .map(it -> it.substring(it.indexOf('=')) + 1) + .map(it -> it.substring(it.indexOf('=') + 1)) .findFirst(); - if (url.isPresent()) - yield String.format("[[CICode,url=%s,name=来自QQ的图片]]", url.get()); - else - yield "[图片]"; + if (url.isPresent()) { + message = matcher.replaceFirst(String.format("[[CICode,url=%s,name=来自QQ的图片]]", url.get())); + } else { + message = matcher.replaceFirst("[图片]"); + } + } else { + message = matcher.replaceFirst("[图片]"); } - else - yield "[图片]"; + break; } - case "reply" -> "[回复]"; - case "at" -> "[@]"; - case "record" -> "[语音]"; - case "forward" -> "[合并转发]"; - case "video" -> "[视频]"; - case "music" -> "[音乐]"; - case "redbag" -> "[红包]"; - case "poke" -> "[戳一戳]"; - case "face" -> "[表情]"; - default -> "[?]"; - }; - matcher.appendReplacement(message, replacement); + case "at": + val id = Arrays.stream(data.split(","))//具体数据分割 + .filter(it -> it.startsWith("qq"))//非空判断 + .map(it -> it.substring(it.indexOf('=') + 1)) + .findFirst(); + if (id.isPresent()) { + message = matcher.replaceFirst(String.format("@%s", id.get())); + } else { + message = matcher.replaceFirst("[@]"); + } + break; + case "record": + message = matcher.replaceFirst("[语音]"); + break; + case "forward": + message = matcher.replaceFirst("[合并转发]"); + break; + case "video": + message = matcher.replaceFirst("[视频]"); + break; + case "music": + message = matcher.replaceFirst("[音乐]"); + break; + case "redbag": + message = matcher.replaceFirst("[红包]"); + break; + case "face": + message = matcher.replaceFirst("[表情]"); + break; + default: + message = matcher.replaceFirst("[?]"); + break; + } } - matcher.appendTail(message); - return BotUtils.unescape(message.toString()); + return message; } } diff --git a/src/main/java/cn/evolvefield/mods/botapi/util/onebot/ImgUtil.java b/src/main/java/cn/evolvefield/mods/botapi/util/onebot/ImgUtil.java new file mode 100644 index 00000000..f51e5a51 --- /dev/null +++ b/src/main/java/cn/evolvefield/mods/botapi/util/onebot/ImgUtil.java @@ -0,0 +1,90 @@ +package cn.evolvefield.mods.botapi.util.onebot; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Base64; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Project: Bot-Connect-Forge + * Author: cnlimiter + * Date: 2023/4/19 20:45 + * Description: + */ +public class ImgUtil { + /** + * 图片URL转Base64编码 + * @param imgUrl 图片URL + * @return Base64编码 + */ + public static String urlToBase64(String imgUrl) { + URL url = null; + InputStream is = null; + ByteArrayOutputStream outStream = null; + HttpURLConnection httpUrl = null; + + try { + url = new URL(imgUrl); + httpUrl = (HttpURLConnection) url.openConnection(); + httpUrl.connect(); + httpUrl.getInputStream(); + + is = httpUrl.getInputStream(); + outStream = new ByteArrayOutputStream(); + + //创建一个Buffer字符串 + byte[] buffer = new byte[1024]; + //每次读取的字符串长度,如果为-1,代表全部读取完毕 + int len = 0; + //使用输入流从buffer里把数据读取出来 + while( (len = is.read(buffer)) != -1 ){ + //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 + outStream.write(buffer, 0, len); + } + // 对字节数组Base64编码 + return encode(outStream.toByteArray()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if(is != null) { + is.close(); + } + if(outStream != null) { + outStream.close(); + } + if(httpUrl != null) { + httpUrl.disconnect(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + return null; + } + + /** + * 图片转字符串 + * @param image 图片Buffer + * @return Base64编码 + */ + public static String encode(byte[] image){ + return replaceEnter(Base64.getEncoder().encodeToString(image)); + } + + /** + * 字符替换 + * @param str 字符串 + * @return 替换后的字符串 + */ + public static String replaceEnter(String str){ + String reg ="[\n-\r]"; + Pattern p = Pattern.compile(reg); + Matcher m = p.matcher(str); + return m.replaceAll(""); + } +}