初学者servlet问题:访问.war中的文件,哪个路径? [英] Beginner servlet question: accessing files in a .war, which path?

查看:103
本文介绍了初学者servlet问题:访问.war中的文件,哪个路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我正在使用的第三方库尝试访问文件时,即使我知道文件在战争.我已经尝试了打包(.war)和爆炸"(目录)部署,并且该文件肯定在那里.我也尝试过设置完全权限.它在Unix(Ubuntu)上.文件为war/dict/index.sense,错误为"dict/index.sense(无此类文件或目录)".

When a third-party library I'm using tries to access a file, I'm getting "Error opening ... file ... (No such file or directory)" even though I KNOW the file is in the WAR. I've tried both packaged (.war) and "exploded" (directory) deployment, and the file is definitely there. I've tried setting full permissions on it too. It's on Unix (Ubuntu). File is war/dict/index.sense and the error is "dict/index.sense (No such file or directory)".

当以Eclipse的GWT应用程序在托管模式下运行时,它在Windows计算机上运行良好,而当我将其传输到Unix机器上进行部署时,效果不佳.

It works fine on my Windows computer when running in hosted mode as a GWT app from Eclipse, just not when I transfer it to the Unix machine for deployment.

我的问题是:以前有人经历过和/或我应该考虑的相对路径是否存在差异,即战争中相对文件访问的根路径是什么?

My question is: has anybody experienced this before and/or are there differences in relative path that I should consider i.e. what's the root path for relative file access in a war?

推荐答案

我将这个答案归功于BalusC,但是由于我一开始并不真正理解它,所以我简化了它:

I owe this answer to BalusC, but since I didn't really understand it at first, I simplified it:

工作目录(相对路径的起点)取决于应用程序的启动方式,因此您实际上对此没有控制权.幸运的是,有一种方法可以获取Web应用程序根目录中任何内容的绝对路径,如下所示:

The working directory (the starting point for relative paths) depends on the way the application is started, so you don't really have control over this. Luckily there's a way to get the absolute path of anything in your web application's root directory, which is as follows:

getServletContext().getRealPath("pathToAnyFileInYourWebAppDocumentRoot.ext")

(给出一个字符串)

这就是您要做的.不幸的是,这仅在您的应用程序部署为目录而不是.war文件(它将返回null的位置)时才起作用.

That's all you have to do. Unfortunately this only works when your app is deployed as a directory, not as a .war file (where it will return null).

(另请参阅BalusC的答案以及该答案的来源).

(See BalusC's answer for another way, and where this answer came from).

这篇关于初学者servlet问题:访问.war中的文件,哪个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆