Error - java.lang.IllegalArgumentException: URI scheme is not a "file"?

When I try to access the font file, I get the following error:

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) 

Below is the 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 suggests what might be the problem. I have no ideas.

Thanks Nick

+4
source share
1 answer

You get this exception because you use the new File(myURI) , and myURI has a difference scheme than file:

For example, this will work (note the file: // ...):

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

whereas it does not work (note http: // ...):

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

If you want to use the getResource() method, you need to work with the URL. You cannot assume that it will always have a file: scheme.

If you need to create a Font from a * .ttf resource file, you can do:

 URL url = this.getClass().getResource("/fonts/ARIALUNI.TTF"); InputStream is = url.openStream(); Font font = Font.createFont(Font.TRUETYPE_FONT, is); 
+14
source

All Articles