服务器上的一张图片  在这个图片上写上XX 字后  从新生成一张图片!  大家有没有这样的代码?谢谢

解决方案 »

  1.   

    用servlet生成验证码的例子,给楼主参考一下import java.io.IOException;import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.*;public class ChkImgGenerate 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 {
            ServletOutputStream out = response.getOutputStream();
            String imgid = request.getParameter("imgid");
            response.setContentType("image/jpeg");
            out.write(getImgs(imgid));
            out.flush();
        }
        
        private byte[] getImgs(String imgid){
            String query = "select img from chk_img where id='"+imgid+"'";
            Blob blob = null;
            byte[] bytes = null;
            //String description = "";
            
            /*Random rnd = new Random();
            for(int i=0; i<ids.length; i++){
                int tmp = rnd.nextInt(total);
                ids[i] = "0123456789".substring(tmp,tmp+1);
            }*/
            
            try{
                /*Context initCtx = new InitialContext();
                DataSource db = (DataSource) initCtx
                        .lookup("java:comp/env/jdbc/mysql");
                Connection conn = db.getConnection();*/
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test",
                        "root",
                        "");
                Statement stmt = conn.createStatement();            
                ResultSet rs = stmt.executeQuery(query);
                //ResultSetMetaData md = rs.getMetaData();
                while(rs.next()){
                    blob = rs.getBlob(1);
                }
                bytes = blob.getBytes(1,(int)(blob.length()));
                conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            return bytes;
        }}