如何获取 WebContent 文件夹中文件的真实路径?

发布于 2024-09-01 00:30:08 字数 144 浏览 5 评论 0原文

我需要获取 WebContent 目录中文件的真实路径,以便我使用的框架可以访问该文件。它只接受 String 文件作为属性,所以我需要在 WebContent 目录中获取该文件的真实路径。

我使用Spring框架,所以解决方案应该可以在Spring中制定。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

时光清浅 2024-09-08 00:30:08

如果您在 servlet 中需要此功能,请使用 getServletContext().getRealPath("/filepathInContext")!

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

夜还是长夜 2024-09-08 00:30:08

getServletContext().getRealPath("") - 如果内容可从 .war 存档中获取,则此方法将不起作用。 getServletContext() 将为 null。

在这种情况下我们可以使用另一种方式来获取真实路径。这是获取属性文件 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");

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");
诗酒趁年少 2024-09-08 00:30:08

你必须告诉java将路径从你的电脑更改为你的java项目,这样
如果你使用 spring 使用:

@Autowired
ServletContext c;

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

但是如果你使用 servlet 只需使用

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

,路径将是 /webapp/images/ :)

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

哎呦我呸! 2024-09-08 00:30:08

在这种情况下,我倾向于提取所需的内容作为资源 (MyClass.getClass().getResourceAsStream()),将其作为文件写入临时位置,并使用该文件进行其他调用。

这样,我就不必费心处理仅包含在 jar 中或位于某个位置(取决于我当前使用的 Web 容器)的内容。

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.

万水千山粽是情ミ 2024-09-08 00:30:08

包含请求作为参数。 Spring 在调用映射方法时将传递请求对象

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

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");
   ...
记忆之渊 2024-09-08 00:30:08

您可以使用 Spring Resource 接口(尤其是 ServletContextResource):

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

意中人 2024-09-08 00:30:08

此方法使用资源加载器获取应用程序中文件的绝对路径,然后向上移动几个文件夹到应用程序的根文件夹。不需要 servlet 上下文!如果您的 WEB-INF 文件夹中有“web.xml”,这应该可以工作。请注意,您可能需要考虑仅将其用于开发,因为这种类型的配置通常最好存储在应用程序外部。

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

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;
}
扎心 2024-09-08 00:30:08

我的解决方案:..../webapps/mydir/ (../webapps/ROOT/../mydir/)

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

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

String dir = request.getSession().getServletContext().getRealPath("/")+"/../mydir";
            Files.createDirectories(Paths.get(dir));
乱世争霸 2024-09-08 00:30:08

当您也想在开发和生产级别中使用 arff.txt 时,请尝试使用此选项

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文