开发者

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

开发者 https://www.devze.com 2022-12-29 05:33 出处:网络
I nee开发者_如何转开发d 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

I nee开发者_如何转开发d 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.


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


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


my solve for: ..../webapps/mydir/ (..../webapps/ROOT/../mydir/)

String dir = request.getSession().getServletContext().getRealPath("/")+"/../mydir";
            Files.createDirectories(Paths.get(dir));


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

精彩评论

暂无评论...
验证码 换一张
取 消