Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux上在拷贝字体路径时出现错误 #23

Open
VioletAyatan opened this issue Dec 31, 2024 · 1 comment
Open

Linux上在拷贝字体路径时出现错误 #23

VioletAyatan opened this issue Dec 31, 2024 · 1 comment

Comments

@VioletAyatan
Copy link

  • Linux环境:Ubuntu 22.04.5 LTS
  • Java环境:Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.5+9.1
  • 使用的库版本:2.2.4

报错代码:com.pig4cloud.captcha.utils.FontsUtil.getFont。

    /**
     * 手动复制字体文件到临时目录. 调用传文件的构造方法创建字体
     * @param fontName 字体文件名称
     * @return
     */
    public static Font getFont(String fontName, int style, float size) {
        Font font = null;

        String path = System.getProperty("java.io.tmpdir");

        File tempFontFile = new File(path + fontName);
        if(!tempFontFile.exists()){
            //临时文件不存在
            copyTempFontFile(fontName, tempFontFile);
        }
        if(tempFontFile.exists()) {
            try {
                font = Font.createFont(Font.TRUETYPE_FONT, tempFontFile).deriveFont(style, size);;
            } catch (FontFormatException | IOException e) {
                e.printStackTrace();
                tempFontFile.delete();
            }
        }
        return font;
    }

上面代码使用System.getProperty("java.io.tmpdir")获取临时目录路径时,在Windows上获取的路径大概如下C:\Users\XXX\AppData\Local\Temp\

此时拼接的字体文件路径无误:C:\Users\XXX\AppData\Local\Temp\actionj.ttf

但是在Linux上获取的临时目录为:/tmp,后面并没有带文件夹分隔符,这导致拼接出来的字体文件路径出错,类似这样:/tmpactionj.ttf

这样在拷贝字体时会报错:

java.io.FileNotFoundException: /tmpactionj.ttf (Permission denied)
	at java.base/java.io.FileOutputStream.open0(Native Method)
	at java.base/java.io.FileOutputStream.open(FileOutputStream.java:289)
	at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:230)
	at com.pig4cloud.captcha.utils.FileUtil.openOutputStream(FileUtil.java:37)
	at com.pig4cloud.captcha.utils.FileUtil.openOutputStream(FileUtil.java:26)
	at com.pig4cloud.captcha.utils.FileUtil.copyToFile(FileUtil.java:20)
	at com.pig4cloud.captcha.utils.FontsUtil.copyTempFontFile(FontsUtil.java:65)
	at com.pig4cloud.captcha.utils.FontsUtil.getFont(FontsUtil.java:45)
	at com.pig4cloud.captcha.base.Captcha.setFont(Captcha.java:299)
	at com.pig4cloud.captcha.base.Captcha.setFont(Captcha.java:295)
	at com.pig4cloud.captcha.base.Captcha.setFont(Captcha.java:291)
	at com.pig4cloud.captcha.base.Captcha.getFont(Captcha.java:277)

希望作者大大能够在代码后面加一段判断是否有文件夹分隔符的逻辑。

@VioletAyatan
Copy link
Author

虽然可以通过加命令行参数的方式解决,但多少还是只能算临时方案。希望作者能修复这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant