Font.createFont 将文件保留在临时目录中
下面的代码完成其工作,但每次运行时都会在临时目录中留下字体文件的副本。这些文件名为 +~JF7154903081130224445.tmp,其中每个创建的文件的数字似乎是随机的。
InputStream fontStream = this.getClass().getResourceAsStream("handsean.ttf");
Font baseFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
fontStream.close();
我在 sun.com 的论坛和网络上的其他资源中发现了多年的讨论,其中这被认为是 JDK 中的错误,从 1.5.0_06 升级到 1.5.0_08 可以解决该问题;但是,我使用的版本是更高版本(1.6.0_13)。
我尝试通过在字体相关操作完成后删除文件来解决问题,但当时文件被锁定。 仅当 Web 应用程序停止后才能删除这些文件。
有人有解决办法吗?
The code below does its work but leaves copies of the font file in the temp directory each time it is run. These files are named +~JF7154903081130224445.tmp where the number seems random for each created file.
InputStream fontStream = this.getClass().getResourceAsStream("handsean.ttf");
Font baseFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
fontStream.close();
I have found years-old discussions in forums at sun.com and other resources on the web where this is recognized as a bug in JDK, where upgrading from 1.5.0_06 to 1.5.0_08 would solve the problem; however, the version I am using is a later version (1.6.0_13).
I tried solving the problem by deleting the files after the font related operations are finished, but the files are locked at that time.
The files can only be deleted after the web application has stopped.
Does anybody have a solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 ttf 文件不在存档内,您可以调用 createFont(File) 而不是 createFont(InputStream)
据我所知,这个错误存在于 Java 6 中,查看 Font 类的源代码就足够了。
If your ttf files are not inside an archive, you can call createFont(File) instead of createFont(InputStream)
As to the best of my knowledge, this bug exists in Java 6, it is enough to look at the sources of Font class.
在 JDK1.6.0_16 中,字体管理器似乎使用临时文件作为一种缓存,并且仅在需要时才从字体中读取字形。它还添加了一个关闭钩子,该钩子将在 JVM 正常终止时删除该文件。根据虚拟机的不同,字体渲染可能也委托给需要访问文件的本机代码,因此对文件保持锁定对我来说似乎是合理的。
即使您的 servlet 容器(您提到的是 Web 应用程序)定期终止,文件是否确实保留,或者您是否在不允许它正确清理其资源的情况下杀死了它?
With JDK1.6.0_16, the font manager seem to be using the temporary file as a kind of cache and will only read glyphs from the font when they are required. It is also adding a shutdown hook, which will delete the file when the JVM terminates ordinarily. Depending on the VM, font rendering is perhaps also delegated to native code which needs access to the file, so keeping a lock on the file seems reasonable to me.
Are the files actually kept, even if your servlet container (you are mentioning a web application) terminates regularly, or are you killing it without allowing it to cleanup its resources properly?