How can I get real path for file in my WebContent folder?

落花浮王杯 提交于 2019-11-29 02:02:23

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

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");
Raslan

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/ :)

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.

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");
   ...

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

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;
}

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!