天天看点

struts2文件下载,动态设置资源地址

转自:http://blog.csdn.net/ctrl_shift_del/article/details/6277340

 1

servletactioncontext.getservletcontext().getresourceasstream("/"+tempfile); 

这是java加载资源的方法,所谓资源,实际上是任何一个文件,但特别的

是,getresourceasstream这个方法不使用绝对路径,

而是使用相对于classpath环境变量的相对路径。

以,如果写:

getresourceasstream("/resource.xml"); 

则要保证classpath下有

resource.xml文件就能找到。

通常,开发环境下,src目录(也就是源代码所在的目录)是包含在classpath中的,

在tomcat下,classpath其一指向:web-inf/classes目录。

另:如需使用绝对路径则可使用方法:

下面看一个完整的项目相关代码(struts2应用):

action 中的java 部分代码:

struts.xml中部分代码:

jsp页面部分代码: