java 读取图片,创建验证码 显示到客户端

java web 图片显示


	response.setContentType("image/jpeg"); // 设置头
	String img = request.getParameter("img"); // 获取参数
	String filePath = request.getServletContext().getRealPath("/upload/")+img; // 拼接参数
	BufferedImage out = ImageIO.read(new File(filePath)); // 读取文件
	ServletOutputStream outputStream = response.getOutputStream(); // 获取输出流
	ImageIO.write(out, "png", outputStream); // 读取的流写入请求流中

java web 创建图片


	response.setContentType("image/png");
	BufferedImage img = new BufferedImage(100,38, BufferedImage.TYPE_INT_RGB);
	Graphics g = img.getGraphics(); // 获取画板
	g.setColor(new Color(69,195,241)); // 设置背景颜色
	g.fillRect(0,0,100,38); //填充整个屏幕
	g.setColor(Color.white); // 设置字体颜色
	Font font = new Font("",Font.PLAIN,23);
	g.setFont(font); 
	String session = UUID.randomUUID().toString().replaceAll("-","").substring(3,7);
	g.drawString(session,15,28); // 添加文字
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40)); // 添加干扰线
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40));
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40));
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40));
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40));
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40));
	g.drawLine(new Random().nextInt(92), new Random().nextInt(40), new Random().nextInt(92), new Random().nextInt(40));
	g.dispose();
	request.getSession().setAttribute("validation",session); // 放入到session中
	ImageIO.write(img, "png", response.getOutputStream()); // 返回到客户端

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值