Dima R.
Dima R.

Reputation: 997

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?

Upvotes: 6

Views: 5054

Answers (4)

WesternGun
WesternGun

Reputation: 12728

First we know two basic facts:

  • When form is submitted, server side process fields according to their name attributes in tags. So a field with no name cannot be submitted.
  • Based on that, Struts 2 automatically does pairing job like this: if a form is submitted, Struts 2 iterate all fields with name; once a field of type file is found, get its name and searches for getters/setters in action class with this pattern:

    • if name is xxx, search for getters/setters of xxx, xxxContentType, xxxFileName.
    • if they are found, store the file content into xxx, the content type of this file in xxxContentType, and the file name in xxxFileName.

    • the same applies to multiples files, and these three variables must be of type Array/Collection.

So, when we define action variables, we must follow these rules, to get the file name(s) you need in action class. If your file input field is named myFile, in your action class you must have:

File myFile;
String myFileContentType;
String myFileFileName;

For multiple uploading:

File[] myFile;
String[] myFileContentType;
String[] myFileFileName;

Files names are stored without path(I am using FF so that is my case. Maybe in other navigators they are stored in full names). To save them in server side, you must assign the names in xxxFileName to each file in xxx. So you are half way there. They are stored in the same order so you can iterate two arrays with same index. Like this:

for(int i=0; i < xxx.length(); i++) {
    saveFile(xxx[i], xxxFileName[i]); 
    //saveFile() is a method you implement to save files. FileWriter, etc.
}

Upvotes: 1

nmc
nmc

Reputation: 8686

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;
}

Upvotes: 8

Dima R.
Dima R.

Reputation: 997

Thanks, everyone. But all that you say is WHAT i WAS TALKING ABOUT. At the end, you have a name of a file as it seen on server machine. Imagine a scenario: User opens a browser and navigates to the page of my application. User is on the computer "A". The server is running on computer "B". When user hits "Browse" button, he picks a file "MyFile.jpg" - file that is sitting on his computer "A". Struts do the work, and upload the file by means of framework onto the temp directory on computer "B". Look at the picture in the examle. The name of a file displayed above the image of Taj Mahal is ff_000_stuf.tmp - this is a temporary file on the server machine. Using FileUtils.copyFile will copy file FROM THAT TEMP DIRECTORY ON THE SERVER to anywhere specified. My Question was specifically about HOW TO ACCESS THE NAME OF THE FILE AS IT WAS ON TEH COMPUTER "A" - "MyFile.jpg". There is something about setFileName() and segFile() method, that I tried to place into the action class, but I do not know or do not understand, how those methods to be called. From the examples, I read the following: "The fields userImageContentType and userImageFileName are optional. If setter method of these fields are provided, struts2 will set the data.". I tried that, and maybe "struts" are calling those, but I still see the file name with all that garbage of "fff_000.tmp" in the name.
I solved my problem, but hacking into the code for FileUpload interceptor, and copying part of it into my Action class. This did the trick, but I do not like this since it defies the purpose of the framework

Upvotes: -2

Denees
Denees

Reputation: 9198

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.

Upvotes: 9

Related Questions