如何在 File 类中提供相对路径来上传任何文件?

发布于 2024-11-07 23:10:36 字数 457 浏览 0 评论 0原文

我正在上传一个文件,我想为其提供相对路径,因为该程序应该在 Linux 和 Windows 环境中运行。

这就是我用来上传的

realPath = getServletContext().getRealPath(/files);
destinationDir = new File(realPath);
if(!item.isFormField())
                {
                    File file = new File(destinationDir,item.getName());

                    item.write(file);
}

任何其他方式直接在此处提供相对路径

File file = new File(destinationDir,item.getName());

I am uploading a file, for which I want to provide a relative path, because the program should work in both linux and windows env.

This is what I am using to upload

realPath = getServletContext().getRealPath(/files);
destinationDir = new File(realPath);
if(!item.isFormField())
                {
                    File file = new File(destinationDir,item.getName());

                    item.write(file);
}

Any other means to directly provide relative path here

File file = new File(destinationDir,item.getName());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-11-14 23:10:36

我想提供相对路径

切勿在网络应用程序中这样做。工作目录无法从代码内部控制。


因为该程序应该在 Linux 和 Windows 环境中运行。

只需使用“/path/to/uploaded/files”。它在两种环境中都同样有效。在 Windows 中,它只能与服务器运行所在的磁盘相同。

File file = new File("/path/to/uploaded/files", filename);
// ...

这是我用来上传的内容

 realPath = getServletContext().getRealPath(/files);
 目的地目录 = 新文件(真实路径);

您不应将文件存储在网络内容中。当 WAR 未扩展时,此操作将会失败,即使扩展了,每当您重新部署 WAR 时,所有文件都会丢失。将它们存储在 WAR 之外的绝对位置,例如之前建议的 /path/to/uploaded/files

另请参阅:

I want to provide a relative path

Never do that in a webapp. The working directory is not controllable from inside the code.


because the program should work in both linux and windows env.

Just use "/path/to/uploaded/files". It works equally in both environments. In Windows it will only be the same disk as where the server runs.

File file = new File("/path/to/uploaded/files", filename);
// ...

This is what i am using to upload

 realPath = getServletContext().getRealPath(/files);
 destinationDir = new File(realPath);

You should not store the files in the webcontent. This will fail when the WAR is not expanded and even when it is, all files will get lost whenever you redeploy the WAR. Store them outside the WAR in an absolute location, e.g. /path/to/uploaded/files as suggested before.

See also:

涫野音 2024-11-14 23:10:36

正如 BalusC 指出的,您不希望将文件保存到使用 getRealPath 找到的目的地。当您重新部署时,此类文件不仅会被清除,而且任何可以访问您网站的用户都可以下载它们,这可能会是一个严重的安全问题,具体取决于您的行为。

当我遇到这个问题时,我通常会创建一个属性文件,并将保存文件的目录放入属性文件中。然后我可以为 Linux 和 Windows,或者为服务器 A 和服务器 B 设置不同的路径。您只需发明一个路径并对其进行硬编码,这可能会起作用,但我经常发现我需要在不同的服务器上使用不同的路径。例如,在我当前的项目中,我们在同一个物理机器上运行三个 Tomcat 实例,用于不同的测试阶段。我们不希望它们写入的文件都进入同一个目录:每个文件都应该有自己的目录。

As BalusC points out, you do not want to save files to a destination found with getRealPath. Not only will such files be wiped out when you redeploy, but they will also be available for download by any user who can reach your web site, which, depending on what you're up to, may be a serious security problem.

When I'm faced with this problem, I normally create a properties file, and put the directory in which to save the files in the properties file. Then I can have a different path for Linux and Windows, or for server A and server B. It may work for you to just invent a path and hardcode it, but I often find that I need different paths on different servers. Like, in my present project we have three instances of Tomcat running on the same physical box for different stages of testing. We don't want files they write to all go to the same directory: each should have its own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文