Java接收text/event-stream格式數(shù)據(jù)的詳細(xì)代碼
java接收text/event-stream格式數(shù)據(jù),并且解決接收HTTPS會不是流式輸出問題
前段時(shí)間因?yàn)橐獙诱Z音轉(zhuǎn)文字接口,對方接口輸出的是text/event-stream返回,返回的是流式輸出,本人在百度找了好久,一直沒有找到關(guān)于怎么接收流式返回的文章,可能很多人不清楚流式輸出指的是什么,流式輸出是和對方建立一個(gè)長連接,接口方會一直不斷的給我們推送數(shù)據(jù),而不用等待對方接口完全輸出后在把返回值一次性返回。
先貼代碼
get請求
public String getEventStream(String urlStr, HttpServletResponse response) {
long statr = System.currentTimeMillis();
log.info("開始請求接口url:{}", urlStr);
InputStream is = null;
StringBuffer bu = new StringBuffer();
try {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
is = conn.getInputStream();
byte[] b = new byte[1024];
int len = -1;
long end = System.currentTimeMillis();
log.info("接口url:{},請求開始流式輸出{}", urlStr, end - statr);
while ((len = is.read(b)) != -1) {
String line = new String(b, 0, len, "utf-8");
// 處理 event stream 數(shù)據(jù)
response.getWriter().write(line);
response.getWriter().flush();
bu.append(line);
}
} catch (IOException e) {
log.error("請求模型接口異常", e);
throw new BusinessException(ResponseCode.TOPIC_INITIATION_FAILED);
} finally {
if (!Objects.isNull(is)) {
try {
//12.關(guān)閉輸入流
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bu.toString();
}這里的urlStr參數(shù)是url加參數(shù),示例:https://baidu.com?text=12345678response是因?yàn)槲倚枰瑯佑昧魇捷敵鑫淖纸o前端,如果你不需要返回給前端,可以不用response參數(shù)。
post請求
public String postEventStream(String urlStr, String json, HttpServletResponse response) {
long statr = System.currentTimeMillis();
log.info("開始請求接口url:{},請求參數(shù){}", urlStr,json);
InputStream is = null;
//11.讀取輸入流中的返回值
StringBuffer bu = new StringBuffer();
try {
//1.設(shè)置URL
URL url = new URL(urlStr);
//2.打開URL連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//3.設(shè)置請求方式
conn.setRequestMethod("POST");
//4.設(shè)置Content-Type
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//5.設(shè)置Accept
conn.setRequestProperty("Accept", "text/event-stream");
//6.設(shè)置DoOutput
conn.setDoOutput(true);
//7.設(shè)置DoInput
conn.setDoInput(true);
//8.獲取輸出流
OutputStream os = conn.getOutputStream();
//9.寫入?yún)?shù)(json格式)
os.write(json.getBytes("utf-8"));
os.flush();
os.close();
//10.獲取輸入流
is = conn.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
long end = System.currentTimeMillis();
log.info("接口url:{},請求參數(shù){},請求開始流式輸出{}", urlStr,json, end - statr);
while ((len = is.read(bytes)) != -1) {
String line = new String(bytes, 0, len, "utf-8");
response.getWriter().write(line);
response.getWriter().flush();
bu.append(line);
}
} catch (IOException e) {
log.error("請求模型接口異常", e);
throw new BusinessException(ResponseCode.TOPIC_INITIATION_FAILED);
} finally {
if (!Objects.isNull(is)) {
try {
//12.關(guān)閉輸入流
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bu.toString();
}第一次寫文章,表達(dá)不好請諒解,這里使用的jdk版本是1.8,如果對于springboot怎么樣返回給前端流式輸出有疑問,可以私信問我
到此這篇關(guān)于Java接收text/event-stream格式數(shù)據(jù)的詳細(xì)代碼的文章就介紹到這了,更多相關(guān)java接收text/event-stream格式數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
springboot基于Mybatis mysql實(shí)現(xiàn)讀寫分離
這篇文章主要介紹了springboot基于Mybatis mysql實(shí)現(xiàn)讀寫分離,需要的朋友可以參考下2019-06-06
java web實(shí)現(xiàn)用戶權(quán)限管理
這篇文章主要介紹了java web實(shí)現(xiàn)用戶權(quán)限管理,設(shè)計(jì)并實(shí)現(xiàn)一套簡單的權(quán)限管理功能,感興趣的小伙伴們可以參考一下2015-11-11
Java實(shí)現(xiàn)調(diào)用MySQL存儲過程詳解
相信大家都知道存儲過程是在大型數(shù)據(jù)庫系統(tǒng)中,一組為了完成特定功能的SQL語句集。存儲過程是數(shù)據(jù)庫中的一個(gè)重要對象,任何一個(gè)設(shè)計(jì)良好的數(shù)據(jù)庫應(yīng)用程序都應(yīng)該用到存儲過程。Java調(diào)用mysql存儲過程,實(shí)現(xiàn)如下,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11
已有的springcloud+mybatis項(xiàng)目升級為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項(xiàng)目升級為mybatis-plus,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

