java Servlet - 将文件保存到磁盘,但不确定使用什么路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5342943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet - save file to disk, but not sure what path to use?
提问by user291701
I have a servlet which accepts a file upload, I'd like to store the file in a specific directory on disk. I'm using jetty. The file system looks like this:
我有一个接受文件上传的 servlet,我想将文件存储在磁盘上的特定目录中。我正在使用码头。文件系统如下所示:
/jetty
/bin
/contexts
/stuff
/webapps
foo.war // one web app
grok.war // another web app
start.jar
I start jetty from that start.jar file. In the context of the servlet, I'm not sure how to save the file to my desired destination, the "/stuff" folder:
我从那个 start.jar 文件启动 jetty。在 servlet 的上下文中,我不确定如何将文件保存到我想要的目的地,即“/stuff”文件夹:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
File downloaded = new File("some/path/here/stuff"); // ?
}
Yeah - I'm not sure what path to use - is there a way I can print the current working directory in java using the File class to figure that out maybe?
是的 - 我不确定要使用什么路径 - 有没有办法可以使用 File 类在 java 中打印当前工作目录来解决这个问题?
Thanks
谢谢
回答by BalusC
This is a pretty poor practice. There is no guarantee that it works on all servletcontainers other than Jetty. Even then, I'm not sure if it works on Jetty. At least, tt would make your webapp unportable to other containers.
这是一个非常糟糕的做法。不能保证它适用于除 Jetty 之外的所有 servlet 容器。即使那样,我也不确定它是否适用于 Jetty。至少, tt 会使您的 webapp 无法移植到其他容器。
When the WAR is expanded (it's servletcontainer specific ifand wherethe WAR will be expanded!), then the ServletContext#getRealPath()
can be used to convert a webcontent-relative path to an absolute disk file system path.
当 WAR 被扩展时(它是 servletcontainer 特定于 WAR是否以及在哪里扩展!),然后ServletContext#getRealPath()
可用于将 web 内容相对路径转换为绝对磁盘文件系统路径。
When the following line is called in a servlet inside foo.war
当下面这行在一个servlet里面被调用时 foo.war
String absoluteFooWebPath = getServletContext().getRealPath("/");
and Jetty has expanded the WAR file in the same webapps folder, then this will result in an absolute path to /jetty/webapps/foo
. Then, to get from there to /jetty/stuff
, you need to navigate two directories up and then from there navigate the stuff
directory.
并且 Jetty 已经在同一个 webapps 文件夹中扩展了 WAR 文件,那么这将导致/jetty/webapps/foo
. 然后,要从那里到/jetty/stuff
,您需要向上导航两个目录,然后从那里导航该stuff
目录。
String absoluteStuffPath = getServletContext().getRealPath("../../stuff");
After all, the most reliable way is to specify a servletcontainer-independent fixed path with read/write rights which you document properly in the installation manual of your webapp or to make it configureable by some VM argument / system property.
毕竟,最可靠的方法是指定一个具有读/写权限的 servletcontainer 独立的固定路径,您在 web 应用程序的安装手册中正确记录了该路径,或者使其可通过某些 VM 参数/系统属性进行配置。
回答by Piyush Mattoo
You can print the current dir, parent dir using following code:
您可以使用以下代码打印当前目录、父目录:
import java.io.File;
public class FileOperations {
public static void main (String args[]) {
File dir1 = new File (".");
File dir2 = new File ("..");
try {
System.out.println ("Current dir : " + dir1.getCanonicalPath());
System.out.println ("Parent dir : " + dir2.getCanonicalPath());
}
catch(Exception e) {
e.printStackTrace();
}
}
}
回答by Brent Worden
The startup.jar should set the jetty.home system property. Access it using System.getProperty("jetty.home")
.
startup.jar 应该设置 jetty.home系统属性。使用System.getProperty("jetty.home")
.
回答by grtjn
Though getRealPath
is likely to return a valid path when running your war file in Jetty, that is likely to be different when you'll switch to a different app server in future.
虽然getRealPath
在 Jetty 中运行您的 war 文件时可能会返回有效路径,但当您将来切换到不同的应用程序服务器时,这可能会有所不同。
It would be better to make it a configurable option in web.xml, using init-param
for instance, which would simply contain an absolute path to some pre-existing folder on the server. Putting it in web.xml allows changing it live when using for instance JBoss. You can use getInitParameter
on the ServletConfig
to get the value.
最好让它成为 web.xml 中的一个可配置选项,init-param
例如,使用它只包含服务器上某个预先存在的文件夹的绝对路径。将它放在 web.xml 中允许在使用例如 JBoss 时实时更改它。您可以使用getInitParameter
onServletConfig
来获取值。