Where should I store persistent files in a Tomcat web app ?
在Tomcat web应用程序中应该在哪里存储持久文件?
- javax.servlet.context.tempdir is not feasible, it's erased when the app is redeployed/removed
- javax.servlet.context。tempdir不可行,当应用程序被重新部署/删除时,它会被删除。
- Don't want to use an absolute path in e.g. servlet init parameters
- 不要在servlet init参数中使用绝对路径
- Storing the files in a database is not an option
- 在数据库中存储文件不是一个选项
5 个解决方案
#1
9
Our team does this a lot. A general rule we follow is outside the web app and outside Tomcat.
我们的团队经常这样做。我们遵循的一般规则是在web应用程序之外,在Tomcat之外。
Our sysadmin set up a directory on our server that the tomcat user has rw permissions to (e.g. /var/tomcat/persist
). We have a built a directory structure under this that tomcat uses to store files, read app-specific init files, etc.
我们的sysadmin在服务器上设置了一个目录,tomcat用户具有rw权限(例如/var/tomcat/persist)。我们在此基础上构建了一个目录结构,tomcat使用该结构来存储文件、读取特定于应用程序的init文件等。
If you don't want to use an absolute path in your init-params for your servlet, consider setting a system property when tomcat is started up. The good thing about that is every application running under tomcat will have access to it. The bad thing about that is every application running under tomcat will have access to it. You could set a property named base.persist.dir
and build subdirectories for each application underneath it. We set system properties in the setenv.sh script in the bin/ directory under the CATALINA_OPTS environment variable.
如果您不想在您的init-params中为您的servlet使用绝对路径,请考虑在tomcat启动时设置系统属性。这样做的好处是,在tomcat下运行的每个应用程序都可以访问它。最糟糕的是,在tomcat下运行的每个应用程序都可以访问它。您可以设置一个名为base.persist的属性。然后为它下面的每个应用程序创建子目录。我们在setenv中设置系统属性。CATALINA_OPTS环境变量下的bin/目录中的sh脚本。
#2
5
Answering the title of the question, what about using a database, a DataSource
and JDNI? Even in a web only context, writing to files using java.io
is not really recommended because of concurrency, threading, security, clustering, portability issues. Some of these problems can be "workarounded" but still, this is not really a best practice. The standard approach is to use a database and I'd suggest to reconsider this option, throwing "file-based" lightweight database like HSQLBD or JavaDB into the mix.
回答问题的标题,使用数据库、数据源和JDNI怎么样?即使是在仅在web上下文中,也要使用java编写文件。由于并发性、线程化、安全性、集群性和可移植性问题,不建议使用io。有些问题可能会被“解决”,但这并不是最佳实践。标准的方法是使用数据库,我建议重新考虑这个选项,将“基于文件的”轻量级数据库(如HSQLBD或JavaDB)加入其中。
(EDIT: For an unknown reason, database is not an option. Using JNDI or context parameters or init parameters to pass an absolute path - which are the less worse options IMHO - is excluded too. For a relative path, maybe look at user.home
or user.dir
then - or any other system property that you could pass on the command line. I don't like it, I wouldn't do it, and this doesn't solve the issues previously mentioned, but it's your choice after all.)
(编辑:出于未知的原因,数据库不是一个选项。使用JNDI或上下文参数或init参数传递绝对路径(IMHO是较差的选项)也被排除。对于相对路径,可以考虑用户。家里或用户。然后是dir——或您可以在命令行上传递的任何其他系统属性。我不喜欢它,我不会做它,这并不能解决前面提到的问题,但这毕竟是你的选择。
#3
4
Storing the files in a webapp directory under the home directory of the user running Tomcat is a good and convenient option. It is outside of Tomcat, which means it will survive redeployment, and it is usually a writable directory (because it is created under the users' home dir). But it is always a good idea to allow overriding the location of such directory via system property.
将文件存储在运行Tomcat的用户的主目录下的webapp目录中是一个很好的选择。它位于Tomcat之外,这意味着它可以在重新部署后继续存在,而且它通常是一个可写目录(因为它是在用户的home dir下创建的)。但是,允许通过系统属性覆盖该目录的位置总是一个好主意。
#4
1
Generally, this would go to the database. But since the OP insists on not using a database, I'd try a different approach:
通常,这将进入数据库。但由于OP坚持不使用数据库,我将尝试另一种方法:
- Filesystem path which is known:
${user.home}/.myapp
. Applications sometimes use this for e.g. search indices which can be recalculated based on data in the database. Might be okay for your use case to use the user's home. - 已知的文件系统路径:${user.home}/.myapp。应用程序有时会使用它,例如可以根据数据库中的数据重新计算的搜索索引。您的用例可以使用用户的home。
- Store the configurable filesystem path in a configuration repository such as the database or perhaps Java Preferences (if you don't like to use servlet init params). Commercial applications such as Atlassian JIRA use a configurable (but absolute) filesystem path where they store issue attachments. If they don't know a better way, i don't know who does :)
- 将可配置的文件系统路径存储在配置存储库中,如数据库或Java首选项(如果不喜欢使用servlet init params)。商业应用程序,如Atlassian JIRA,使用一个可配置(但绝对)的文件系统路径来存储问题附件。如果他们不知道更好的方法,我不知道谁知道。
#5
0
I generally would suggest to use a database to store persistent data and expose it via a DataSource.
我通常建议使用数据库来存储持久数据并通过数据源公开它。
If you don't want to do that, I guess you could consider using the "user.home" system property (I have seen this used in a few circumstances). But... there are no guarantees that your servlet will be run with permission to write access unless you configure that yourself.
如果您不想这样做,我想你可以考虑使用“用户”。home“系统属性(我在一些情况下看到过这种用法)。但是…没有任何保证,您的servlet将被允许写入访问权限,除非您自己配置它。
#1
9
Our team does this a lot. A general rule we follow is outside the web app and outside Tomcat.
我们的团队经常这样做。我们遵循的一般规则是在web应用程序之外,在Tomcat之外。
Our sysadmin set up a directory on our server that the tomcat user has rw permissions to (e.g. /var/tomcat/persist
). We have a built a directory structure under this that tomcat uses to store files, read app-specific init files, etc.
我们的sysadmin在服务器上设置了一个目录,tomcat用户具有rw权限(例如/var/tomcat/persist)。我们在此基础上构建了一个目录结构,tomcat使用该结构来存储文件、读取特定于应用程序的init文件等。
If you don't want to use an absolute path in your init-params for your servlet, consider setting a system property when tomcat is started up. The good thing about that is every application running under tomcat will have access to it. The bad thing about that is every application running under tomcat will have access to it. You could set a property named base.persist.dir
and build subdirectories for each application underneath it. We set system properties in the setenv.sh script in the bin/ directory under the CATALINA_OPTS environment variable.
如果您不想在您的init-params中为您的servlet使用绝对路径,请考虑在tomcat启动时设置系统属性。这样做的好处是,在tomcat下运行的每个应用程序都可以访问它。最糟糕的是,在tomcat下运行的每个应用程序都可以访问它。您可以设置一个名为base.persist的属性。然后为它下面的每个应用程序创建子目录。我们在setenv中设置系统属性。CATALINA_OPTS环境变量下的bin/目录中的sh脚本。
#2
5
Answering the title of the question, what about using a database, a DataSource
and JDNI? Even in a web only context, writing to files using java.io
is not really recommended because of concurrency, threading, security, clustering, portability issues. Some of these problems can be "workarounded" but still, this is not really a best practice. The standard approach is to use a database and I'd suggest to reconsider this option, throwing "file-based" lightweight database like HSQLBD or JavaDB into the mix.
回答问题的标题,使用数据库、数据源和JDNI怎么样?即使是在仅在web上下文中,也要使用java编写文件。由于并发性、线程化、安全性、集群性和可移植性问题,不建议使用io。有些问题可能会被“解决”,但这并不是最佳实践。标准的方法是使用数据库,我建议重新考虑这个选项,将“基于文件的”轻量级数据库(如HSQLBD或JavaDB)加入其中。
(EDIT: For an unknown reason, database is not an option. Using JNDI or context parameters or init parameters to pass an absolute path - which are the less worse options IMHO - is excluded too. For a relative path, maybe look at user.home
or user.dir
then - or any other system property that you could pass on the command line. I don't like it, I wouldn't do it, and this doesn't solve the issues previously mentioned, but it's your choice after all.)
(编辑:出于未知的原因,数据库不是一个选项。使用JNDI或上下文参数或init参数传递绝对路径(IMHO是较差的选项)也被排除。对于相对路径,可以考虑用户。家里或用户。然后是dir——或您可以在命令行上传递的任何其他系统属性。我不喜欢它,我不会做它,这并不能解决前面提到的问题,但这毕竟是你的选择。
#3
4
Storing the files in a webapp directory under the home directory of the user running Tomcat is a good and convenient option. It is outside of Tomcat, which means it will survive redeployment, and it is usually a writable directory (because it is created under the users' home dir). But it is always a good idea to allow overriding the location of such directory via system property.
将文件存储在运行Tomcat的用户的主目录下的webapp目录中是一个很好的选择。它位于Tomcat之外,这意味着它可以在重新部署后继续存在,而且它通常是一个可写目录(因为它是在用户的home dir下创建的)。但是,允许通过系统属性覆盖该目录的位置总是一个好主意。
#4
1
Generally, this would go to the database. But since the OP insists on not using a database, I'd try a different approach:
通常,这将进入数据库。但由于OP坚持不使用数据库,我将尝试另一种方法:
- Filesystem path which is known:
${user.home}/.myapp
. Applications sometimes use this for e.g. search indices which can be recalculated based on data in the database. Might be okay for your use case to use the user's home. - 已知的文件系统路径:${user.home}/.myapp。应用程序有时会使用它,例如可以根据数据库中的数据重新计算的搜索索引。您的用例可以使用用户的home。
- Store the configurable filesystem path in a configuration repository such as the database or perhaps Java Preferences (if you don't like to use servlet init params). Commercial applications such as Atlassian JIRA use a configurable (but absolute) filesystem path where they store issue attachments. If they don't know a better way, i don't know who does :)
- 将可配置的文件系统路径存储在配置存储库中,如数据库或Java首选项(如果不喜欢使用servlet init params)。商业应用程序,如Atlassian JIRA,使用一个可配置(但绝对)的文件系统路径来存储问题附件。如果他们不知道更好的方法,我不知道谁知道。
#5
0
I generally would suggest to use a database to store persistent data and expose it via a DataSource.
我通常建议使用数据库来存储持久数据并通过数据源公开它。
If you don't want to do that, I guess you could consider using the "user.home" system property (I have seen this used in a few circumstances). But... there are no guarantees that your servlet will be run with permission to write access unless you configure that yourself.
如果您不想这样做,我想你可以考虑使用“用户”。home“系统属性(我在一些情况下看到过这种用法)。但是…没有任何保证,您的servlet将被允许写入访问权限,除非您自己配置它。