Completely troubleshoot file path issues with Java Web Projects (War packages)

Source: Internet
Author: User
Tags java web jboss

In the Java EE Project, we usually publish the project, usually the war package, as a standalone unit, able to remotely upload, load, publish, and can achieve version number control, but this is different from our own proactive deployment using myeclipse in the development process, the following specific instructions.

With the war package deployment, there is a very big difference between Tomcat and WebLogic, and Tomcat will actively pressurize the war to the current folder and then use the Unpacked folder to advertise it, which is generally not too much of an accident, but the assumption is that it is published with WebLogic. Will not be decompressed, assuming that the development process is not aware of the war package after the publication of the possibility of a file can not find the phenomenon. For example: Use the following method to get the path:


This is with the absolute URI path, for example:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/. The path with the drive letter, apparently unable to find the file in the war based on the disk character

Another way:

So how can you get it?

Tested. This is the URI folder that gets the current class, for example:/d:/jboss-4.2.2.ga/server/default/deploy/hp.war/web-inf/classes/com/jebel/helper/is also an absolute path, Obviously not applicable to war packages.

In fact, both of these approaches are in the fork, as reading the file does not necessarily read the path. File operations are generally converted to a stream, since to read the file, it is better to read directly into the input stream, there is less one-step encapsulation. Look at the following ways:

InputStream is= This.getclass (). getResourceAsStream ("/config/bctcms/" + templatefilename);
Read the Classes folder under Folder config/bctcms/. The file name is called the Templatefilename input stream. Tested in the war can be read normally.

There is a disadvantage to this method. Only the files under the Classes folder can be read. The inability to file files under other folders is obviously not appropriate for all scenarios.

Assume that the file is under the Web-inf folder. How to read it?

The answer is to use the servletcontext.getresourceasstream (String) method.

That is, the context information is obtained first, and then through the absolute path to root in the project folder. Find the file, for example:

InputStream is= Context.getresourceasstream (TemplatePath + "/" + templatefilename); templatepath= "/WEB-INF/classes/ config/bctcms/"Templatefilename=" Source.xls "
You can see that TemplatePath is the path relative to the context root, not relative to classes, so that even if the file is under Web-inf other folders. can also be found smoothly. After testing, the situation in the war is well supported.

Take a look at the API documentation for Servletcontext.getresourceasstream ,
Returns a URL to the resource, that's mapped to a specified path. The path must begin with a "/" and are interpreted as relative to the current context root.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system with a database, or in a. war file.

I'm sure we can all understand. You don't have to repeat it. There is just one question, what is the context? Answer: ServletContext. Contextual information, which is obtained using request in the Java EE class. Such as:

ServletContext context = Request.getsession (). Getservletcontext ();
So how do you get it in the ordinary class? A temporary, especially good way. Using application is one way, the second way is to try to go to the request object successively, such as:

Requestattributes RA = requestcontextholder.getrequestattributes (); HttpServletRequest request = ((servletrequestattributes) RA). Getrequest (); this.context = Request.getsession (). Getservletcontext ();
It is also possible to obtain a context

But the assumption is that the input stream can be obtained directly from the JSP:

Application.getresourceasstream ("xxx");

Path problem do not dwell too long, according to the above approach to solve, it is best to useServletContext method to get. Just need to know a situation to be able to. The most important thing is to achieve the goal. Right?

There is not the right place, welcome you to do supplementary correction!

Update: When using the Spring Timer: request and ServletContext are not available, then use this method to get ServletContext directly:

Contextloader.getcurrentwebapplicationcontext (). Getservletcontext ()

update20151228:

When you write a file, you may need to obtain a path, such as when uploading a file that requires an output stream. the output stream cannot be obtained directly through ServletContext, but the actual path can be obtained directly. such as:

Servletcontext.getrealpath ("/")

The path obtained is the ${context}/path, and the output stream is obtained based on the subpath and file name.




Completely troubleshoot file path issues with Java Web Projects (War packages)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.