在struts.xml中我如下配置
为什么项目将我上传的临时文保存到D:/temp,为何不是项目的WebAppRoot+/temp 下?
那我本地测试为xp,服务器linux,那我如何来操作?
[b]
原因解释:[/b]
[b][color=blue]
当你配置"struts.multipart.saveDir"时,struts会将目录定向为:[/color][/b]
[code="java"]
if (saveDir != null) {
fac.setRepository(new File(saveDir));
}[/code]
[b][color=blue]当"struts.multipart.saveDir"为“/temp”时,很容易验证 :
new File("/temp").getAbsolutePath();
为当前的根目录下的temp目录。
当你使用相对路径比如 "myproject/temp"配置时,效果依然不行。因为
new File(相对路径) 得到的结果是:
user.dir + 相对路径
在Tomcat下user.dir是 Tomcat目录下的bin目录,所以上述路径最终是:
Tomcat/bin/myproject/temp
( 不要试图使用 ../webapps/myproject/temp , 这样是不成功的)。[/color][/b]
[b]
解决办法2个,个人爱好自选:[/b]
[b][color=blue]1、更改user.dir的默认值。
在系统启动的时候,用ServletContextListener修改系统属性。
System.setProperty("user.dir","你的Tomcat的目录下的webapps");
然后在配置:[/color][/b]
[code="xml"] [/code]
[color=blue]
[b]
2、直接了当:[/b][/color]
[code="xml"] [/code]
[quote]为什么项目将我上传的临时文保存到D:/temp[/quote]
因为你用的是/temp 指定的,所以在当前的 盘符下的/temp目录(对XP而言)。
[quote]那我本地测试为xp,服务器linux,那我如何来操作? [/quote]
指定绝对路径,试试
/temp 在linux服务器上的一个放临时文件的目录,即根目录下的temp目录。
在win xp下
这样使用目录:
[code="xml"] [/code]
用你WebAppRoot+/temp的绝对路径填写进去。
[quote]“指定文件上传时的临时工作目录.如果没有设置,将才用Servlet容器为web应用分配的临时工作目录”。如果不指定tempDir,这个目录就是%TOMCAT_HOME%\temp;如果指定的话,就要指定绝对路径,如“C:\temp”。[/quote]
//////////////////////////////
一般都是指定绝对路径.
可以试试
[code="xml"] [/code]
看看官网给出的struts.properties文件([url=http://struts.apache.org/2.0.11/docs/strutsproperties.html]http://struts.apache.org/2.0.11/docs/strutsproperties.html[/url])
[code="java"]# uses javax.servlet.context.tempdir by default
struts.multipart.saveDir=[/code]
解析下,
接下来,看看javax.servlet.context.tempdir
看看struts2中怎么得到temp文件目录的
The directory used is determined from the first of the following to be non-empty.(当前面一个为空,取后面一个)
[list]
[*] A temp dir explicitly defined either using the tempDir servlet init param, or the tempDir attribute of the element in the Struts config file.(配置文件中)
[*] The container-specified temp dir, obtained from the javax.servlet.context.tempdir servlet context attribute.
[*] The temp dir specified by the java.io.tmpdir system property.
再去看看javax.servlet.context.tempdir
[quote]
servlet api 2.2 final release:
4.7 Temporary Working Directories
It is often useful for Application Developers to have a temporary working area on the local
filesystem. All servlet containers must provide a private temporary directory per servlet context and
make it available via the context attribute of javax.servlet.context.tempdir. The
object associated with the attribute must be of type java.io.File.
[/quote]
通过如下方法获得。
[code="java"]<%= application.getAttribute("javax.servlet.context.tempdir").toString() %> [/code]
或者
根据servlet的servletContext获得。