A. J2EE中JSP頁面怎麼獲取java類里生成的隨機驗證碼

直接 <img src="your image request url" >

B. 用java隨機生成四位驗證碼,只求編程代碼

不知道你問的是不是生成這種圖片驗證碼?
如果只要一個隨機四位數 那這行代碼就夠了(new Random().nextInt(9000) + 1000;),
如果是生成頁面圖片驗證碼就是下面的了:
//設定 響應模式
resp.setContentType("image/jpeg");

// 生成令牌環數據;
Integer token = new Random().nextInt(9000) + 1000;

// 保存令牌環數據到session中
req.getSession().setAttribute(IMAGE_TOKEN_NAME, token);

// 生成令牌環圖片
ServletOutputStream out = resp.getOutputStream();
BufferedImage img = new BufferedImage(60, 20,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, img.getWidth(), img.getHeight());
g.setColor(Color.BLUE);
g.setFont(new Font("", Font.BOLD, 18));
g.drawString(String.valueOf(token), 10, 16);
ImageIO.write(img, "jpg", out);
out.close();

C. java swing隨機驗證碼

(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{

response.setContentType("image/jpeg");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
HttpSessionsession=request.getSession();

intwidth=60,height=20;

BufferedImageimage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);

//獲取圖形上下文
Graphicsg=image.getGraphics();

//生成隨機類
Randomrandom=newRandom();

//設定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);

//設定字體
g.setFont(newFont("TimesNewRoman",Font.PLAIN,18));

//隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160,200));
for(inti=0;i<155;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}

//取隨機產生的認證碼(4位數字)
StringsRand="";
for(inti=0;i<4;i++){
Stringrand=String.valueOf(random.nextInt(10));
sRand+=rand;
//將認證碼顯示到圖象中
g.setColor(newColor(20+random.nextInt(110),20+random
.nextInt(110),20+random.nextInt(110)));//調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}

//將認證碼存入SESSION
session.setAttribute("rand",sRand);
//圖象生效
g.dispose();
=response.getOutputStream();
//輸出圖象到頁面
ImageIO.write(image,"JPEG",responseOutputStream);

//以下關閉輸入流!
responseOutputStream.flush();
responseOutputStream.close();
}

static ColorgetRandColor(intfc,intbc){
//給定范圍獲得隨機顏色
Randomrandom=newRandom();
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}

/**
*HandlestheHTTPGETmethod.
*
*@paramrequest
*servletrequest
*@paramresponse
*servletresponse
*/
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
processRequest(request,response);
}

這個是我web里用的驗證碼,你可以改改

D. Java上機怎麼隨機生成驗證碼

我在網上看過PHP做的一個隨機字元串生成程序,很簡單的喔,親:
function randStr($length=8)
{
$str = substr(md5(time()), 0, $length);
return $str;
}
具體步驟就是先獲得當前時間毫秒數,再md5加密後取前多少位就行了。

E. Java Swing中隨機驗證碼如何實現

根本思想是使用
隨機數
具體實現:
1.
生成一個隨機數
2.
將隨機數使用全局變數保留
3.
將隨機數設置成按鈕的提示文字
4.
編寫點擊之後重新生成隨機數,並且綁定到按鈕上
5.
驗證
如果需要實例,請留下郵箱!^_^

F. Java產生隨機驗證碼

package com.you100.util;
import java.util.Random;
public class Picture {
public static void main(String[] args) {
Random ran=new Random();char ch[] =new char[]{'a','b','c','d','e','f','g','@','1','2','3','4','5'};

StringBuilder str=new StringBuilder("");
for(int i=0;i<4;i++){

str.append(ch[ran.nextInt(ch.length)]);

}
System.out.println("驗證碼="+str);

}}

在控制台上隨回機生成答驗證碼

G. 如何在java中生成驗證碼在頁面生成

以下是我寫的生成的圖片驗證碼

//調用驗證碼生成並把驗證碼返回頁面
Map<String,BufferedImage>imageMap=ImageUtil.createImage();
Stringcode=imageMap.keySet().iterator().next();
session.setAttribute("imageCode",code);
BufferedImageimage=imageMap.get(code);
response.setContentType("image/jpeg");
OutputStreamops=response.getOutputStream();
ImageIO.write(image,"jpeg",ops);
ops.close();
//生成驗證碼工具類
publicfinalclassImageUtil{

privatestaticfinalchar[]chars={'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','0','1','2','3','4','5','6','7','8','9'};
privatestaticfinalintSIZE=4;
privatestaticfinalintLINES=5;
privatestaticfinalintWIDTH=80;
privatestaticfinalintHEIGHT=40;
privatestaticfinalintFONT_SIZE=30;

publicstaticMap<String,BufferedImage>createImage(){
StringBuffersb=newStringBuffer();
BufferedImageimage=newBufferedImage(WIDTH,HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphicsgraphic=image.getGraphics();
graphic.setColor(Color.LIGHT_GRAY);
graphic.fillRect(0,0,WIDTH,HEIGHT);
Randomran=newRandom();
//畫隨機字元
for(inti=1;i<=SIZE;i++){
intr=ran.nextInt(chars.length);
graphic.setColor(getRandomColor());
graphic.setFont(newFont(null,Font.BOLD+Font.ITALIC,FONT_SIZE));
graphic.drawString(chars[r]+"",(i-1)*WIDTH/SIZE,HEIGHT/2);
sb.append(chars[r]);
}
//畫干擾線
for(inti=1;i<=LINES;i++){
graphic.setColor(getRandomColor());
graphic.drawLine(ran.nextInt(WIDTH),ran.nextInt(HEIGHT),
ran.nextInt(WIDTH),ran.nextInt(HEIGHT));
}
Map<String,BufferedImage>map=newHashMap<String,BufferedImage>();
map.put(sb.toString(),image);
returnmap;
}

(){
Randomran=newRandom();
Colorcolor=newColor(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256));
returncolor;
}

(BufferedImageimage)
throwsIOException{
ByteArrayOutputStreambos=newByteArrayOutputStream();
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
byte[]imageBts=bos.toByteArray();
InputStreamin=newByteArrayInputStream(imageBts);
returnin;
}
}

H. JAVA程序中,隨機產生一個6位的驗證碼 (是由0

import java.util.Random;

public class TestRandom {
public String getRandStr(int charCount) {
String charValue = "";
for (int i = 0; i < charCount; i++){
char c = (char) (randomInt(0,26)+'a');
charValue += String.valueOf(c);
}
return charValue;
}
public String getRandNum(int charCount) {
String charValue = "";
for (int i = 0; i < charCount; i++){
char c = (char) (randomInt(0,10)+'0');
charValue += String.valueOf(c);
}
return charValue;
}

public int randomInt(int from, int to){
Random r = new Random();
return from + r.nextInt(to - from);
}
}

I. 用java實現:隨機獲取4位的驗證碼

驗證碼是指網頁的驗證碼還是手機的驗證碼

下面是隨機生成四位數的相關代碼

importjava.util.Random;

publicclassRandomTest{
publicstaticvoidmain(String[]args){
System.out.println("Math.random得到小數");
System.out.println(Math.round(Math.random()*10000));
System.out.println("Random");
System.out.println(newRandom().nextInt(9999));
System.out.println("字元串前面補0的話就這樣String.format");
System.out.println(String.format("%04d",newRandom().nextInt(9999)));
}
}