Java对接模型流式接口,并流式输出
核心依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Demo代码
@GetMapping(value = "/test", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public DeferredResult<ResponseBodyEmitter> proxyModel22() throws IOException {
DeferredResult<ResponseBodyEmitter> deferredResult = new DeferredResult<>();
// 创建 URL
URL url = new URL("Third-party API");
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
connection.setDoOutput(true); // 设置为 POST 请求
connection.setDoInput(true); // 允许读取响应
// 构建请求体
String bodyJSON = "{\n" +
" \"input\": \"Who are you?\",\n" +
" \"user\": \"guest\",\n" +
" \"stream\": true,\n" +
" \"increase\": true,\n" +
" \"sessionId\": \"9xxxxxx\",\n" +
" \"history\": []\n" +
"}";
JSONObject jsonObject = JSON.parseObject(bodyJSON);
try (OutputStream os = connection.getOutputStream()) {
os.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8)); // 写入请求体
os.flush();
}
// 检查响应状态码
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
InputStream inputStream = connection.getInputStream();
Thread thread = new Thread(() -> {
byte[] buffer = new byte[4096]; // 缓冲区大小
int length;
try {
while ((length = inputStream.read(buffer)) != -1) {
// 只发送实际读取的数据长度
byte[] dataToSend = new byte[length];
System.arraycopy(buffer, 0, dataToSend, 0, length);
emitter.send(dataToSend);
}
inputStream.close();
emitter.complete(); // 完成发送
} catch (IOException e) {
log.error("错误信息为: " + e);
}
});
thread.start();
deferredResult.setResult(emitter);
} else {
// 如果第三方服务返回错误状态码
deferredResult.setErrorResult(new RuntimeException("Third-party API returned an error: " + responseCode));
}
return deferredResult;
}