下面是我的jsp代码,图片信息能显示出来,但就是字符串无法在图片里面显示,请你们帮我看下,到底是哪里出问题了?
<%@ page contentType="image/jpeg" pageEncoding="utf-8"%>
<%@page import="java.awt.*,java.awt.image.*,java.util.*" %>
<%@page import="javax.imageio.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<%
int width=60,height=20;
BufferedImage image =
new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Random r = new Random();
Graphics g = image.getGraphics();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 30));
String[] str={"0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","G","F","I","J",
"K","R","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z"};
String num ="";
for(int i=0;i<6;i++){
num = num+str[r.nextInt(62)];
}
g.dispose();
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "JPEG", sos);
sos.flush();
sos.close();
%>
</body>
</html>
for(int i=0;i <6;i++){
num = num+str[r.nextInt(62)];
}
g.drawString(num,30,20);
我都试了好久,也检查了好多遍,就是没看出来是哪错了!!!!!!!
out.clear();
out = pageContext.pushBody();
就不会报错
不过只有背景没有文字 很奇怪的问题
我再弄下
<body>
<%
response.setContentType("image/jpeg");
int width=60,height=20;
BufferedImage image =
new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Random r = new Random();
Graphics g = image.getGraphics();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, width,height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
String[] str={"0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","G","F","I","J",
"K","R","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z"};
String num ="";
for(int i=0;i <4;i++){
num = num+str[r.nextInt(62)];
num = num+str;
g.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));
g.drawString(num,6+10*i,15);
}
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(image, "JPEG", sos);
g.dispose();
out.clear();
out = pageContext.pushBody();
sos.close();
image=null;
%>
</body>
还需要调试一下 能出来了
你自己看需求在改改 <body>
<%
response.setContentType("image/jpeg");
int width=60,height=20;
BufferedImage image =
new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Random r = new Random();
Graphics g = image.getGraphics();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, width,height);
// g.setFont(new Font("Times New Roman",Font.PLAIN,18));
String[] str={"0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F","G","F","I","J",
"K","R","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z"};
String num ="";
for(int i=0;i <4;i++){
num = str[r.nextInt(62)];
// num = num+str;
g.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));
g.drawString(num,6+13*i, 15);
}
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(image, "JPEG", sos);
g.dispose();
out.clear();
out = pageContext.pushBody();
sos.close();
image=null;
%>
</body>