How to read a text file in a Struts 2 application

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

EDIT: Thanks guys for the answer. For me this worked:

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

Could you explain why I worked because I am taking the first steps in java.

+7
java-ee resources servlets configuration
source share
4 answers

If it is placed in the webapp class path, just use:

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

If it is placed in the global classpath, use:

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

If it is placed in webcontent just use:

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

The examples assume that they are placed in the root. Of course, you can use a relative path rather than the root root or webcontent root, for example. path/to/file.txt . You can get ServletContext in Struts at ServletActionContext#getServletContext() .

Edit: You edited your question as follows:

EDIT: Thanks guys for the answer. For me this worked:

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

Could you explain why

This is actually not the β€œright” way, but it is also doable. You only need to make sure that you know the relative path of the file, not the actual path of the GetPointsOfInterestAction class. Of course, this class is located in another package, so you basically need to turn the directory back (as in regular disk file systems: cd ../ , etc.). Again, this is not the most elegant way. You must use one of the first two of the above methods, with the resource name resources/visitor_attractions.txt .

+10
source share

Here

ServletActionContext.getServletContext()

You seem to know what to do next.

+3
source share

You can use the Resource Bundle . I use them to search for property files. I believe the directory should just be in the Classpath, and the ResourceBundle will find it.

+3
source share

If the file is in the classpath (this is not clear, but it would be nice), I would suggest using the utility class org.apache.struts2.util.ClassLoaderUtils . From his java documentation:

This class is extremely useful for loading resources and classes in failover mode, which runs on different application servers.

Pay particular attention to the static method ClassLoaderUtils.getResourceAsStream(String resourceName, Class callingClass) , which is a convenient method for loading a resource as a stream. As described in the document, the algorithm used to find the resource is given in getRessource() :

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

  • From Thread.currentThread().getContextClassLoader()
  • From ClassLoaderUtil.class.getClassLoader()
  • From callingClass.getClassLoader()
+2
source share

All Articles