9

フォントファイルにアクセスしようとすると、次のエラーが発生します。

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)

私のコードの下を見つけてください:

   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);
    }

何が問題になる可能性があるかを提案してください。私は考えが足りません。

ありがとうニック

4

1 に答える 1

19

new File(myURI)コンストラクターを使用しているため、その例外が発生しますが、 とmyURIは異なるスキーマがありますfile:

たとえば、これは機能します(メモファイル: //...):

System.out.println(new File(new URI("file:///etc/passwd")));

これは機能しませんが( http: //...に注意してください):

System.out.println(new File(new URI("http://localhost/etc/passwd")));

メソッドを使用する場合getResource()は、URL を操作する必要があります。常に「file:」スキーマを持つとは限りません。

リソース *.ttf ファイルから Font を作成する必要がある場合は、次のようにします。

URL url = this.getClass().getResource("/fonts/ARIALUNI.TTF");
InputStream is = url.openStream();
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
于 2011-09-03T21:55:16.037 に答える