I upload the file using Struts 2, then the content goes to the database. In the database I store file content, content type AND FILE NAME AS IT IS ON THE CLIENT MACHINE (whatever the client sees in the 'Browse' when choosing the file to upload - for example 'myFIle.txt' ). However, the problem is, when I store the file to the database, it takes place on the server, and at that time I only see the file name as it is AFTER BEING UPLOADED TO THE TEMP DIRECTORY ON SERVER. (something like 373_393jfu_39.tmp )
How can we access the name of the file that was actually on client? I know that for security reasons, struts somehow hide the uploading process... but is there way to hack in between?
http://java.dzone.com/articles/struts2-tutorial-part-67 Here he explains how to make a very clear and detailed upload, I think is the best way for you.
See example at http://struts.apache.org/2.0.14/docs/file-upload.html
You can get the file name as it is on the client machine if you provide a setXFileName(String fileName)
setter.
To keep the file with the original file name instead of the temporary file name as it would receive when uploaded to the server, you can do something like:
String targetXFilename = PATH_TO_DIRECTORY + "/" + getXFileName();
File targetXFile = new File(targetXFilename);
try {
FileUtils.copyFile(getXFile(), targetXFile);
} catch(IOException e) {
return Consts.RET_ERROR_EXCEPTION_IO;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With