開發環境:win10+springboot1.5+Kaptcha2.3.xspring
問題:驗證碼圖片在windows下正常顯示,在Linux下能顯示圖片,但不顯示文字windows
相關代碼:centos
@Bean public DefaultKaptcha captchaProducer() { DefaultKaptcha captchaProducer = new DefaultKaptcha(); Properties properties = new Properties(); // 圖片邊框 properties.setProperty("kaptcha.border", "yes"); // 邊框顏色 properties.setProperty("kaptcha.border.color", "105,179,90"); // 字體顏色 properties.setProperty("kaptcha.textproducer.font.color", "red"); // 圖片寬 properties.setProperty("kaptcha.image.width", "110"); // 圖片高 properties.setProperty("kaptcha.image.height", "40"); // 字體大小 properties.setProperty("kaptcha.textproducer.font.size", "30"); // session key properties.setProperty("kaptcha.session.key", "code"); // 驗證碼長度 properties.setProperty("kaptcha.textproducer.char.length", "4"); // 字體 properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑"); Config config = new Config(properties); captchaProducer.setConfig(config); return captchaProducer; }
緣由及解決方法:因爲Linux環境不存在相關字體,需先在Linux下安裝字體緩存
安裝字體:系統 centos7springboot
一、yum -y install fontconfigsession
二、進入 /usr/share 能夠看到 fontconfig、fonts目錄生成字體
三、在Windows系統上,拷貝須要的字體centos7
四、將所需的字體上傳的到Linux環境的 /usr/share/fonts/chinese目錄:spa
若是沒有 /usr/share/fonts/chinese 目錄,執行以下命令:code
mkdir -p /usr/share/fonts/chinese chmod -R 755 /usr/share/fonts/chinese
上傳字體如圖:
五、安裝ttmkfdir來搜索目錄中全部的字體信息,並彙總生成fonts.scale文件
yum -y install ttmkfdir
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
六、修改字體配置文件了
vi /etc/fonts/fonts.conf
七、刷新內存中的字體緩存,這樣就不用reboot重啓了
fc-cache
fc-list看一下字體列表。