使用 Java 和 JSP 将文件上传到服务器上的目录 - 无法获得正确的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4726847/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:44:46  来源:igfitidea点击:

Upload file to directory on server using Java and JSP - can't get path right

javajspservlets

提问by katura

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

在我的 vps 上,我想将文件上传到 Logos 目录。我的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.

当一个文件通过我的 jsp 页面上传时,该文件被重命名,然后我想把它放到 Logos 目录中......但我似乎无法在我的 servlet 代码中找到正确的路径。

Snippet of servlet code -

servlet 代码片段 -

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?

编辑
使用 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 

采纳答案by Bozho

It's a common practice to make the path for storage configurable - either via some application.propertiesfile, or if you don't have such a properties file - as a context-paramin web.xml. There you configure the path to be the absolute path, like:

通常的做法是使存储路径可配置 - 通过某个application.properties文件,或者如果您没有这样的属性文件 - 作为context-paramin web.xml. 在那里您将路径配置为绝对路径,例如:

configuredUploadDir=/home/webadmin/domain.com/html/Logos

Obtain that value in your code (depending on how you stored it), and have:

在您的代码中获取该值(取决于您的存储方式),并具有:

File uploadDir = new File(configuredUploadDir);

Note: make sure you have the permissions to read and write the target directory.

注意:确保您具有读写目标目录的权限。

回答by RAQ

You can use following code in any jsp or servlet.

您可以在任何 jsp 或 servlet 中使用以下代码。

1) String serverPath= getServletContext().getRealPath("/");

1) String serverPath= getServletContext().getRealPath("/");

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.

这将为您提供从根目录到 Web 应用程序目录的服务器的完整路径。
对我来说,当我从 myapp 应用程序 sysout 时,它是“D:\local\tomcat-6.0.29\webapps\myapp”

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\filenameto the serverPath which we got earlier.
This will work in all situation even you have multiple servers installed on the same system.

如上所述获得服务器系统的整个真实路径后,您可以获得相对于您的目录的路径。所以,如果我有一些数据文件的myapp \数据-我可以将其追加\ DATA \文件名,这是我们早先得到了SERVERPATH。
即使您在同一系统上安装了多个服务器,这也适用于所有情况。

2) You can get server home from system propertiesusing

2)您可以使用从系统属性获取服务器主页

System.getProperty("TOMCAT_HOME")

System.getProperty("TOMCAT_HOME")

and then can use this absolute pathin your program

然后可以在你的程序中使用这个绝对路径

3) To pass absolute directory path to any servlet using <init-param>

3)使用绝对目录路径传递给任何servlet <init-param>

Hope this will work for you.

希望这对你有用。

回答by Peter

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.

那么问题来了:File构造函数不创建文件,只为创建做准备,那么,在你构造一个文件实例之后,你必须调用方法createNewFile(),仅此而已。

回答by Eric Giguere

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.

路径“/Logos/”将尝试在您系统的根目录中创建文件,这不是您想要的。查看 ServletContext.getRealPath() 方法。