当前位置:网站首页>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);
}
}
边栏推荐
- [RCTF2015]EasySQL
- guetzli简单使用
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- Re8: reading papers Hier spcnet: a legal stat hierarchy based heterogeneous network for computing legal case
- Alibaba Cloud Toolkit —— 项目一键部署工具
- Differences between the use of structs and classes
- 结构体和类使用的区别
- [physical simulation] the principle and practice of the simplest shape matching
- Comprehensively design an oppe homepage -- Design of star models
- movable-view 组件(可上下左右拖动 )
猜你喜欢

CAD进阶练习题(一)

MVC和ECS两种设计架构的初浅理解

微信小程序---网络数据请求

互联网协议

Nacos win10 安装配置教程

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
![[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)](/img/84/640de0bf779cd45498204909be56d1.png)
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)

My SQL is OK. Why is it still so slow? MySQL locking rules

vlang捣鼓之路

Simulation of three-phase voltage source inverter based on SISOTOOL pole assignment PI parameters and pless
随机推荐
Bugku login1
Class initialization mechanism of JVM
VS2017打开项目提示需要迁移的解决方法
我的sql没问题为什么还是这么慢|MySQL加锁规则
综合设计一个OPPE主页--顶部,头部的设计
Happy 10th birthday, clojure
From SiCp to LISP video replay
C # set different text watermarks for each page of word
Summary of key knowledge of C language
A firefox/chrome plug-in that visualizes browser history
修改mysql数据库root用户的密码
Vscode batch delete
Alibaba Cloud Toolkit —— 项目一键部署工具
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记3:对话和生成
A preliminary understanding of MVC and ECS design architectures
First knowledge of OpenGL (4) link shader
Nacos win10 installation and configuration tutorial
Trends in software testing tools in 2021
Bugku login2
Re9:读论文 DEAL Inductive Link Prediction for Nodes Having Only Attribute Information