Java接收text/event-stream格式數據的詳細代碼
java接收text/event-stream格式數據,并且解決接收HTTPS會不是流式輸出問題
前段時間因為要對接語音轉文字接口,對方接口輸出的是text/event-stream返回,返回的是流式輸出,本人在百度找了好久,一直沒有找到關于怎么接收流式返回的文章,可能很多人不清楚流式輸出指的是什么,流式輸出是和對方建立一個長連接,接口方會一直不斷的給我們推送數據,而不用等待對方接口完全輸出后在把返回值一次性返回。
先貼代碼
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 數據 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.關閉輸入流 is.close(); } catch (IOException e) { e.printStackTrace(); } } } return bu.toString(); }
這里的urlStr參數是url加參數,示例:https://baidu.com?text=12345678response是因為我需要同樣用流式輸出文字給前端,如果你不需要返回給前端,可以不用response參數。
post請求
public String postEventStream(String urlStr, String json, HttpServletResponse response) { long statr = System.currentTimeMillis(); log.info("開始請求接口url:{},請求參數{}", urlStr,json); InputStream is = null; //11.讀取輸入流中的返回值 StringBuffer bu = new StringBuffer(); try { //1.設置URL URL url = new URL(urlStr); //2.打開URL連接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //3.設置請求方式 conn.setRequestMethod("POST"); //4.設置Content-Type conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //5.設置Accept conn.setRequestProperty("Accept", "text/event-stream"); //6.設置DoOutput conn.setDoOutput(true); //7.設置DoInput conn.setDoInput(true); //8.獲取輸出流 OutputStream os = conn.getOutputStream(); //9.寫入參數(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:{},請求參數{},請求開始流式輸出{}", 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.關閉輸入流 is.close(); } catch (IOException e) { e.printStackTrace(); } } } return bu.toString(); }
第一次寫文章,表達不好請諒解,這里使用的jdk版本是1.8,如果對于springboot怎么樣返回給前端流式輸出有疑問,可以私信問我
到此這篇關于Java接收text/event-stream格式數據的詳細代碼的文章就介紹到這了,更多相關java接收text/event-stream格式數據內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot Security整合JWT授權RestAPI的實現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權RestAPI的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11springboot基于Mybatis mysql實現(xiàn)讀寫分離
這篇文章主要介紹了springboot基于Mybatis mysql實現(xiàn)讀寫分離,需要的朋友可以參考下2019-06-06已有的springcloud+mybatis項目升級為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項目升級為mybatis-plus,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03