问题描述
我有一个上传表单的Web应用程序,用户可以上传文件。
我想将这些文件存储在一个文件夹文件中。我想这个文件夹直接放在webapp-root下。
使用struts2,在我的uploadFileAction中,我可以很容易地使用设置这个路径String uploadDir = ServletActionContext.getServletContext()。getRealPath(/ files )+/;
但是,当我试图检索文件,显然我试图访问该文件的bean不有权访问 getServletContext()
,从而在 getRealPath
$ b $上引发一个空指针现在我试图找出我应该如何处理这个问题。我听说过春天的资源是要走的路,但我找不到任何相关的例子。
有没有简单的方法来获得我的路径?我只想要rootpath到我的webapp。没有什么比这更少的了...
这是不行的。所有这些文件都会在重新部署Web应用程序或者重新启动服务器时丢失。这些文件不包含在原始WAR中。
您需要将它们存储在webapp上下文以外的固定路径中。例如。 的/ var / web应用/上传
。您可以在属性文件或系统属性中配置此路径。
I have a web application with an upload form where users can upload files.
I'd like to store the files a folder 'files'. And i'd like this folder to be placed directly under the webapp-root.
Using struts2, in my uploadFileAction, i can easily set this path with String uploadDir = ServletActionContext.getServletContext().getRealPath("/files")+ "/";
But when i'm trying to retrieve the files, apparently my bean which tries to access the file does not have access to getServletContext()
and thus throws a nullpointer on getRealPath
Now im trying to figure out how i should approach this. I've heard about spring resource is the way to go, but i cant find any relevant examples.
Is there an easy way to get my paths? I just want the rootpath to my webapp. Nothing more nothing less...
This isn't going to work. All those files will get lost whenever you redeploy the webapp or even when you restart the server. Those files are namely not contained as part of the original WAR.
You need to store them in a fixed path outside the webapp's context. E.g. /var/webapp/uploads
. You can configure this path in a properties file or as a system property.
这篇关于Java EE - 获取上传文件的真实路径的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!