如何将路径设置为“上下文路径”对于使用 Apache Common fileupload 上传的文件?

发布于 2024-09-02 14:55:00 字数 600 浏览 7 评论 0原文

我正在使用 Apache 通用文件上传库和 Netbeans 6.8 + Glassfish。我试图将当前上传路径更改为 servlet 的当前上下文路径,如下所示: WEB-INF/upload

所以我写道:

File uploadedFile = new File("WEB-INF/upload/"+fileName);
session.setAttribute("path",uploadedFile.getAbsolutePath());
item.write(uploadedFile);

但我注意到库将上传的文件保存到 glassfish 文件夹 中,这是当我打印上传的绝对路径时得到的结果文件:

C:\Program Files\sges-v3\glassfish\domains\domain1\WEB-INF\upload\xx.rar 

我的问题:

  • 如何强制通用文件上传将上传的文件保存在相对于当前servlet路径的路径中,这样我就不需要指定整个路径?这可能吗?

I'm using Apache common fileupload library with Netbeans 6.8 + Glassfish.I'm trying to change the current upload path to be in the current context path of the servlet , something like this:
WEB-INF/upload

so I wrote :

File uploadedFile = new File("WEB-INF/upload/"+fileName);
session.setAttribute("path",uploadedFile.getAbsolutePath());
item.write(uploadedFile);

but I noticed that the library saves the uploaded files into glassfish folder , here what I get when I print the absolute path of the uploaded file :

C:\Program Files\sges-v3\glassfish\domains\domain1\WEB-INF\upload\xx.rar 

My Question :

  • How can I force the common fileupload to save the uploaded file in a path relative to the current servlet path , so I don't need to specify the whole path ? is this possible ?

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

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

发布评论

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

评论(1

亣腦蒛氧 2024-09-09 14:55:00

java.io.File 作用于本地磁盘文件系统,并且对它所运行的上下文一无所知。当您传递 相对网络路径。它将相对于当前工作目录,这取决于您启动环境的方式。你不想依赖于此。

您可以使用 ServletContext#getRealPath() 将相对 Web 路径转换为绝对本地磁盘文件系统路径。

String relativeWebPath = "/WEB-INF/uploads";
String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
File uploadedFile = new File(absoluteFilePath, FilenameUtils.getName(item.getName()));
// ...

也就是说,我希望您知道部署文件夹不是上传的文件的正确位置,这些文件应该永久保存。当您重新部署 Web 应用程序时,所有内容都会丢失。另请参阅如何将文件写入应用程序的资源/图像文件夹?

The java.io.File acts on the local disk file system and knows absolutely nothing about the context it is running in. You should not expect it to find the "right" location when you pass a relative web path in. It would become relative to the current working directory which is dependent on how you started the environment. You don't want to be dependent on that.

You can use ServletContext#getRealPath() to convert a relative web path to an absolute local disk file system path.

String relativeWebPath = "/WEB-INF/uploads";
String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
File uploadedFile = new File(absoluteFilePath, FilenameUtils.getName(item.getName()));
// ...

That said, I hope that you're aware that the deploy folder isn't the right location for uploaded files which are supposed to be saved permanently. Everything will get lost when you redeploy the webapp. See also How to write a file to resource/images folder of the app?

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