1

i am try to read the file using ServletContext `

InputStream is = servletContext.getResourceAsStream(path)

the value of path is :

path = D:\Assignments\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\HelpGuide\GeneratedReports\userDetail.pdf`

the userDetail.pdf file is exist in given path , but when i try to get the resource as stream using servlet context define as above , the value of is is null .

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

2 Answers2

1

This is because the getResourceAsStream looks to a path relative to the context root. Checkout javadoc of servlet context and especially the "getResource" part: http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String) You should use a relative path and put your pdf in your classpath, that would be a better practice as your app will not rely on an absolute path.

benzonico
  • 10,635
  • 5
  • 42
  • 50
0

This is such a bad idea, as the servlet is telling you.

Put that PDF in the CLASSPATH (e.g. WEB-INF/classes of your WAR) and read it as an InputStream from the servlet context.

Absolute paths are the wrong way to go for web apps.

duffymo
  • 305,152
  • 44
  • 369
  • 561