記一次服務釋出之後,圖形驗證碼亂碼的服務排查
阿新 • • 發佈:2019-02-03
由於業務拓展,新買了臺系統為centOS7的伺服器,配置完jdk和nginx之後,將服務釋出到伺服器上並部署啟動,然後重新整理頁面,神奇的事情就出現了:
第一個想到的問題,就是進行本地除錯,發現一切正常;於是在生成驗證碼文字的地方加上了logger輸出,再次釋出程式到伺服器上,確定是否是文字生成時產生的問題,附上程式碼:
- public Valicode() throws IOException {
- // String vcode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- String vcode = "0123456789";
- codeImg = new
- Graphics2D g = codeImg.createGraphics();
- g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setColor(Color.white);
- g.fillRect(0, 0, 74, 30);
- for (int i = 0; i < 100; i++) {
- int x = (int) Math.round((Math.random() * 74));
- int y = (int) Math.round((Math.random() * 30));
- Color color = new Color((float) (Math.random() * 0.7) + 0.3F, (
- (float) (Math.random() * 0.7) + 0.3F);
- g.setColor(color);
- g.drawRect(x, y, 1, 1);
- }
- code = "";
- for (int i = 0; i < 4; i++) {
- int fontsize = (int) Math.round(Math.random() * 3 + 20);
- Font font = new Font("", (int) Math.round(Math.random() * 3), fontsize);
- Color color = new Color((float) (Math.random() * 0.7), (float) (Math.random() * 0.7), (float) (Math.random() * 0.7));
- g.setColor(color);
- g.setFont(font);
- // Character c = vcode.charAt(Math.round((float) Math.random() * 35));
- Character c = vcode.charAt(Math.round((float) Math.random() * 9));
- code += c;
- g.drawString(c + "", 18 * i + (int) (Math.random() * 10 - 5) + 2, 24 + (int) (Math.random() * 10 - 5));
- }
- logger.info("code:{}",code);
- }
進入font原始碼:
- public Font(String name, int style, int size) {
- this.name = (name != null) ? name : "Default";
- this.style = (style & ~0x03) == 0 ? style : 0;
- this.size = size;
- this.pointSize = size;
- }
以及系統的字型列表:
發現並沒有我們需要的字型,於是基本可以確定問題點是出在系統字型上面。
解決方法很直接,就是直接到windows的字型目錄下
選擇 微軟雅黑 字型,直接上傳到伺服器的字型檔案目錄下,可以建立自定義目錄:(我是新建了micro的目錄,將拷貝出來的字型放在了此目錄下)
然後利用命令:fc-cache 重新載入字型配置,清除字型快取。然後重啟專案,發現一切ok。