且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

tomcat在哪里存储我的文件?

更新时间:2023-09-21 16:54:16

最终,您的应用程序将确定文件的存储位置.

Ultimately, it is what your application does that determines where the files are stored.

听起来,您的应用程序正在将文件存储在JVM的当前目录中,当您通过NetBeans启动Web服务器时,该目录恰好是"bin"目录.如果是这样,您将在Tomcat作为Windows服务启动时在当前目录中的任何位置找到它们.

By the sounds of it, your application is storing files in the current directory of the JVM, which happens to be the "bin" directory when you launch the web server via NetBeans. If so, you will find them, in whatever the current directory is when Tomcat is launched as a windows service.

坦率地说,我认为您错了.您应该明智地决定要存储的是 上传的文件,然后确保所使用的上传机制将它们放置在那里.

Frankly, I think you've got this wrong. You should be making a conscious decision as to were uploaded files should be stored, and then making sure that the upload mechanism you are using puts them there.

显然,将它们放在当前目录中不是一个好主意.您不希望根据启动Web容器的方式将它们存储在不同的位置.显然,"bin"目录是不合适的地方. (如果用户尝试上传名称与"bin"中存在的脚本之一匹配的文件,会发生什么?)

Obviously, putting them in the current directory is a bad idea. You don't want them being stored in different places depending on how the web container is started. And obviously the "bin" directory is an inappropriate place. (What happens if the user tries to upload a file whose name matches one of the scripts that live in "bin"?)

那么您将文件放置在 的哪个位置?

So where should you be putting the files?

我认为,您有三种选择:

In my opinion, you've got three choices:

  • work目录的子目录中,这是Tomcat通常在其中放置临时文件(例如编译的JSP)的地方.

  • In a subdirectory of the work directory ... which is where Tomcat conventionally puts transitory files such as compiled JSPs.

在Tomcat安装目录的自定义子目录中.

In a custom subdirectory of the Tomcat installation directory.

在文件系统中其他位置的单独目录中.

In a separate directory somewhere else in the file system.

您不应再将其放置在webapp目录中,因为重新部署webapp时文件通常会被吹走,并且上传的文件会干扰您的webapp风险更大.

You shouldn't be dropping then in the webapp directory, because files there are typically blown away when the webapp is redeployed, and because there's a greater risk that uploaded files will interfere with your webapp.

您不应该将它们放入bin或log或lib或config目录中,因为这样做可能会造成干扰……而且它们根本不是逻辑所在.

You shouldn't be dropping them in the bin or logs or lib or config directories because of the risk of interference ... and because they are simply not the logical place.

如果要编写相对于tomcat安装目录根目录的文件,则可以通过调用System.getProperty("catalina.base")找出问题所在.

If you want to write files relative to the root of the tomcat installation directory, you can find out what that is by calling System.getProperty("catalina.base").

但是无论您做什么,都需要确保用户不会意外或故意将文件上传到错误的位置;例如,提供绝对路径名,或使用"../../...."从您的上传区域中逃脱的路径名.

But what ever you do, you need to make sure that a user can't accidentally or deliberately upload files to the wrong place; e.g by supplying an absolute pathname, or a pathname that uses "../../...." to escape from your upload area.