Hello,
I am unable to use ‘VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();’ in Vaadin 10 to get the absolute path. This was possible in Vaadin 8. Could anyone please help?
Hello,
I am unable to use ‘VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();’ in Vaadin 10 to get the absolute path. This was possible in Vaadin 8. Could anyone please help?
Hi Shruthi!
In V8 the implementation of getAbsolutePath()
was something like:
public File getBaseDirectory() {
final String realPath = VaadinServlet
.getResourcePath(getServlet().getServletContext(), "/");
if (realPath == null) {
return null;
}
return new File(realPath);
}
And the method VaadinServlet.getResourcePath()
:
protected static String getResourcePath(ServletContext servletContext,
String path) {
String resultPath = null;
resultPath = servletContext.getRealPath(path);
if (resultPath != null) {
return resultPath;
} else {
try {
final URL url = servletContext.getResource(path);
resultPath = url.getFile();
} catch (final Exception e) {
// FIXME: Handle exception
getLogger().log(Level.INFO,
"Could not find resource path " + path, e);
}
}
return resultPath;
}
So it should be simple to create an utility method that does the same in V10, something like:
public class ServletUtils {
public static File getBaseDirectory(VaadinServlet servlet) {
final String realPath = getResourcePath(servlet.getServletContext(), "/");
if (realPath == null) {
return null;
}
return new File(realPath);
}
protected static String getResourcePath(ServletContext servletContext,
String path) {
String resultPath = null;
resultPath = servletContext.getRealPath(path);
if (resultPath != null) {
return resultPath;
} else {
try {
final URL url = servletContext.getResource(path);
resultPath = url.getFile();
} catch (final Exception e) {
// FIXME: Handle exception
getLogger().log(Level.INFO,
"Could not find resource path " + path, e);
}
}
return resultPath;
}
}
And then use it like:
ServletUtils.getBaseDirectory(VaadinServlet.getCurrent());
Hope it helps!
Any news on this? I can’t get the path to the WEB-INF directory outside a servlet.
Hi Andreas!
Did you try my proposal?
Regards.
That’s fine! Works in Vaadin 13.