在前臺頁面有如下語句<img onclick="this.src='/ran/random?random='+Math.random()" alt="驗證碼,點擊圖片更換" src="/ran/random?random=0.2868249340216069" width="86" height="40">
其中src 指向的地址不是一個img文件,而是一個轉(zhuǎn)到SpringMVC的類中方法src="/ran/random?random=0.2868249340216069"
方法如下:
@Controller
public class RandomCodeController {
@RequestMapping(value={"/ran/random"})
public void genericRandomCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setHeader("Cache-Control", "private,no-cache,no-store");
response.setContentType("image/png");
HttpSession session = request.getSession();
int width = 85;
int height = 28;
BufferedImage image = new BufferedImage(width, height, 2);
Graphics2D g = image.createGraphics();
g.setComposite(AlphaComposite.getInstance(3, 1.0f));
Random random = new Random();
g.setColor(new Color(231, 231, 231));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Microsoft YaHei", 2, 24));
String sRand = "";
for (int responseOutputStream = 0; responseOutputStream < 4; ++responseOutputStream) {
String rand = String.valueOf(random.nextInt(10));
sRand = sRand + rand;
g.setColor(new Color(121, 143, 96));
g.drawString(rand, 13 * responseOutputStream + 16, 23);
}
session.setAttribute("COMMON_RAND_CODE", (Object)sRand);
g.dispose();
ServletOutputStream var12 = response.getOutputStream();
ImageIO.write((RenderedImage)image, "png", (OutputStream)var12);
var12.close();
}
}
所以其實這個src相聯(lián)系的是一個ServletOutputStream這個地方我不太理解ServletOutputStream如何和一個圖片聯(lián)系起來?
添加回答
舉報
0/150
提交
取消