HttpServletResponse相关
目录
返回给客户端数据:字节流和字符流
package com.zhujunwei.httpServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpServletResponse01 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/**
* 返回给客户端数据两种方式:字节流和字符流方式
* 一次只能执行其中的一个
*/
// byWriter(resp);
byOutputStream(resp);
//设置当前这个请求的处理状态码
// resp.setStatus(sc);
//设置一个头
// resp.setHeader(name, value);
//设置相应的类型,以及编码
// resp.setContentType(type);
}
/**
* 以字节流的方式写数据
* @param resp
* @throws IOException
*/
private void byOutputStream(HttpServletResponse resp) throws IOException {
resp.getOutputStream().write("getOutputStream().write()".getBytes());
}
/**
* 以字符流的方式写数据
* @param resp
* @throws IOException
*/
@SuppressWarnings("unused")
private void byWriter(HttpServletResponse resp) throws IOException {
resp.getWriter().write("<h1>getWriter().write()</h1><br>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果:
getOutputStream().write()
解决返回字节流和字符流乱码问题
package com.zhujunwei.httpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpServletResponse02 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//字符流输出
// writerEncoding(resp);
//字节流输出
outPutStreamEncoding(resp);
}
/**
* 解决字节流输出乱码
* @param resp
* @throws IOException
* @throws UnsupportedEncodingException
*/
private void outPutStreamEncoding(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {
//1、指定浏览器看这份数据使用的码表
resp.setHeader("Content-Type", "text/html; charset=UTF-8");
//2、指定输出的中文用的码表
resp.getOutputStream().write("我是最帅的。。".getBytes());
}
/**
* 解决字符流输出乱码
* @param resp
* @throws IOException
*/
@SuppressWarnings("unused")
private void writerEncoding(HttpServletResponse resp) throws IOException {
//这里写出去的文字,默认使用的是ISO-8859-1,我们可以指定写出去的时候,使用什么编码写
//1、指定输出到客户端的时候,这些文字使用UTF-8
resp.setCharacterEncoding("UTF-8");
//2、直接规定浏览器看这份数据的时候,使用什么编码看
resp.setHeader("Content-Type", "text/html; charset=UTF-8");
resp.getWriter().write("我是最帅的。。。");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果:
我是最帅的。。
万能解决乱码
package com.zhujunwei.httpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HttpServletResponse03 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//只需要设置这一行就可以不管是字节或者字符流都不会乱码
resp.setContentType("text/html; charset=UTF-8");
//字符流输出
writerEncoding(resp);
//字节流输出
// outPutStreamEncoding(resp);
}
/**
* 解决字节流输出乱码
* @param resp
* @throws IOException
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unused")
private void outPutStreamEncoding(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {
resp.getOutputStream().write("我真的是最帅的。。".getBytes());
}
/**
* 解决字符流输出乱码
* @param resp
* @throws IOException
*/
private void writerEncoding(HttpServletResponse resp) throws IOException {
resp.getWriter().write("我真的是最帅的。。。");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果
我真的是最帅的。。。
文件下载
tomcat提供的默认下载方式
Tomcat提供的有文件下在的默认Servlet,如果浏览器能打开这种文件的话会自动打开,不会弹出下载框。
我们在项目里面放置几个文件供测试使用

download.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>tomcat提供的默认下载方式</h2>
<a href="download/test.txt">test.txt</a><br>
<a href="download/m1.jpg">m1.jpg</a><br>
<a href="download/download.rar">download.rar</a><br>
<hr>
<h2>手动写下载方法</h2>
<a href="DownloadServlet?fileName=test.txt">test.txt</a><br>
<a href="DownloadServlet?fileName=m1.jpg">m1.jpg</a><br>
<a href="DownloadServlet?fileName=download.rar">download.rar</a><br>
</body>
</html>
手动编写的DownloadServlet
package com.zhujunwei.download;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1、获取要下载的文件名字
String fileName = req.getParameter("fileName");
//2、获取这个文件在tomcat里面的绝对路径地址
String path = getServletContext().getRealPath("download/"+fileName);
//让浏览器收到这份资源的时候,以下载的方式提醒用户,而不是直接展示
resp.setHeader("Content-Disposition", "attachment; filename="+fileName);
//3、转换成输入流
InputStream is = new FileInputStream(path);
OutputStream os = resp.getOutputStream();
int len = 0 ;
byte[] buffer = new byte[1024];
while((len = is.read(buffer))!=-1) {
os.write(buffer,0,len);
}
os.close();
is.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结果:

HttpServletResponse相关的更多相关文章
- servlet实现文件下载所需步骤及说明
servlet实现文件下载所需步骤及说明 CreateTime--2017年9月1日15:46:22 Author:Marydon 参考链接:http://blog.sina.com.cn/s/b ...
- POI生成Excel工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInp ...
- 嵌入式单片机STM32应用技术(课本)
目录SAIU R20 1 6 第1页第1 章. 初识STM32..................................................................... ...
- java web学习总结(七) -------------------HttpServletResponse对象(一)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- Struts2相关面试题
Struts2面试题 1.struts2工作流程 Struts 2框架本身大致可以分为3个部分: 核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心 ...
- spring相关jar包的含义
spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...
- Spring 相关jar包详细介绍
文章转自:http://blog.csdn.net/farawayhome/article/details/6623946 aspectj目录下是在Spring框架下使用aspectj的源代码和测试程 ...
- JAVAWEB学习总结 HttpServletResponse对象(一)
Web服务器收到客户端(浏览器)的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象. request和response对象既然代表请求和响 ...
- HttpServletResponse对象 学习
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- Servlet/JSP-02 Servlet相关类
ServletConfig / ServletContext / ServletRequest / ServletResponse 一. ServletConfig:封装了Servlet得配置信息,并 ...
随机推荐
- 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-13- iframe操作-中篇(详细教程)
1.简介 按照计划今天就要用实际的例子进行iframe自动化测试.宏哥还是用之前找到的一个含有iframe的网页(QQ邮箱和163邮箱),别的邮箱宏哥就没有细看了,可能后期这两个邮箱页面优化升级,也就 ...
- 使用crewai创建属于你自己的AI团队
crewai介绍 CrewAI 是一个用于协调自主 AI 代理的前沿框架. CrewAI 允许你创建 AI 团队,其中每个代理都有特定的角色.工具和目标,协同工作以完成复杂任务. 把它想象成组建你的梦 ...
- FishSpeech应用篇——专属朗读人
背景 FishSpeech部署教程参见:使用FishSpeech进行语音合成推理 - 天命小猪 - 博客园 部署好之后,就能够基于推理来定制自己专属朗读人.编程能力强的小伙伴可以结合AI定制一个自己的 ...
- 解决2023新版Edge浏览器页面加载不出来问题
如果你遇到2023新版Edge浏览器页面无法加载的问题,可以尝试以下几种解决方法: 检查网络连接:确保你的网络连接正常,可以尝试打开其他网页或使用其他应用程序进行网络测试. 清除浏览器缓存:打开Edg ...
- 事务中无法切换数据源?DataSourceSwitchInvoker:轻松实现多数据源切换执行工具类
背景: 在有标注为@Transactional的类或公共方法中(传播特性,如:NOT_SUPPORTED.SUPPORTS.REQUIRED[默认值].REQUIRES_NEW)执行数据源切换可能不成 ...
- 【Loongson】支持AXI总线接口
概述 支持axi接口.但其实没有burst,没有cache,没有tlb,所以仿真起来全是空泡,冲突转发相关功能正确性就测不出来. 从sram改为axi:等待时间从一拍到看信号握手 主要更改/bug处: ...
- Caused by: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String 解决办法
使用MyBatis 更新数据库数据的时候 遇到了这个错误: Caused by: java.lang.IllegalArgumentException: invalid comparison: jav ...
- Django的MVT模式和Spring的MVC模式类比
Spring的MVC模式 MVC: Model-View-Controller 模型-视图-控制器 M: 数据处理 V: 界面显示 C: 逻辑处理 最开始用于Desktop程序开发,现在已被广泛使用, ...
- MOS管的引脚,G、S、D分别代表什么?
引脚解析: G:gate 栅极,N沟道的电源一般接在D. S:source 源极,输出S,P沟道的电源一般接在S. D:drain 漏极,输出D.增强耗尽接法基本一样. mos管是金属(metal)- ...
- jupyterhub nginx proxy pass----ipv6转ipv4实现内网穿透
jupyterhub 很多人应该已经对jupyter和notebook已经有所了解了.如果是多人共享服务器的话,就需要用到jupyter的多用户版本jupyterhub.jupyterhub架构如图所 ...