img responseピクチャデータの表示

5212 ワード

[url]http://blog.csdn.net/ykf69177/article/details/8655881[/url]
  <br>    loadImg=function(){  <br>        document.getElementById("img").src="servlet/IdentityServlet?ts="+new Date().getTime();  <br>    }  <br>   



//           
FileInputStream fis = new FileInputStream("/Users/kun/Desktop/ServerImage/lxj12345.jpeg") ;
//
int size = fis.available();
byte data[] = new byte[size] ;
fis.read(data) ;
fis.close();

//
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream() ;
os.write(data);
os.flush();
os.close();
public class IdentityServlet extends HttpServlet {  

/**
* The doGet method of the servlet.

*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("image/jpeg"); //

String randomString = getRandomStr(); //
request.getSession(true).setAttribute("randomString", randomString);

int width =100,height=30; //

Color color = getRandomColor(); //
Color reverse = getReverseColor(color); // ,

BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //

Graphics2D graph = bi.createGraphics(); //
graph.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16)); //
graph.setColor(color); //
graph.fillRect(0, 0, width, height); //
graph.setColor(reverse); //
graph.drawString(randomString, 18, 20); //

/**
*
*/
for(int i = 0; i<100; i++){
graph.drawRect(random.nextInt(width),random.nextInt(height), 1,1);
}

ServletOutputStream out = response.getOutputStream(); // JPEG
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out); //
encoder.encode(bi);

out.flush();
}

/**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

public static char[] 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',
'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'}; //

private Random random = new Random(); //

/**
* 6
* @return
*/
private String getRandomStr(){
StringBuffer buffer = new StringBuffer();
for(int i=0;i<6;i++){
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}

/**
*
* @return
*/
private Color getRandomColor(){
return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
}

/**
*
* @param color
* @return
*/
private Color getReverseColor(Color color){
return new Color(255-color.getRed(),255-color.getGreen(),255-color.getBlue());
}
}