在用Maven之前,kaptcha的jar包是下载后作为LIbrary导入项目的,但是在项目使用maven后,maven上不存在kaptcha的坐标,只能下载jar包到本地并添加到项目中。
然而问题是:maven只能打包pom.xml里面声明的依赖,不能识别本地jar包。本文介绍把本地jkaptcha的jar包添加到pom.xml中,并在Java项目中使用的方法
解决方法 1. 下载jar包kaptcha下载地址
下载kaptcha-2.3.2.jar包并解压
2. maven安装本地jar包cmd进入jar包所在文件夹,输入以下命令
注意:maven会根据安装目录的config目录下的settings.xml中的仓库地址来安装jar包,要更改安装到的仓库位置(默认c盘)请自行百度
mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dfile=kaptcha-2.3.2.jar -Dpackaging=jar -DgeneratePom=true3. 引入坐标
在pom.xml中引入坐标
4. 使用验证码功能 4.1 applicationContext.xml下配置beancom.google.code kaptcha 2.3.2
4.2 业务代码yes 105,179,90 blue 125 45 45 code 4 宋体,楷体,微软雅黑
Controller层新建KaptchaController类
@Controller
public class KaptchaController {
@Autowired
private Producer captchaProducer;
@RequestMapping("/kaptcha.jpg")
public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("******************验证码是: " + code + "******************");
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}
5. jsp中使用




