From 6c6e61cb870f2741536082fbc20218d816ddac77 Mon Sep 17 00:00:00 2001 From: sh Date: Fri, 17 Apr 2026 11:26:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ai=E5=AF=B9=E8=AF=9D=E4=B8=AD?= =?UTF-8?q?(=E7=A9=BA=E5=8F=82=E8=80=83)=20=E6=BC=8F=E6=B4=9E=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E9=97=AE=E9=A2=98=E2=80=94=E2=80=94=E5=B7=B2=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/cms/config/ChatClient.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java index 53a09b8..bcea58d 100644 --- a/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java +++ b/ruoyi-bussiness/src/main/java/com/ruoyi/cms/config/ChatClient.java @@ -4,8 +4,8 @@ import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.cms.domain.chat.ChatRequest; import com.ruoyi.common.utils.StringUtils; -import lombok.var; import okhttp3.*; +import okio.BufferedSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -97,18 +97,34 @@ public class ChatClient { } // 逐行读取 SSE 格式的响应 - try (var bufferedSource = body.source()) { - //判断 bufferedSource - if (bufferedSource != null) { - return; - } + BufferedSource bufferedSource = body.source(); + if (bufferedSource == null) { + System.out.println("bufferedSource 为空,直接返回"); + return; + } + try { while (!bufferedSource.exhausted()) { String chunk = bufferedSource.readUtf8Line(); if (chunk != null && !chunk.trim().isEmpty()) { callback.onData(chunk); } } + } catch (IOException e) { + System.out.println("读取流异常"+ e); + } finally { + try { + bufferedSource.close(); + } catch (IOException e) { + } } + /*try (var bufferedSource = body.source()) { + while (!bufferedSource.exhausted()) { + String chunk = bufferedSource.readUtf8Line(); + if (chunk != null && !chunk.trim().isEmpty()) { + callback.onData(chunk); + } + } + }*/ // 通知流结束 callback.onComplete();