How can one read a text file in a Struts 2 app [du

2019-03-16 15:03发布

Developing a Struts 2 app I run in a following problem. I need to read a text file that is deployed in web server with my app. How can I access it knowing its relative path. In other words how can I find absolute path if I know relative path inside the deployed directory. When I had similar problem with servlets I used to use this.getContextPath() (or something similar) that returned an absolute path to the folder in the webserver.

EDIT: Thank you guys for answer. For me worked this:

String path=GetPointsOfInterestAction.class.getResource("../../../resources/visitor_attractions.txt")

Could you please explain why I it worked because I'm making first steps in java.

4条回答
可以哭但决不认输i
2楼-- · 2019-03-16 15:34

If it is placed in the webapp's classpath, then just use:

InputStream input = servletContext.getResourceAsStream("file.txt");

If it is placed in the global classpath, then use:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");

If it is placed in the webcontent, then just use:

InputStream input = new FileInputStream(servletContext.getRealPath("file.txt"));

The examples assumes that they're placed in the root. You can of course use relative path as opposed to the classpath root or webcontent root, e.g. path/to/file.txt. You can get the ServletContext in Struts by ServletActionContext#getServletContext().

Edit: You edited your question with following:

EDIT: Thank you guys for answer. For me worked this:

String path=GetPointsOfInterestAction.class.getResource("../../../resources/visitor_attractions.txt")

Could you please explain why

This is actually not the "right" way, but this is also doable. You only need to make sure that you know the relative path of the file as opposed to the actual path of the GetPointsOfInterestAction class. This class is of course placed in a different package, so you basically need to step a directory back (like you do in normal disk file systems: cd ../ and so on). Again, this is not the most elegant way. You need to use one of the first two aforementioned ways, with the resource name resources/visitor_attractions.txt.

查看更多
成全新的幸福
3楼-- · 2019-03-16 15:41

If the file is on the classpath (which is not clear, but this would be a good idea), I'd suggest to use the utility classorg.apache.struts2.util.ClassLoaderUtils. From its java documentation:

This class is extremely useful for loading resources and classes in a fault tolerant manner that works across different applications servers.

Pay a special attention to the ClassLoaderUtils.getResourceAsStream(String resourceName, Class callingClass) static method which is a convenience method to load a resource as a stream. As documented, the algorithm used to find the resource is given in getRessource():

This method will try to load the resource using the following methods (in order):

  • From Thread.currentThread().getContextClassLoader()
  • From ClassLoaderUtil.class.getClassLoader()
  • From the callingClass.getClassLoader()
查看更多
狗以群分
4楼-- · 2019-03-16 15:47

You could use a Resource Bundle. I use them to look up properties files. I believe that the directory just needs to be on the Classpath and the ResourceBundle will find it.

查看更多
戒情不戒烟
5楼-- · 2019-03-16 15:56

Here

ServletActionContext.getServletContext()

You seem to know what to do from there.

查看更多
登录 后发表回答