从 java 类文件获取 apache webcontents 文件夹的绝对路径
需要在动态 Web 应用程序内获取 java 类文件中的绝对路径...
实际上我需要获取 apache webapps 文件夹的路径...部署 webapps 的路径
eg /apache-root/webapps/my-deployed-app/WebContent/images/imagetosave.jpg
需要在 java 类文件中获取它,而不是在 jsp 页面或任何视图页面上。
有什么想法吗?
Need to get absolute path in java class file, inside a dynamic web application...
Actually i need to get path of apache webapps folder... where the webapps are deployed
e.g. /apache-root/webapps/my-deployed-app/WebContent/images/imagetosave.jpg
Need to get this in a java class file, not on jsp page or any view page...
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如许多其他答案所提到的,您可以只使用
ServletContext#getRealPath ()
将相对 Web 内容路径转换为绝对磁盘文件系统路径,以便您可以在File
或FileInputStream
中进一步使用它。ServletContext
位于继承的getServletContext()
方法:但是,文件名“imagetosave.jpg”表明您尝试通过
FileOutputStream
存储上传的图像。公共 webcontent 文件夹是存储上传图像的错误位置!每当重新部署 Web 应用程序甚至服务器通过清理重新启动时,它们都会丢失。原因很简单,上传的镜像根本不包含在要部署的WAR文件中。您绝对应该在 webapp 部署文件夹之外寻找另一个位置作为上传图像的更永久存储,以便它在多次部署/重新启动时保持完整。最好的方法是准备一个固定的本地磁盘文件系统文件夹,例如
/var/webapp/uploads
并将其作为某些配置设置提供。最后将图像存储在那里。另请参阅:
As mentioned by many other answers, you can just use
ServletContext#getRealPath()
to convert a relative web content path to an absolute disk file system path, so that you could use it further inFile
orFileInputStream
. TheServletContext
is in servlets available by the inheritedgetServletContext()
method:However, the filename "imagetosave.jpg" indicates that you're attempting to store an uploaded image by
FileOutputStream
. The public webcontent folder is the wrong place to store uploaded images! They will all get lost whenever the webapp get redeployed or even when the server get restarted with a cleanup. The simple reason is that the uploaded images are not contained in the to-be-deployed WAR file at all.You should definitely look for another location outside the webapp deploy folder as a more permanent storage of uploaded images, so that it will remain intact across multiple deployments/restarts. Best way is to prepare a fixed local disk file system folder such as
/var/webapp/uploads
and provide this as some configuration setting. Finally just store the image in there.See also:
如果您有 javax.servlet.ServletContext 您可以调用:
来获取图像存储位置的实际路径。
ServletContext 可以从 javax.servlet.http.HttpSession 访问。
但是,您可能想考虑使用:
或
If you have a javax.servlet.ServletContext you can call:
to get the actual path of where the image is stored.
ServletContext can be accessed from a javax.servlet.http.HttpSession.
However, you might want to look into using:
or
这应该根据类的文件位置返回您的绝对路径。
This should return your absolute path based on the class's file location.
方法-1:
//步骤1:导入java.net.InetAddress;
//step2 : 提供您的文件路径
//step3 : 将所有部分放在一起
Method - 2 :
//Step : 1-获取绝对 url
//Step : 2-然后将其与上下文路径进行子串
//step : 3-在Web文件夹后提供您的文件路径
我的建议
注意:发生这种情况是因为如果您在没有 IDE 的情况下进行编码,则它存在于 IDE 的类路径中,然后将其保存在 java 编译的类文件的位置或您可以访问的公共文件夹中。
玩得开心
Method -1 :
//step1 : import java.net.InetAddress;
//step2 : provide your file path
//step3 : grab all peices together
Method - 2 :
//Step : 1-get the absolute url
//Step : 2-then sub string it with the context path
//step : 3-provide your file path after web folder
MY SUGGESTION
NOTE : this happens because it is present in the class path of your IDE if you are coding without IDE then keep it in the place of your java compiled class file or in a common folder which you can access.
HAVE FUN
我能够在过滤器中获得对 ServletContext 的引用。我最喜欢这种方法的是它发生在 init() 方法中,该方法在第一次加载 Web 应用程序时被调用,这意味着只执行一次。
我通过 web.xml 将“templatepath”添加到filterConfig:
I was able to get a reference to the ServletContext in a Filter. What I like best about this approach is that it occurs in the init() method which is called upon the first load the web application meaning the execution only happens once.
I added the "templatepath" to the filterConfig via the web.xml:
您可以在控制器中获取路径:
You can get path in controller:
您可以编写一个 ServletContextListener:
并在 web.xml 中配置它
You could write a ServletContextListener:
and configure it in web.xml