At the moment, after uploading a file, the tmp file that gets created gets stored in:
\Apache Software Foundation\Apache Tomcat
7.0.22\work\Catalina\localhost\Project\upload__78a0adab_1380z353bfa__7gfe_00000000.tmp
How do I store it in my own custom directory? I’ve tried:
<param name="saveDir">/tmp</param>
But it still goes to the one I mentioned above.
EDIT 1 – struts.xml:
<action name="file_save" method="fileSave" class="FileActionBean">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="saveDir">/tmp</param>
<param name="allowedTypes">
image/bmp,image/gif,image/jpeg,image/jpg,image/png
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result type="redirect">file</result>
</action>
EDIT 2:
I am using struts 2 version 2.1.8.1
Are you using Spring?
In that case you can configure your ActionBean with your desired upload-directory.
Just add to your action-bean:
And modify your spring config
applicationContext.xmlto set the uploadDir. You can to it by setting value direct, or more preferably, by configfile as shown below:Update:
It is pretty easy to control where you want the file to be saved.
If the first character of your path string is not /, it will be relative to your “current” directory.
So you dont really need any “root” path if you want to save it in the webapp directory. Just plain simply:
File file = new File("results.txt");Or if you want to store it somewhere else than your current directory you can either navigate using
../../somethingor by just typing out the whole structure to where you want like:File file = new File("/home/something/something/myFile.txt");Or in an windows environment you could try
File file = new File("Z:\\results\\results.txt");
But it is not really a good idea to put your uploaded files in the same directory as the webapp. Then you will lose all your files every time you redeploy.