从 Web 服务访问文件
我使用 netbeans 创建一个 Web 服务,并使用 Glassfish 作为服务器在 netbeans 中测试它。
我有一个文件,我希望网络服务能够从中读取数据并可能向其中写入数据。但我该把文件放在哪里。如果“course”是我的netbeans项目根目录,我尝试将文件放在以下位置:
\Course
\Course\xml-resources\jaxb\FlightRequest
\Course\web
\Course\web\WEB-INF
\Course\src\java\org\me\FBooking
\Course\build\web
\Course\build\web\WEB-INF
\Course\build\web\WEB-INF\classes
并尝试在我的解组代码中的Web服务中访问它(因为我尝试访问的文件是一个xml文档):
un = (AvailableFlights) unmarshaller.unmarshal(new java.io.File("AvailableFlights.xml"));
但是它找不到该文件
那么我应该把它放在哪里?
I am using netbeans to create a web service and using Glassfish as the server to test it within netbeans.
I have a file that i wish the web service to be able to read data from and possibly write to it. But where do i put the file. If 'course' is my netbeans project root i have tried placing the file in the following locations:
\Course
\Course\xml-resources\jaxb\FlightRequest
\Course\web
\Course\web\WEB-INF
\Course\src\java\org\me\FBooking
\Course\build\web
\Course\build\web\WEB-INF
\Course\build\web\WEB-INF\classes
and tried accessing it in the web service in my unmarshalling code using (as the file i am trying to access is an xml document):
un = (AvailableFlights) unmarshaller.unmarshal(new java.io.File("AvailableFlights.xml"));
But it cant find the file
So where am i supposed to place it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要写入该文件,则应该将该文件定位在可部署代码之外。
如果您只需要只读访问权限,则将文件放入 web/ 将使文件可以通过 Web 浏览器访问。那可能不是你想要的。
如果您将该文件放在 WEB-INF/classes 下,您的代码就可以访问该文件,但不会公开暴露。
用于访问该文件的代码片段仅适用于文件系统上的文件,而不适用于作为 WAR 的一部分部署的文件,因此如果您决定将其打包为 WAR 的一部分,则需要研究加载该文件的其他方法。你的战争。
If you need to write to the file, you should locate the file outside of your deployable code.
If you only need read-only access, putting the file in web/ will make the file accessible from a web browser. That may not be what you want.
If you put the file under WEB-INF/classes, it will be accessible to your code, but not publicly exposed.
Your code fragment for accessing the file will only work for files on the file system, and not for files you deploy as part of the WAR, so you need to look into other ways of loading the file if you decide to package it as part of your WAR.
看一下
哪个应该能够读取 WAR 文件中的文件。 (现在还没有测试过..)但这仅适用于从文件中读取。
Take a look at
which should be able to read from files within a WAR file. (Haven't tested it right now..) But this is only for reading from the file.