img 显示response图片数据

http://blog.csdn.net/ykf69177/article/details/8655881

<script type="text/javascript">  
    loadImg=function(){  
        document.getElementById("img").src="servlet/IdentityServlet?ts="+new Date().getTime();  
    }  
 </script>  
   
 <body>  
   <img src="servlet/IdentityServlet" id="img" onclick="loadImg()"/>  
 </body>  



//想要返回图片的路径  
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. <br> 
     * 
     * 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. <br> 
     * 
     * 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());  
    }  
}  











猜你喜欢

转载自huangyongxing310.iteye.com/blog/2413612
IMG