您的位置:程序门 -> java -> web 开发



在javabean中如何返回文件的真实路径?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


在javabean中如何返回文件的真实路径?
发表于:2007-09-03 14:49:56 楼主
请问在javabean中如何返回一个文件在文件系统中的真实路径?如/web-inf/web.xml返回d:\myweb\web-inf\web.xml
就像在jsp页面中使用request.getrealpath( "/web-inf/web.xml ")时一样。。。
谢谢!
发表于:2007-09-03 14:57:251楼 得分:0
public   static   string   getpath(){
    properties   property   =   system.getproperties();
    string   strpath=property.getproperty( "user.dir ");
    strpath=strpath+ "\\index\\ ";
    return   strpath;
}
发表于:2007-09-03 15:23:382楼 得分:0
property.getproperty( "user.dir ");返回了我的tomcat安装目录d:\program   files\apache   software   foundation\tomcat   5.5
但我要的是在我网站目录下任何一个文件的文件系统真实路径,如:d:\myweb\web-inf\web.xml,就跟在资源管理器的地址栏里看到的一样

请再指教,thanks!
发表于:2007-09-03 16:36:313楼 得分:0
string   requestpath   =   request.getservletpath()   +   ((request.getpathinfo()   !=   null)   ?
                                request.getpathinfo()   :   " ");
string   realpath   =   getservletconfig().getservletcontext().getrealpath(requestpath);


----------------------------------------
copy   from   apache   axisservlet.java

maybe   useful   for   you.
good   luck
发表于:2007-09-03 16:41:534楼 得分:0
mark
发表于:2007-09-03 17:02:065楼 得分:0
string   requestpath   =   request.getservletpath()   +   ((request.getpathinfo()   !=   null)   ?
                                request.getpathinfo()   :   " ");
string   realpath   =   getservletconfig().getservletcontext().getrealpath(requestpath);

试了一下,编译时报错:cannot   find   symbol   :   variable   request
是不是还有别的地方要注意?
发表于:2007-09-03 17:08:246楼 得分:0
you   can   pass   httprequest   as   a   param   into   the   java   bean.
发表于:2007-09-03 18:04:347楼 得分:0
thread.currentthread().getcontextclassloader().getresource( " ").getpath()

这样应该可以结帖了~
发表于:2007-09-03 19:45:148楼 得分:0
在bean里定义静态一个string
在jsp里set一下啊
先在jsp里取得路径,然后set进去,在里边直接用就可以了
发表于:2007-09-03 20:34:259楼 得分:0
to   wuzeng2002(wind)
你的方法还不能结贴啊,

因为你的路径是找到了${web-inf}/classes目录下了,所以,应该向上一层。

并且需要注意的是,用户可能没有定义这个classes目录,


快速检索

最新资讯
热门点击