gpt4 book ai didi

java - 服务程序 : try to read pdf file using servlet in eclipse

转载 作者:太空宇宙 更新时间:2023-11-04 07:47:28 25 4
gpt4 key购买 nike

我尝试使用 ServletContext 读取文件`

InputStream is = servletContext.getResourceAsStream(path)

路径的值为:

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

userDetail.pdf文件存在于给定路径中,但是当我尝试使用上面定义的 servlet 上下文以流形式获取资源时, is 的值为空。

最佳答案

这是因为 getResourceAsStream 查找相对于上下文根的路径。查看 servlet 上下文的 javadoc,尤其是“getResource”部分:http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)您应该使用相对路径并将 pdf 放入类路径中,这将是更好的做法,因为您的应用程序不会依赖绝对路径。

关于java - 服务程序 : try to read pdf file using servlet in eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com