错误 - java.lang.IllegalArgumentException:URI 方案不是“文件"? [英] error - java.lang.IllegalArgumentException: URI scheme is not "file"?
问题描述
我在尝试访问字体文件时收到以下错误:
I am receiving the following error when trying to access font file :
011.08.31 12:12:42.704 ERROR [PDFOutputHandler] - Unable to resolve Unicode font
java.lang.IllegalArgumentException: URI scheme is not "file"
at java.io.File.<init>(File.java:366)
at com.xx.reports.output.handler.PDFOutputHandler.addUnicodeFont(PDFOutputHandler.java:393)
at com.xx.reports.output.handler.PDFOutputHandler.renderOutput(PDFOutputHandler.java:104)
at com.xx.reports.output.handler.PDFOutputHandler.renderOutput(PDFOutputHandler.java:134)
at com.xx.reports.output.appender.PdfAppender.renderOutput(PdfAppender.java:103)
at com.xx.reports.servlet.BasePdfOutputServlet.setResponsePdf(BasePdfOutputServlet.java:53)
at com.xx.reports.servlet.JSPToPDFServlet.execute(JSPToPDFServlet.java:115)
at com.xx.reports.servlet.JSPToPDFServlet.doGet(JSPToPDFServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
请在下面找到我的代码:
Please find below my code:
try
{
if (unicodeFontPath == null)
{
URI fontClassURI = new URI(this.getClass().getResource("/fonts/ARIALUNI.TTF").toString());
unicodeFontPath = new File(fontClassURI).getAbsolutePath();
}
renderer.getFontResolver().addFont(unicodeFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (Exception e)
{
logger.error("Unable to resolve Unicode font", e);
}
请提出可能是什么问题.我没有想法.
PLease suggest what could be the issue. I am out of ideas.
谢谢尼克
推荐答案
你得到那个异常,因为你使用了 new File(myURI)
构造函数,而 myURI
有不同于 file:
的架构.
You get that exception, because you are using new File(myURI)
constructor, while myURI
has differet schema than file:
.
例如,这将工作(注意文件://...):
For example, this will work (note file://...):
System.out.println(new File(new URI("file:///etc/passwd")));
虽然这不起作用(注意http://...):
while this will not work (note http://...):
System.out.println(new File(new URI("http://localhost/etc/passwd")));
如果你想使用getResource()
方法,那么你必须对URL进行操作.你不能假设它总是有文件:"模式.
If you want to use getResource()
method, then you have to operate on URL. You cannot assume it will always have "file:" schema.
如果你需要从资源 *.ttf 文件中创建字体,你可以这样做:
If you need to create Font from resource *.ttf file, you can do:
URL url = this.getClass().getResource("/fonts/ARIALUNI.TTF");
InputStream is = url.openStream();
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
这篇关于错误 - java.lang.IllegalArgumentException:URI 方案不是“文件"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!