response向客户端写入数据 水深无声 2023-08-17 16:10 82阅读 0赞 1、写入文字: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8");//设置服务器的编码,默认是ISO-8859-1 response.setContentType("text/html; charset = utf-8");//告诉浏览器服务器的编码格式 response.getWriter().write("你好,JAVA"); } ![1392562-20190908111620400-668075161.png][] 2、图片: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out=response.getOutputStream(); File file=new File("图片路径"); FileInputStream fi = new FileInputStream(file); byte[] buf = new byte[1024]; int len = 0; while ((len = fi.read(buf)) != -1) { out.write(buf, 0, len); // 将数组中的指定长度的数据写入到输出流中。 } fi.close(); out.close(); } 采用了缓冲数组读入图片数据,减少了时间的消耗。创建ServletOutputStream对象,将数据输出到了客户端。 ![1392562-20190908113400449-1742541602.png][] 转载于:https://www.cnblogs.com/zhai1997/p/11484889.html [1392562-20190908111620400-668075161.png]: /images/20230810/d3b98330be7c4ecf8e73c8eebe64d9c9.png [1392562-20190908113400449-1742541602.png]: /images/20230810/4aaaf725cc6a448497ae78b3faefb612.png
还没有评论,来说两句吧...