web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletResponse
ServletOutputStream getOutputStream() throws IOException;
PrintWriter gerWriter() throws IOException;
要获取下载文件的路径
下载的文件名叫啥
设置想办法让浏览器支持下载我们需要的东西
获取下载文件的输入流
创建缓冲区
获取OutputStream对象
将FileOutputStream流写入到buffer缓冲区
使用OutputStream将缓冲区的数据输出到客户端
package com.wyh.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
/**
* @author WuYongheng
* @date 2022/8/30
* @description
*/
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 要获取下载文件的路径
String realPath = "D:\\code\\Javaweb\\response\\target\\classes\\联系人.xlsx";
System.out.println("下载文件的路径:" + realPath);
// 下载的文件名叫啥
String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
// 设置想办法让浏览器支持下载我们需要的东西
resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
// 创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
// 获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
// 将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区的数据输出到客户端
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
filedown
com.wyh.servlet.FileServlet
filedown
/wyh
验证怎么来的?
package com.wyh.servlet;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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 WuYongheng
* @date 2022/8/30
* @description
*/
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 如何让浏览器5秒自动刷新一次;
resp.setHeader("refresh", "5");
// 在内存中创建一张图片
BufferedImage image = new BufferedImage(80, 20, 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);
// 告诉浏览器,这个请求用图片的方式打开
resp.setContentType("image/png");
// 网站存在缓存,不让浏览器缓存
resp.setDateHeader("expries", -1);
resp.setHeader("Cache-Control", "no-cache");
resp.setHeader("Pragma", "no-cache");
// 把图片写给浏览器
ImageIO.write(image, "png", resp.getOutputStream());
}
// 生成随机数,不够添0,保证 7位
private String makeNum() {
Random random = new Random();
String num = random.nextInt(9999999) + "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 7 - num.length(); i++) {
sb.append("0");
}
num = sb.toString() + num;
return num;
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
image
com.wyh.servlet.ImageServlet
image
/image
右键可以另存为,说明确实是图片
package com.wyh.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
/**
* @author WuYongheng
* @date 2022/8/30
* @description
*/
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobby");
System.out.println(username);
System.out.println(password);
System.out.println(Arrays.toString(hobbies));
// 通过请求转发
req.getRequestDispatcher("/success.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LoginServlet
com.wyh.servlet.LoginServlet
LoginServlet
/login
注意 action = "login"
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
登录