aspose file tools*
The moose likes Struts and the fly likes File name when using Struts 2 file upload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File name when using Struts 2 file upload" Watch "File name when using Struts 2 file upload" New topic
Author

File name when using Struts 2 file upload

Dima Riabin
Greenhorn

Joined: Jan 05, 2011
Posts: 16

Hi, I upload the file using Struts 2, then the content goes to the database. In teh 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 teh 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 the process to get parts?
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

public com.examples.UploadAction implemements Action {
private File file;
private String contentType;
private String filename;

public void setUpload(File file) {
this.file = file;
}

public void setUploadContentType(String contentType) {
this.contentType = contentType;
}

public void setUploadFileName(String filename) {
this.filename = filename;
}

...
}

Just follow like you will get the filename.


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Dima Riabin
Greenhorn

Joined: Jan 05, 2011
Posts: 16

Thanks for your reply. I have tried this solution, but .. who and where is it called?
My jsp has input field like this:

<s:file name="userFile" label="File "/>

and then in the action, I have

private File userFile;
private String contentType;
private String filename;
private String statusString ;

I have the getters and setters for the fields:
Now note: The userFile is the file that is ON THE SERVER, and it doesn't have name the same us the one on the client.
When I choose the file from fileChooser in the browser, I select file named "abc.txt", then struts UPLOAD THE FILE TO THE SERVER, and after that my action is taking place. So when 'execute()' method is entered 'userFile' is the file that already sits on teh server, and it's name is DIFFERENT from what the original file name was.
I was interested in how to have access to the CLIENT side file name.

I know what Struts Interceptor is doing:
Interceptor wrap HttpServletRequest into MultiPartWrapper, and then that MultiPart wrapper has a method 'getNames' that returns an array of file names. This really comes from the request object, but .. in the struts action, I have access to the request object - that doesn't have the method to give me fileNames - I will have to repeat what is already done in the interseptor. I was wandering if I can avoid this, if the client file name can be available to me some other way
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

It's fileUploadInterceptor giving a temporary file object not the actual file, you have to file copy that object wherever you want use Apache file utils FileUtils.copy(sourcefile, destFile) to copy the file. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File name when using Struts 2 file upload