getResourceAsStream () path problem

tags: getResourceAsStream()  Javaweb path

Take the example tomcat comes to do the project examples:

        // "/" with respect to E: \ app \ apache-tomcat-8.5.31 \ webapps \ examples
        session.getServletContext().getResourceAsStream("/");

                 // relative to the E: \ app \ apache-tomcat-8.5.31 \ webapps \ examples \ WEB-INF \ classes
        this.getClass().getResourceAsStream("/");

                 // same:. This.getClass () getResourceAsStream ( "/") ;, but do not write "/"
        this.getClass().getClassLoader().getResourceAsStream("");

From the tested code can be seen

1. session.getServletContext().getResourceAsStream("/");  Equivalent to the current project directory compiled, "/" represents the root directory of the current project;

2. this.getClass().getResourceAsStream("/");Equivalent WEB-INF \ classes, "/" on behalf of classes directory (compiled src directory) under the current project;

3. this.getClass().getClassLoader().getResourceAsStream("");And this.getClass () getResourceAsStream ( "/");.. The same directory beginning, the difference is not to write "/", in fact getClass () getResourceAsStream ( "/") is called a source ingetClassLoader()Method, write without any increase in forward slashes.

 

Intelligent Recommendation

Solve the null problem with getResourceAsStream in IntelliJ IDEA

To solve this problem is very simple: windows version press the shortcut key alt + shift + ctrl + s mac:command + ; Enter the module configuration, select the module you are using, click sources, crea...

ClassLoader solve the getResource, getResourceAsStream space problem

The method uses a ClassLoader getResource utf-8 encoded path information, when the pathThe presence of Chinese and space, he will convert these characters, so that, more often than not get what we wan...

GetresourceASSTREAM in Java Return to NULL problem processing

GetresourceASSTREAM in Java Return to NULL problem processing: The main reason for this problem is the setting of the source: ​ In our commonly used IDEA, the input stream returned with GetResourceASS...

ClassLoader's getResourceAsStream method usage and path issues in java and web projects

ClassLoader, a small-name class loader, sounds so tall, the explanation of the online god is esoteric, just getting started, it is difficult to understand. My understanding of it is to load some files...

IDEA failed to read the configuration file using getResourceAsStream of the problem

Directory Structure: debug finds that in is empty, the code is not wrong, only the content of the db.properties file is not read The first feeling is that there may be a problem with the resources fil...

More Recommendation

getResourceAsStream and getClassLoader (). getResourceAsStream

Directly on the map, to facilitate comparison I do not understand even a path in which the above configuration, oh, on the map, even though some of my colleagues in the eyes of eclise have become anti...

java getResourceAsStream

[size=large][b]Class's getResourceAsStream(String path)[/b][/size] 1) Relative and absolute paths can be used, and absolute paths start with '/', such as 2) The relative path is the file under the cur...

getResource and getResourceAsStream

Foreword In java there are some differences in resource acquisition path, commonly used getResource and getResourceAsStream 1.Class.getResource(String path)       path not to '/' a...

For Class getResourceAsStream and ClassLoader getResourceAsStream methods

                This is a very basic question, here are some examples to help you quickly understand and remember the problem...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top