Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 file Upload - accessing client file name

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?

like image 549
Dima R. Avatar asked Aug 02 '11 15:08

Dima R.


Video Answer


2 Answers

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.

like image 168
Denees Avatar answered Nov 15 '22 12:11

Denees


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;
}
like image 29
nmc Avatar answered Nov 15 '22 12:11

nmc