simple captcha的一个重要bug
在simple captcha的bugs里有描述。
在没有装X11的服务器上会抛出一个HeadlessException错误,很多资料(包括simple captcha的主页)上介绍要加上 -Djava.awt.headless=true 这个启动参数,但是似乎1.5后并没有用。
其实并不复杂,因为引用了GraphicsEnvironment和GraphicsDevice,所以只要注解就行了:
在 DefaultWordRenderer.java 里
//GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); //GraphicsDevice gd = ge.getDefaultScreenDevice();//GraphicsConfiguration gc = gd.getDefaultConfiguration();
用ant重新编译一下,注意里面有一个j2ee.jar,找一个tomcat里的servlet-api.jar就可以了。
另外它是使用1.1的格式编译的,可以用1.5来编译,但是要改几个变量名,因为跟enum起冲突。
附件里给出一个已经编译好的jar ,使用1.5的格式编译。 1 楼 恋空302 2011-10-08 我想问问,如果在我的应用服务器servlet里面通过http请求获取另外一个应用服务器生成的验证码。
try
{
String url="http://10.70.235.4/getcha?session=B2AH8K8L4H4&busid=ZJBOSS";
byte [] images=getImgStr(url,3);
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(images);
responseOutputStream.flush();
responseOutputStream.close();
}catch (Exception ex)
{
ex.printStackTrace();
}
----
private byte[] getImgStr(String url,int timeOut){
byte[] response=null;
HttpClient client = new HttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("","");
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
client.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);
client.getParams().setParameter("http.socket.timeout",new Integer(timeOut * 1000));
client.getParams().setParameter("http.protocol.content-charset","GBK");
GetMethod method = new GetMethod(url);
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
response=method.getResponseBodyAsString().getBytes();
} else {
throw new Exception("Unexpected failure: "+ method.getStatusLine().toString());//打印服务器返回的状态
}
method.releaseConnection();
} catch (Exception e) {
logger.error("Http协议get方法获取图形验证码出现异常:", e);
}
return response;
} 2 楼 恋空302 2011-10-08 我通过这种方式来获取,结果图片显示不出来,请帮我看看