简体   繁体   English

web项目中的Java文件路径

[英]Java file path in web project

I need to access the resource files in my web project from a class.我需要从一个类访问我的 web 项目中的资源文件。 The problem is that the paths of my development environment are different from the ones when the project is deployed.问题是我的开发环境的路径和项目部署时的路径不一样。

For example, if I want to access some css files while developing I can do like this:例如,如果我想在开发时访问一些 css 文件,我可以这样做:

File file = new File("src/main/webapp/resources/styles/some.css/");

But this may not work once it's deployed because there's no src or main directories in the target folder.但是一旦部署,这可能不起作用,因为目标文件夹中没有srcmain目录。 How could I access the files consistently?我怎样才能始终如一地访问文件?

You seem to be storing your CSS file in the classpath for some unobvious reason.您似乎出于某种不明显的原因将 CSS 文件存储在类路径中。 The folder name src is typical as default name of Eclipse's project source folder.文件夹名称src是典型的 Eclipse 项目源文件夹的默认名称。 And that it apparently magically works as being a relative path in the File constructor (bad, bad), only confirms that you're running this in the IDE context.并且它显然神奇地作为File构造函数中的相对路径(坏,坏),只能确认您在 IDE 上下文中运行它。

This is indeed not portable.这确实不便携。

You should not be using File 's constructor.您不应该使用File的构造函数。 If the resource is in the classpath, you need to get it as resource from the classpath.如果资源在类路径中,则需要从类路径中将其作为资源获取。

InputStream input = getClass().getResourceAsStream("/main/webapp/resources/styles/some.css");
// ...

Assuming that the current class is running in the same context, this will work regardless of the runtime environment.假设当前类在相同的上下文中运行,无论运行时环境如何,这都将起作用。

See also:也可以看看:


Update : ah, the functional requirement is now more clear.更新:啊,功能需求现在更清楚了。

Actually I want to get lastModified from the file.实际上我想从文件中获取 lastModified 。 Is it possible with InputStream? InputStream 有可能吗? ——

Use getResource() instead to obtain it as an URL .使用getResource()来获取它作为URL Then you can open the connection on it and request for the lastModified .然后你可以打开它的连接并请求lastModified

URL url = getClass().getResource("/main/webapp/resources/styles/some.css");
long lastModified = url.openConnection().getLastModified();
// ...

If what you're looking to do is open a file that's within the browser-visible part of the application, I'd suggest using ServletContext.getRealPath(...)如果您想要做的是打开应用程序浏览器可见部分内的文件,我建议使用ServletContext.getRealPath(...)

Thus:因此:

File f = new File(this.getServletContext().getRealPath("relative/path/to/your/file"));

Note: if you're not within a servlet, you may have to jump through some additional hoops to get the ServletContext , but it should always be available to you in a web environment.注意:如果您不在 servlet 内,您可能需要跳过一些额外的环节来获取ServletContext ,但它应该始终在 Web 环境中可供您使用。 This solution also allows you to put the .css file where the user's browser can see it, whereas putting it under /WEB-INF/ would hide the file from the user.此解决方案还允许您将 .css 文件放在用户浏览器可以看到的地方,而将其放在/WEB-INF/会向用户隐藏该文件。

Put your external resources in a sub-directory of your project's WEB-INF folder.将外部资源放在项目WEB-INF文件夹的子目录中。 Eg, put your css resources in WEB-INF/styles and you should be able to access them as:例如,将您的 css 资源放在WEB-INF/styles ,您应该能够以如下方式访问它们:

new File("styles/some.css"); new File("styles/some.css");

Unless you're not using a standard WAR for deployment, in which case, you should explain your setup.除非您不使用标准 WAR 进行部署,在这种情况下,您应该解释您的设置。

Typically resource files are placed in your war along with your class files.通常,资源文件与类文件一起放置在您的战争中。 Thus they will be on the classpath and can be looked up via因此,它们将在类路径上,可以通过以下方式查找

getClass.getResource("/resources/styles/some.css")

or by opening a File as @ig0774 mentioned.或者通过@ig0774 提到的打开文件。

If the resource is in a directory that is not deployed in the WAR (say you need to change it without redeploying), then you can use a VM arg to define the path to your resource.如果资源位于未在 WAR 中部署的目录中(假设您需要在不重新部署的情况下更改它),那么您可以使用 VM arg 来定义资源的路径。

-Dresource.dir=/src/main/webapp/resources

and do a lookup via that variable to load it.并通过该变量进行查找以加载它。

In Java web project, the standard directory like:在 Java web 项目中,标准目录如:

{WEB-ROOT} / 
           /WEB-INF/
           /WEB-INF/lib/
           /WEB-INF/classes

So, if you can get the class files path in file system dynamic, you can get the resources file path.所以,如果你可以在文件系统动态中获取类文件路径,你就可以获取资源文件路径。

you can get the path ( /WEB-INF/classes/ ) by:您可以通过以下方式获取路径( /WEB-INF/classes/ ):

this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()

so, then the next ...那么接下来...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM