Java接收text/event-stream格式數(shù)據(jù)的詳細代碼
java接收text/event-stream格式數(shù)據(jù),并且解決接收HTTPS會不是流式輸出問題
前段時間因為要對接語音轉(zhuǎn)文字接口,對方接口輸出的是text/event-stream返回,返回的是流式輸出,本人在百度找了好久,一直沒有找到關(guān)于怎么接收流式返回的文章,可能很多人不清楚流式輸出指的是什么,流式輸出是和對方建立一個長連接,接口方會一直不斷的給我們推送數(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是因為我需要同樣用流式輸出文字給前端,如果你不需要返回給前端,可以不用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(); }
第一次寫文章,表達不好請諒解,這里使用的jdk版本是1.8,如果對于springboot怎么樣返回給前端流式輸出有疑問,可以私信問我
到此這篇關(guān)于Java接收text/event-stream格式數(shù)據(jù)的詳細代碼的文章就介紹到這了,更多相關(guān)java接收text/event-stream格式數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Security整合JWT授權(quán)RestAPI的實現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權(quán)RestAPI的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11springboot基于Mybatis mysql實現(xiàn)讀寫分離
這篇文章主要介紹了springboot基于Mybatis mysql實現(xiàn)讀寫分離,需要的朋友可以參考下2019-06-06Java實現(xiàn)調(diào)用MySQL存儲過程詳解
相信大家都知道存儲過程是在大型數(shù)據(jù)庫系統(tǒng)中,一組為了完成特定功能的SQL語句集。存儲過程是數(shù)據(jù)庫中的一個重要對象,任何一個設(shè)計良好的數(shù)據(jù)庫應用程序都應該用到存儲過程。Java調(diào)用mysql存儲過程,實現(xiàn)如下,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11已有的springcloud+mybatis項目升級為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項目升級為mybatis-plus,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03