核心提示:有时java项目部署到阿里云机器上,登录页面的图形验证码有可能是乱码,如下图所示分析了java中生成验证码的方法String s = RandomStringUtils.random(4, true,...
有时java项目部署到阿里云机器上,登录页面的图形验证码有可能是乱码,如下图所示
分析了java中生成验证码的方法
String s = RandomStringUtils.random(4, true, true); // 保存入session,用于与用户的输入进行比较. // 注意比较完之后清除session. HttpSession session = request.getSession(true); session.setAttribute(Constants.AUTH_CODE, s); response.setContentType("images/jpeg"); //response.setContentType("images/jpeg,charset=utf-8"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ServletOutputStream out = response.getOutputStream(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 设定字体 Font mFont = new Font("Times New Roman", Font.BOLD, 22);// 设置字体 g.setFont(mFont);
注意到Times New Roman这个字体,乱码的原因就是阿里云机器上没有这种字体,
解决方法 把本机上的这个字体上传到服务器
本机字体目录C:\Windows\Fonts,如下图
上传到linux服务器上的jre/lib/fonts目录下,如下图
然后重启tomcat,问题解决。