error - java.lang.IllegalArgumentException:URI方案不是"文件"?

nic*_*ick 4 java uri illegalargumentexception file-uri

我在尝试访问字体文件时收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

请在下面找到我的代码:

   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);
    }
Run Code Online (Sandbox Code Playgroud)

请问可能是什么问题.我没有想法.

谢谢尼克

Mic*_*jer 15

你得到了那个例外,因为你使用的是new File(myURI)构造函数,而myURI不是schema file:.

例如,这将起作用(注意文件: // ...):

System.out.println(new File(new URI("file:///etc/passwd")));
Run Code Online (Sandbox Code Playgroud)

虽然这不起作用(注意http: // ...):

System.out.println(new File(new URI("http://localhost/etc/passwd")));
Run Code Online (Sandbox Code Playgroud)

如果要使用getResource()方法,则必须对URL进行操作.你不能假设它总是有"file:"模式.

如果您需要从资源*.ttf文件创建字体,您可以执行以下操作:

URL url = this.getClass().getResource("/fonts/ARIALUNI.TTF");
InputStream is = url.openStream();
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
Run Code Online (Sandbox Code Playgroud)