-
1. Re: How to get image path inside EJB bundled in EAR?
jaysensharma May 4, 2015 8:35 AM (in response to valsaraj007)1 of 1 people found this helpfulgetServletContext().getRealPath() is Servlet API. However for Application Servers and for the Enterprise Applications the String logoPath = "file://"+ this.getClass().getResource("/").getPath() + "/images"; approach is not good as there is no guarantee what path will be returned, because it is up to the implementation.
However you can use the [this.getClass().getClassLoader().getResourceAsStream("images/yourLogo.png");] approach to get the resource and to use it.try { InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("images/yourLogo.png"); FileOutputStream fout = new FileOutputStream (new File(System.getProperty("user.home")+"/yourLogo.png")); int i=0; while((i=inStream.read())!=-1){ fout.write((byte)i); } inStream.close(); } catch(Exception e) { e.printStackTrace(); } }
-
2. Re: How to get image path inside EJB bundled in EAR?
valsaraj007 May 5, 2015 12:26 AM (in response to jaysensharma)Hi Jay,
As per the above code, we are going to create duplicate files, right? ie, reading stream and writing in user's directory to use that path? Actually we need the path inside ear/web.war like jboss-8.2.0.Final\domain\servers\node-00\tmp\vfs\deployment\deploymenta54d265f6bf674d2\web.war88\images\test.png. This path is then set on a template file which will be used by the third party library to generate PDF. Reading stream and writes a file will take some CPU time as well. Is there any way to get this path inside web.war from EJB?
Thanks!
-
3. Re: How to get image path inside EJB bundled in EAR?
jameslivingston May 5, 2015 6:40 PM (in response to valsaraj007)According to the spec, the contents of a WAR is not available to other sub-deployments, so there is no portable way to do this. In addition, there is no guarantee that you can actually get a path to a file on disk, since the server does not have to explode archives for internal use.
If you need access to resources (like an image) from multiple sub-deployments, you could put them in a separate jar file in the EAR. If you need to serve them as static content from the web app, you'll want to have a servlet which finds the resources and copies the data to the output stream. On some servers (including WildFly8+) this will be less efficient since it involves application code rather than Undertow copying it.
Does the third party library accept URLs or InputStreams? If so, you should use those instead. If it only accepts File objects, then I think the only options are to use un-portable hacks, or find a better library.