How can I get real path for file in my WebContent

2020-02-09 11:18发布

问题:

I need to get real path for file in my WebContent directory, so that framework that I use can access that file. It only takes String file as attribute, so I need to get the real path to this file in WebContent directory.

I use Spring Framework, so solution should be possible to make in Spring.

回答1:

If you need this in a servlet then use getServletContext().getRealPath("/filepathInContext")!



回答2:

getServletContext().getRealPath("") - This way will not work if content is being made available from a .war archive. getServletContext() will be null.

In this case we can use another way to get real path. This is example of getting a path to a properties file C:/Program Files/Tomcat 6/webapps/myapp/WEB-INF/classes/somefile.properties:

// URL returned "/C:/Program%20Files/Tomcat%206.0/webapps/myapp/WEB-INF/classes/"
URL r = this.getClass().getResource("/");

// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
String decoded = URLDecoder.decode(r.getFile(), "UTF-8");

if (decoded.startsWith("/")) {
    // path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
    decoded = decoded.replaceFirst("/", "");
}
File f = new File(decoded, "somefile.properties");


回答3:

you must tell java to change the path from your pc into your java project so if you use spring use :

@Autowired
ServletContext c;

String UPLOAD_FOLDEdR=c.getRealPath("/images"); 

but if you use servlets just use

String UPLOAD_FOLDEdR = ServletContext.getRealPath("/images");  

so the path will be /webapp/images/ :)



回答4:

In situations like these I tend to extract the content I need as a resource (MyClass.getClass().getResourceAsStream()), write it as a file to a temporary location and use this file for the other call.

This way I don't have to bother with content that is only contained in jars or is located somewhere depending on the web container I'm currently using.



回答5:

Include the request as a parameter. Spring will then pass the request object when it calls the mapped method

@RequestMapping .....
public String myMethod(HttpServletRequest request) {
   String realPath = request.getRealPath("/somefile.txt");
   ...


回答6:

You could use the Spring Resource interface (and especially the ServletContextResource): http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/core/io/Resource.html



回答7:

This approach uses the resource loader to get the absolute path to a file in your app, and then goes up a few folders to the app's root folder. No servlet context required! This should work if you have a "web.xml" in your WEB-INF folder. Note that you may want to consider using this solely for development, as this type of configuration is usually best stored externally from the app.

public String getAppPath()
{
    java.net.URL r = this.getClass().getClassLoader().getResource("web.xml");
    String filePath = r.getFile();
    String result = new File(new File(new File(filePath).getParent()).getParent()).getParent();

    if (! filePath.contains("WEB-INF"))
    {
        // Assume we need to add the "WebContent" folder if using Jetty.
        result = FilenameUtils.concat(result, "WebContent");
    }

    return result;
}


回答8:

try to use this when you want to use arff.txt in your development and production level too

  String path=getServletContext().getRealPath("/WEB-INF/files/arff.txt");