当前位置:网站首页>Response响应字节数据
Response响应字节数据
2022-07-21 19:27:00 【华为云】
Response响应字节数据
响应字节数据首先需要获取到自己输出流
ServletOutputStream outputStream = resp.getOutputStream();
然后通过字节输出流写数据
outputStream.write(字节数据);
现在我们通过自己输出流来将一张图片传到浏览器
准备的一张图片
package com.itheima.web.response;import org.apache.commons.io.IOUtils;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.FileInputStream;import java.io.IOException;import java.io.PrintWriter;/** * 响应字节数据:设置字节数据的响应体 */@WebServlet("/resp4")public class ResponseDemo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 读取文件 FileInputStream fis = new FileInputStream("E:\\java_project__\\request-demo\\src\\main\\java\\com\\itheima\\web\\response\\be.png"); //2. 获取response字节输出流 ServletOutputStream os = response.getOutputStream(); //3. 完成流的copy /* byte[] buff = new byte[1024]; int len = 0; while ((len = fis.read(buff))!= -1){ os.write(buff,0,len); }*/ IOUtils.copy(fis,os); fis.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
==IOUtils.copy(fis,os);==该方法需要我们加入相关的依赖
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
边栏推荐
- 2022 latest Hubei construction eight members (Mechanics) simulated examination question bank and answers
- 动态新增、修改Logback的Appender(可实现动态调整日志级别,Appender参数)
- 光明正大的水贴来自考研人对暑假的感悟
- i18n 是什么,有什么作用
- How to use document tools for API management?
- day02-2
- Which is the best interface documentation tool at home and abroad?
- [cicadaplayer] vs2019 (v142) x64 ffmepg: error c2169: "lrintf": internal function, cannot be defined
- Android App test plan
- QT creator 8 release
猜你喜欢
std::thread 与类对象结合
Execution failed for task ‘:app:kaptDevDebugKotlin‘.
1307_ Summary of crystal oscillator test in embedded design
STD:: thread combine avec des objets de classe
Wallpaper background wall / avatar / dynamic wallpaper applet source code - support user submissions - with some collection functions + building tutorials
11-GuliMall 后台管理中商品系统的分类维护
C # implement setting expiration time for PDF documents
智能手机天线调谐
Laundry applet source code - add docking with third-party errands
leetcode刷题:动态规划02(爬楼梯)
随机推荐
Leetcode skimming: dynamic planning 02 (climbing stairs)
测试环境建设的基本原则
[cicadaplayer] vs2019 (v142) x64 ffmepg: error c2169: "lrintf": internal function, cannot be defined
Unity Scrollview scroll to the specified position
Live broadcast preview | new design ideas and production methods brought by NFT, aigc/ugc tools and metauniverse products
走出心理舒适区的七个最佳方法
Dof景深基础
Summary of some experiences in the process of R & D platform splitting
How to change the console font to console?
Execution failed for task ‘:app:kaptDevDebugKotlin‘.
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
梨花带雨音乐播放器3.91源码开源(网站添加背景音乐)
Request to obtain request line data and request header data
Urlparrern configuration
Request and response description
控制台字体怎么改为console?
[微信小程序开发者工具] × #initialize-error: Error: ENOENT: no such file or directory, open
Continuous improvement of software testing process
A new colleague from the company made typora perfect! Too tough
洗衣店小程序源码-增加对接第三方跑腿