当前位置:网站首页>Response对象-响应字符数据
Response对象-响应字符数据
2022-07-26 16:24:00 【陈毓辰】
Response响应数据
1. response将字符数据返回到浏览器
需要两步:
- 通过response对象获取字符输出流PrintWriter writer = resp.getWriter();
- 通过字符输出流写数据:writer.writer(“aaa”);
案例
浏览器中打印出字符
/** * 响应字符数据:设置字符数据的响应体 */
@WebServlet("/resp3")
public class ResponseDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1. 获取字符输出流
PrintWriter writer = response.getWriter();
writer.write("aaa");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
还可以返回html字符串,并且浏览器能解析出来,如下:
PrintWriter writer = response.getWriter();
//content-type,告诉浏览器返回的数据类型是HTML类型数据,这样浏览器才会解析HTML标签
response.setHeader("content-type","text/html");
writer.write("<h1>aaa</h1>");
注意: 一次请求响应结束后,response对象就会被销毁,所以不要手动关闭流
提醒 如果要返回一个中文字符串你好,需要设置响应数据编码为utf-8
//设置响应的数据格式及数据的编码
response.setContentType("text/html;charset=utf-8");
writer.write("你好");
2. response响应字节数据
需要将字节数据写回到浏览器,我们需要两个步骤:
- 通过response对象获取字节输出流:servletOutputStream outputStream = resp.getOutputStream();
- 通过字节输出流写数据:outputStream。write(字节数据);
1.返回一个图片文件到浏览器,如下:
/** * 响应字节数据:设置字节数据的响应体 */
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 读取文件
FileInputStream fis = new FileInputStream("d://a.jpg");
//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);
}
fis.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
上述代码中,对于流的copy的代码还是比较复杂的,所以我们可以使用别人提供好的方法来简化代码的开发,具体的步骤如下:
- pom.xml添加依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
- 调用工具类方法
//fis:输入流
//os:输出流
IOUtils.copy(fis,os);
优化代码之后:
/** * 响应字节数据:设置字节数据的响应体 */
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 读取文件
FileInputStream fis = new FileInputStream("d://a.jpg");
//2. 获取response字节输出流
ServletOutputStream os = response.getOutputStream();
//3. 完成流的copy
IOUtils.copy(fis,os);
fis.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
边栏推荐
- Selection of industrial serial port to WiFi port to Ethernet module of Internet of things
- The difference and efficiency comparison of three methods of C # conversion integer
- PAT甲级 1047 Student List for Course
- C#事件和委托的区别
- 我的sql没问题为什么还是这么慢|MySQL加锁规则
- [BJDCTF2020]Easy MD5
- Alibaba Cloud Toolkit —— 项目一键部署工具
- IDEA 阿里云多模块部署
- Why is digital transformation so difficult?!
- PAT甲级 1046 Shortest Distance
猜你喜欢

Simulation of three-phase voltage source inverter based on SISOTOOL pole assignment PI parameters and pless

Botu PLC Sequential switch function block (SCL)

FTP protocol
![[BJDCTF2020]Easy MD5](/img/6a/61a4b5624c33f1f334bea344cfa2c8.png)
[BJDCTF2020]Easy MD5

Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform

导数、微分、偏导数、全微分、方向导数、梯度的定义与关系

ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记3:对话和生成

Wechat applet - network data request
![[RCTF2015]EasySQL](/img/68/328ee5cffc8b267b6b0f284eb8db2c.png)
[RCTF2015]EasySQL

Nacos win10 installation and configuration tutorial
随机推荐
SQL statement -- single line comment and multi line comment
Internet Protocol
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记3:对话和生成
Configmap of kubernetes
Comprehensively design an oppe homepage -- the design of the top and head
RE9: read the paper deal inductive link prediction for nodes having only attribute information
2022 Niuke summer multi school training camp 1 (acdgij)
IDEA 阿里云多模块部署
什么是分布式定时任务框架?
Linux安装mysql8.0.29详细教程
Advanced CAD exercises (I)
NUC 11构建 ESXi 7.0.3f安装网卡驱动-V2(2022年7月升级版)
Pat class a 1047 student list for course
Sword finger offer special assault edition day 11
[RCTF2015]EasySQL
Comprehensively design an oppe homepage -- Design of star models
Re9:读论文 DEAL Inductive Link Prediction for Nodes Having Only Attribute Information
NUC 11 build esxi 7.0.3f install network card driver-v2 (upgraded version in July 2022)
The difference between anonymous methods and lambda expressions
Wechat applet - network data request