〔picture.java〕
package creator.common.attachlogin;
import java.io.*;
public class picture{
final int key=1;
final string error_format_int= "format of color is not rgb.sample /"212|232|0/"";
final string error_color_input="format of color(num|num|num): num in 0-255";
/**格式化输出数据**/
public string manage(string temp){
string returnstr="";
temp = encrypt(temp);
byte[] by =temp.getbytes();
for(int i=0;i<by.length;i++){
returnstr=returnstr+(byte)by[i]+"|";
}
return returnstr;
}
/**格式化输入数据**/
public byte[] dismanage(string temp){
int len=0,index=0,i=0,first=0;
while(( i=temp.indexof("|",first))>-1){
len++;
first=i+1;
}
byte[] by=new byte[len];
first=0;
while(( i=temp.indexof("|",first))>-1){
by[index]=byte.parsebyte(temp.substring(first,i));
index++;
first=i+1;
}
return by;
}
/**随机生成四位的附加码**/
public string getrandom(){
int i1 = (int)(java.lang.math.random()*10);
int i2 = (int)(java.lang.math.random()*10);
int i3 = (int)(java.lang.math.random()*10);
int i4 = (int)(java.lang.math.random()*10);
return string.valueof(i1)+string.valueof(i2)+string.valueof(i3)+string.valueof(i4);
}
/**加密1:错位处理**/
public string encrypt(string randomstr){
string para=random()+randomstr.substring(0,1)+random()+random()+randomstr.substring(1,2);
para= para+random()+randomstr.substring(2);
return jiami(para);
}
/**得到随机数0-9之间**/
private string random(){
string temp = string.valueof((int)(java.lang.math.random()*10));
return temp;
}
/**加密2:加密处理,此方法可以自己修改**/
private string jiami(string str){
byte[] by = str.getbytes();
bytearrayinputstream in = new bytearrayinputstream(by);
int ch;
int index=0;
byte[] temp = new byte[in.available()];
while((ch=in.read())!=-1){
temp[index]=(byte)(ch-key);
index++;
}
bytearrayinputstream ins = new bytearrayinputstream(temp);
bufferedreader freader = new bufferedreader(new inputstreamreader(ins));
try{ return freader.readline();}catch(exception e){return "";}
}
/**从给的数字里得到正确的数字**/
public string discrypt(string temp){
string para = jiemi(dismanage(temp));
return para.substring(1,2)+para.substring(4,5)+ para.substring(6,8);
}
/**解密处理**/
private string jiemi(byte[] by){
bytearrayinputstream in = new bytearrayinputstream(by);
int ch;
int index=0;
byte[] temp = new byte[in.available()];
while((ch=in.read())!=-1){
temp[index]=(byte)(ch+key);
index++;
}
bytearrayinputstream ins = new bytearrayinputstream(temp);
bufferedreader freader = new bufferedreader(new inputstreamreader(ins));
try{ return freader.readline();}catch(exception e){return "";}
}
/**分解rgb格式的颜色 num|num|num**/
public int[] masterdata(string temp){
int index_len=0,index=0,next_index=0;
int[] return_arr=new int[3];
boolean break_error=false;
if(getmax(temp,"|")==2){
while((index_len=temp.indexof("|",next_index))>-1){
if(getint(temp.substring(next_index,index_len))==256){
break_error = true;
}else{
return_arr[index]=getint(temp.substring(next_index,index_len));
next_index=index_len+1;
index++;
}
if(break_error) break;
}
if(break_error){
return null;
}else{
return_arr[index] = getint(temp.substring(next_index));
return return_arr;
}
}else{
system.out.println(error_format_int+":"+temp);
return null;
}
}
private int getmax(string temp,string temp2){
int index=0,index_len=0,index_next=0;
while((index=temp.indexof(temp2,index_next))>-1){
index_len++;
index_next=index+1;
}
return index_len;
}
private int getint(string temp){
try{
return integer.parseint(temp);
}catch(exception e){
system.out.println(error_color_input+":"+temp);
return 256;
}
}
}
〔createimage.java〕
package creator.common.attachlogin;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.image.bufferedimage;
import com.sun.image.codec.jpeg.*;
import com.sun.image.codec.jpeg.jpegcodec;
public class createimage extends httpservlet {
static final private string content_type = "text/html; charset=gb2312";
final string input_back_color_error="input rgb backcolor is error";
final string input_fore_color_error="input rgb forecolor is error";
private picture pic = new picture();
//initialize global variables
public void init() throws servletexception {
}
//process the http get request
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
//生成图片
try{
int imagewidth = 60;
int imageheight = 20;
bufferedimage image = new bufferedimage(imagewidth, imageheight, bufferedimage.type_int_rgb);
graphics graphics = image.getgraphics();
graphics.setcolor(color.white);
graphics.fillrect(0,0,imagewidth,imageheight);
graphics.setcolor(color.white);
fileoutputstream fos = new fileoutputstream("attach.jpg");
bufferedoutputstream bos = new bufferedoutputstream(fos);
jpegimageencoder encoder = jpegcodec.createjpegencoder(bos);
encoder.encode(image);
bos.close();
}catch(exception e){
system.out.println("生成图片出错!");
}
string querynum = request.getparameter("image");
string queryrgb="";
if(request.getparameter("rgb")!=null){
queryrgb = request.getparameter("rgb");
}
response.setheader("cache-control","no-st
闽公网安备 35060202000074号