使用 Java 和 JSP 将文件上传到服务器上的目录 - 无法获得正确的路径
在我的 vps 上,我想将文件上传到 Logos 目录。 我的vps上的目录结构如下 -
/home/webadmin/domain.com/html/Logos
当通过我的jsp页面上传文件时,该文件被重命名,然后我想将其放入Logos目录中....但我似乎无法得到我的 servlet 代码中的路径。
servlet 代码片段 -
String upload_directory="/Logos/"; // path to the upload folder
File savedFile = new File(upload_directory,BusinessName+"_Logo."+fileExtension);
//.....
//file saved to directory
//.....
我尝试了很多变体,但仍然失败。指定路径的正确方法是什么?
已编辑
使用 getServletContext() 的问题是它返回 Tomcat 和我的 webapp 所在目录的路径...而我想到达我的 html 和图像文件所在的目录 - 在 vps 的根目录下。我如何指定该路径?
String server_path = getServletContext().getRealPath("/"); // get server path.
//server_path = /opt/tomcat6/webapps/domain.com/
String upload_directory = "Logos/"; // get path to the upload folder.
String complete_path = server_path + upload_directory; // get the complete path to the upload folder.
//complete_path = /opt/tomcat6/webapps/domain.com/Logos/
File savedFile = new File(complete_path,"NewLogo.jpg");
//savedFile = /opt/tomcat6/webapps/domain.com/Logos/NewLogo.jpg
On my vps, I want to upload a file to the Logos directory.
The directory structure is as follows on my vps -
/home/webadmin/domain.com/html/Logos
When a file is uploaded through my jsp page, that file is renamed, and then I want to put it into the Logos directory.... but I can't seem to get the path right in my servlet code.
Snippet of servlet code -
String upload_directory="/Logos/"; // path to the upload folder
File savedFile = new File(upload_directory,BusinessName+"_Logo."+fileExtension);
//.....
//file saved to directory
//.....
I've tried many variations, but still fail. What is the proper way to specify the path?
Edited
The problem with using getServletContext() is that it returns the path to the directory where Tomcat and my webapp is...whereas I want to reach the directory where my html and image files are - under the root directory of the vps. How do I specify that path?
String server_path = getServletContext().getRealPath("/"); // get server path.
//server_path = /opt/tomcat6/webapps/domain.com/
String upload_directory = "Logos/"; // get path to the upload folder.
String complete_path = server_path + upload_directory; // get the complete path to the upload folder.
//complete_path = /opt/tomcat6/webapps/domain.com/Logos/
File savedFile = new File(complete_path,"NewLogo.jpg");
//savedFile = /opt/tomcat6/webapps/domain.com/Logos/NewLogo.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使存储路径可配置是一种常见的做法 - 通过某些
application.properties
文件,或者如果您没有这样的属性文件 - 作为context-param
> 在web.xml
中。在那里,您将路径配置为绝对路径,例如:在代码中获取该值(取决于您存储它的方式),并具有:
注意:确保您具有读取和写入的权限目标目录。
It's a common practice to make the path for storage configurable - either via some
application.properties
file, or if you don't have such a properties file - as acontext-param
inweb.xml
. There you configure the path to be the absolute path, like:Obtain that value in your code (depending on how you stored it), and have:
Note: make sure you have the permissions to read and write the target directory.
您可以在任何 jsp 或 servlet 中使用以下代码。
这将为您提供从根目录到 Web 应用程序目录的服务器完整路径。
对我来说,当我从 myapp 应用程序中 sysout 时,它是:“D:\local\tomcat-6.0.29\webapps\myapp”。
一旦您获得了上述服务器系统的完整真实路径,您就可以获得相对于您的目录的路径。因此,如果我在 myapp\data 中有一些数据文件 - 我可以将其附加到 \data\filename 到我们之前获得的 serverPath。
即使您在同一系统上安装了多个服务器,这也适用于所有情况。
2) 您可以使用系统属性获取服务器主目录
然后可以在程序中使用这个绝对路径
3) 使用
希望 将绝对目录路径传递给任何servlet这对你有用。
You can use following code in any jsp or servlet.
This will give you full path of the server from root directory to your web application directory.
For me its: "D:\local\tomcat-6.0.29\webapps\myapp" when I sysout from myapp application.
Once you got the whole real path for the server system as above you can get the path relative to your directory. So if I have some data file in myapp\data - I can get it appending \data\filename to the serverPath which we got earlier.
This will work in all situation even you have multiple servers installed on the same system.
2) You can get server home from system properties using
and then can use this absolute path in your program
3) To pass absolute directory path to any servlet using
<init-param>
Hope this will work for you.
好吧,问题是:File 构造函数不会创建文件,只是准备创建,然后,在构造文件实例后,您必须调用方法 createNewFile(),仅此而已。
Well, the problem is: the File constructor doesn't create the file, only prepares to for the creation, then, after you construct a file instance you must invoke the method createNewFile(), and thats all.
路径“/Logos/”将尝试在系统的根目录中创建文件,这不是您想要的。查看 ServletContext.getRealPath() 方法。
The path "/Logos/" will attempt to create the file in the root of your system, which is not what you want. Look at the ServletContext.getRealPath() method.