[javaweb]javaweb中HttpServletResponse实现文件下载,验证码和请求重定向功能
HttpServletResponse
web服务器接受到客户端的http请求之后,针对这个请求,分别创建一个代表请求的httpServletRequest和代表响应的HttpServletResponse
-
文件下载
package com.javaweb.servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; /** * @author panglili * @create 2022-07-19-18:17 */ public class Response extends HelloServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取下载路径 String path="C:\\Users\\塔塔\\Pictures\\Camera Roll\\1.jpg"; //下载的文件名 String fileName = path.substring(path.lastIndexOf("\\") + 1); //设置想办法让浏览器支持下载需要的 resp.setHeader("Content-Disposition","attachment;fileName="+fileName); //获取下载文件的输入流 FileInputStream stream = new FileInputStream(path); //创建缓冲区 int len=0; byte[] buffer = new byte[1024]; //获取OutputStream ServletOutputStream out = resp.getOutputStream(); //将文件写到buffer缓冲区 while((len=stream.read(buffer))>0){ out.write(buffer,0,len); } //关闭流 stream.close(); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
-
验证码实现
package com.javaweb.servlet;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
/**
* @author panglili
* @create 2022-07-19-18:39
*/
public class ImageServlet extends HelloServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//每三秒刷新一次
resp.setHeader("refresh","3");
//在内存创建一个图片
BufferedImage image=new BufferedImage(80,20,BufferedImage.TYPE_INT_BGR);
//得到图片
Graphics2D g=(Graphics2D)image.getGraphics();
//设置背景颜色
g.setColor(Color.red);
g.fillRect(0,0,80,20);
//给图片写数据
g.setColor(Color.BLACK);
g.setFont(new Font(null, Font.BOLD,20));
g.drawString(makeNum(),0,20);
//告诉浏览器这个请求用图片打开
resp.setContentType("image/jpeg");
//网站有缓存,不让存
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-Cache");
resp.setHeader("Pragma","no-cache");
ImageIO.write(image,"jpg",resp.getOutputStream());
}
private String makeNum(){
Random random=new Random();
String num=random.nextInt(9999999)+"";
StringBuffer str = new StringBuffer();
for (int i=0;i<7-num.length();i++){
str.append("0");
}
num=str.toString()+num;
return num;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>img</servlet-name>
<servlet-class>com.javaweb.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>img</servlet-name>
<url-pattern>/img</url-pattern>
</servlet-mapping>
-
请求重定向
package com.javaweb.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author panglili * @create 2022-07-19-18:46 */ public class Redirect extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("/img"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
<servlet> <servlet-name>redi</servlet-name> <servlet-class>com.javaweb.servlet.Redirect</servlet-class> </servlet> <servlet-mapping> <servlet-name>redi</servlet-name> <url-pattern>/redi</url-pattern> </servlet-mapping>