[英]How do I upload a file from a Java servlet to a location on a web server within Tomcat/webapps
How do I upload a file from a Java servlet to a location on a web server within Tomcat/webapps. 如何将文件从Java servlet上传到Tomcat / webapps中Web服务器上的某个位置。
I am using Commons upload. 我正在使用Commons上传。 I have a location such as myserver:8080/myapp/mylocation
where I want to put the files that are uploaded. 我有一个位置,例如myserver:8080/myapp/mylocation
,我要在其中放置上载的文件。
I tried using getServletContext().getRealPath("/");
我尝试使用getServletContext().getRealPath("/");
to find where I am and then appended that with mylocation but I get a nullpointer exception. 查找我的位置,然后将其附加到mylocation上,但出现空指针异常。
I know I sound vague, it's because I am confused about the big picture, what are the generals steps I need to perform to make this work. 我知道我听起来有些含糊,这是因为我对总体情况感到困惑,为完成这项工作我需要执行哪些一般步骤。 Any code or links to code would be much appreciated. 任何代码或代码链接将不胜感激。
You may need to check permissions. 您可能需要检查权限。 Make sure that you have write permissions on the directory in which you plan to store the file. 确保对计划存储文件的目录具有写权限。 Generally you may want to store the file in WEB-INF/mylocation. 通常,您可能希望将文件存储在WEB-INF / mylocation中。 Then getServletContext().getRealPath("/WEB-INF/mylocation") becomes the location of where you are going to save the file. 然后,getServletContext()。getRealPath(“ / WEB-INF / mylocation”)成为您要保存文件的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.