统计网站的访问次数 点击切换验证码 文件下载

1.统计网站的访问次数:
index.html:
个人博客访问量
Servlet:
@WebServlet(value = “/MyServlet” , loadOnStartup = 4) //服务器启动时,创建此Servlet对象
public class MyServlet extends HttpServlet {
@Override
public void init() throws ServletException {
getServletContext().setAttribute(“count”,0);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置response响应编码
response.setContentType(“text/html;charset=UTF-8”);
response.getWriter().write(“

博客访问量

”);
//用户每次访问,从域中取出,加1 ,再存进去
ServletContext servletContext = request.getServletContext();
//从域中取出
Integer count = (Integer) servletContext.getAttribute(“count”);
//加1
count++;
//再存进去
servletContext.setAttribute(“count”,count);
response.getWriter().write(“
你是,第”+count+“位访问此网站…
”);
}

2.点击切换验证码


Servlet(生成验证码图片):
@WebServlet("/CheckcodeServlet")
public class CheckcodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//  创建画布
	int width = 120;
	int height = 40;
	BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	//  获得画笔
	Graphics g = bufferedImage.getGraphics();
	//  填充背景颜色
	g.setColor(Color.white);
	g.fillRect(0, 0, width, height);
	//  绘制边框
	g.setColor(Color.red);
	g.drawRect(0, 0, width - 1, height - 1);
	//  生成随机字符
	//  准备数据
	String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	//  准备随机对象
	Random r = new Random();
	//  声明一个变量 保存验证码
	String code = "";
	//  书写4个随机字符
	for (int i = 0; i < 4; i++) {
		//  设置字体
		g.setFont(new Font("宋体", Font.BOLD, 28));
		//  设置随机颜色
		g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

		String str = data.charAt(r.nextInt(data.length())) + "";
		g.drawString(str, 10 + i * 28, 30);

		//  将新的字符 保存到验证码中
		code = code + str;
	}
	//  绘制干扰线
	for (int i = 0; i < 6; i++) {
		//  设置随机颜色
		g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));

		g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
	}

	//  将验证码 打印到控制台
	System.out.println(code);

	//  将验证码放到session中
	request.getSession().setAttribute("code_session", code);

	//  将画布显示在浏览器中
	ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
}

}
3.文件下载

工具类
public class DownLoadUtils {
public static String getName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains(“Firefox”)) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = “=?utf-8?B?” + base64Encoder.encode(filename.getBytes(“utf-8”)) + “?=”;
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, “utf-8”);
}
return filename;
}
}
Servlet类:
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //1.获取请求文件名 : 以key找值value(name)
    String filename = request.getParameter("filename");

    //2.获取文件名真实路径,封装字节输入流
    //获取域对象(request对象将信息都存放到域中都可以访问)
    ServletContext servletContext = request.getServletContext();

    //获取路径
    String realPath = servletContext.getRealPath("/download/" + filename);

    //3.获取输入流
    FileInputStream in = new FileInputStream(realPath);

    //指定文件mime类型(给浏览器说用的什么类型的文件)
    String mimeType = servletContext.getMimeType(filename);
    response.setContentType(mimeType);  //告诉浏览器发送文件的类型

    //4.告诉浏览器以附件方式保存
    //解决中文乱码和浏览器兼容性
    String userAgent = request.getHeader("user-agent");
    System.out.println(userAgent);
    //调用工具类
     filename = DownLoadUtils.getName(userAgent, filename);

    response.setHeader("content-disposition","attachment;filename="+filename);

    ServletOutputStream out = response.getOutputStream();

    //打印获取
    //使用工具类
    IoUtil.copy(in,out);

    //关闭流
    out.close();
    in.close();
}

}
web目录结构:
download存放本地文件
WEB-INF: lib存放jar包: hutool-all-5.2.3.jar
index.html:

图片下载
压缩包下载
文档下载

你可能感兴趣的:(笔记)