Obviously, a third-party JAR expects a local URI based on the file system on disk. This is actually a mistake on their side. It is possible to retrieve content via uri.toURL().openStream() without worrying about the context the URI is sitting on. The rejection of the cost suggests that for this the third-party uses new FileInputStream(new File(uri)) . First of all, this should be reported on their side so that they can correctly fix it.
At the same time, it is best to convert it to a local URI based on the local file system instead of a virtual file system or web URI. This can be done by creating a temporary file using File#createTempFile() in a temporary folder managed by the container, entering the contents into it, and finally providing the temporary file as a URI.
The example below assumes that you are inside the servlet and thus have getServletContext() on hand. Otherwise, the Java EE environment you are using should be able to give / enter you a ServletContext .
String tempDir = (String) getServletContext().getAttribute(ServletContext.TEMPDIR); File tempFile = File.createTempFile("temp-", ".properties", new File(tempDir)); Files.copy(propertiesUrl.openStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
However, are you absolutely sure that this properties file should not be placed in the /WEB-INF folder? Right now, it is publicly available to anyone in the world with a web browser. See Also ao Where to place and how to read configuration resource files in a servlet application?
source share