response应用
response应用
一、response下载文件
1.新建FileServlet类
// 文件下载测试
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.获取要下载的文件路径 直接写死的笨办法
String realPath = "E:\\untitled\\servlet\\response\\target\\classes\\HeaderImg.png";
System.out.println("下载文件的路径:" + realPath);
// 2.获取下载的文件名
String FileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
System.out.println("下载文件的名字:" + FileName);
// 3.设置让浏览器能够支持下载需要的东西 Content-disposition URLEncoder.encode解决中文乱码问题
resp.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(FileName,"UTF-8"));
// 4.获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
// 5.创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
// 6.获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
// 7.将FileOutputStream流写入到buffer缓冲区,使用OutputStram将缓冲区中的数据输出到客户端
while ((len = in.read(buffer)) > 0){
out.write(buffer,0,len);
}
in.close();
out.close();
}
}
2.配置servlet映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>filedown</servlet-name>
<servlet-class>com.wuxin.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>filedown</servlet-name>
<url-pattern>/down</url-pattern>
</servlet-mapping>
</web-app>
二、response实现验证码
1.新建ImageServlet类
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 如何让浏览器3秒刷新一次
resp.setHeader("refresh","3");
// 在内存中创建一张图片
BufferedImage Image = new BufferedImage(80,80,BufferedImage.TYPE_INT_RGB);
// 得到图片
Graphics2D g = (Graphics2D) Image.getGraphics();
// 设置图片背景颜色
g.setColor(Color.white); // 设置画笔颜色
g.fillRect(0,0,80,20);
// 给图片写数据
g.setColor(Color.blue); // 设置画笔颜色
g.setFont(new Font(null,Font.BOLD,20)); // 设置画笔字体
g.drawString(makeNum(),0,20); // 画随机数字
// 告诉浏览器,这个请求用图片的方式打开 jpeg或者png
resp.setContentType("image/jpeg");
// 网站存在缓存,不让浏览器缓存 早些年的写法,由于电脑配置越来越高,可以不写
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-cache");
resp.setHeader("Pragme","no-cache");
// 把图片写给浏览器
ImageIO.write(Image,"jpeg",resp.getOutputStream());
}
// 生成随机数
private String makeNum(){
Random random = new Random();
String num = random.nextInt(9999) + "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4 - num.length(); i++) {
sb.append("0");
}
String s = sb.toString() + num;
return num;
}
}
2.配置web.xml映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>filedown</servlet-name>
<servlet-class>com.wuxin.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>filedown</servlet-name>
<url-pattern>/down</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileDown</servlet-name>
<servlet-class>com.wuxin.servlet.TestFileDownLoad</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileDown</servlet-name>
<url-pattern>/Load</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.wuxin.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/img</url-pattern>
</servlet-mapping>
</web-app>
三、response实现重定向
resp.sendRedirect("/r/img");
苟利国家生死以,岂因祸福避趋之