前端页面获取不到servlet传来的图片
前端页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$title>
head>
<body>
<link href="favicon.ico" rel="shortcut icon">
<form method="post">
用户名<input type="text" name="username"><br>
密码:<input type="password" name="pwd"><br>
验证码:<input type="text" name="code"><img src="/session/CheckCode"><br>
<input type="submit" value="登录">
form>
body>
html>
servlet页面:
package com.servlet;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
@WebServlet("/CheckCode")
public class CheckCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int width=100,height=30;
//在内存中生成图片
BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//得到画笔
Graphics graphics = bufferedImage.getGraphics();
//设置画笔颜色
graphics.setColor(Color.yellow);
//填充背景色
graphics.fillRect(0,0,width,height);
//设置边线
graphics.setColor(Color.black);
graphics.drawRect(0,0,width,height);
//随机生成验证码,在图片上画验证码
String str="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
Random random=new Random();
graphics.setColor(Color.red);
for (int i=1;i<=4;i++){
char ch=str.charAt(random.nextInt(str.length()));//随机取字符
//把字符画到图片上
graphics.drawString(ch+"",width/5*i,height/2);
}
//画干扰线
graphics.setColor(Color.gray);
for (int i=0;i<=5;i++){
int x1=random.nextInt(width);
int x2=random.nextInt(height);
int y1=random.nextInt(width);
int y2=random.nextInt(height);
//画线
graphics.drawLine(x1,x2,y1,y2);
}
//把图片发送给客户端
ImageIO.write(bufferedImage,".jpg",resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
运行结果为图片裂开,开发者工具显示没有报错信息
你是不是应该打断点看啊