有时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,问题解决。

未大透