最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java流形式返回前端的實(shí)現(xiàn)示例

 更新時(shí)間:2023年04月20日 16:18:29   作者:蒼穹之躍  
? Java后端開發(fā)項(xiàng)目時(shí),需要給前端傳一些數(shù)據(jù),本文主要介紹了Java流形式返回前端的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

為了實(shí)現(xiàn)像ChatGPT一樣的效果:文字進(jìn)行逐個(gè)顯示,后端返回的時(shí)候需要以流的形式。

一、字符串流

    @PostMapping("returnStream")
    public void returnStream(HttpServletResponse response) throws IOException {
        String message = "我是一段等待已流形式返回的文字";
        // 以流的形式返回
        ServletOutputStream out = null;
        ByteArrayOutputStream baos = null;
        try {
            InputStream inStream = new ByteArrayInputStream(message.getBytes());
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Objects.requireNonNull(baos).flush();
            baos.close();
            Objects.requireNonNull(out).flush();
            out.close();
        }
 
    }

二、文件流

		ServletOutputStream out = null;
		ByteArrayOutputStream baos = null;
		try {
			File file=new File(filename);
			InputStream inStream=new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int len;
			baos = new ByteArrayOutputStream();
			while ((len = inStream.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			out = response.getOutputStream();
			out.write(baos.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			baos.flush();
			baos.close();
			out.flush();
			out.close();
		}

到此這篇關(guān)于Java流形式返回前端的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java流形式返回前端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

中西区| 泾阳县| 黑水县| 澳门| 彩票| 乐安县| 象州县| 日喀则市| 许昌县| 华安县| 如皋市| 崇明县| 怀安县| 和田县| 宝兴县| 滨州市| 玉林市| 扶沟县| 农安县| 红桥区| 贡嘎县| 上蔡县| 武安市| 长顺县| 福鼎市| 外汇| 佛冈县| 太湖县| 昌江| 宁阳县| 明溪县| 屏山县| 曲阳县| 衡南县| 九台市| 漯河市| 邻水| 疏勒县| 抚宁县| 平阴县| 昌宁县|