I'm trying to do upload a file using struts2 file tag. Followed all the mentioned as given in http://struts.apache.org/2.0.11/docs/file-upload-interceptor.html. but still am not able to retreive the file contents in my action class. Below is the code snippet which i added.
ApplicationResources.properties
struts.messages.error.uploading=a general error that occurs when the file could not be uploaded
struts.messages.error.file.too.large=occurs when the uploaded file is too large
struts.messages.error.content.type.not.allowed=occurs when the uploaded file does not match the expected content types specified
MyAction.java
import java.io.File;
//The below properties with getter and setter methods defined
private File subUploadFile; // The actual file
private String uploadContentType; // The content type of the file
private String uploadFileName; // The uploaded file name and path
public String uploadFile() throws Exception {
System.out.println("Inside uploadFile() .....");
System.out.println("File : " + getSubUploadFile());
System.out.println("File Name : " + getUploadFileName());
System.out.println("Content type : " + getUploadContentType());
return SUCCESS;
}
@Leonardo: Start new threads for new topics. There are probably other ways we *could* upload files in S2, but what's wrong with the built-in method using interceptors?
Why are you redefining interceptors? Just use the defaultStack.
In your action you define the "fileUpload" interceptor-ref--but when you define interceptors on a per-action basis you're defining *all* the interceptors the action will use. Your action, then, uses *only* the "fileUpload" interceptor, which is almost certainly not what you intended.
The defaultStack already includes servletConfig and fileUpload: remove *all* interceptor configuration from your configuration file.
Note also that the fileUpload interceptor deletes the file once the action has completed execution--you may need to copy the file elsewhere if you want to preserve it.
Sakthi Priya
Greenhorn
Joined: Apr 02, 2009
Posts: 26
posted
0
Hi David,
As suggested removed the complete <interceptors> tag. Should i remove the <interceptor-ref> tag defined within action tag?
If servletConfig and fileUplaod already there in defaultstack, should i refer this in my struts.xml anywhere?
Thanks
David Newton wrote:@Leonardo: Start new threads for new topics. There are probably other ways we *could* upload files in S2, but what's wrong with the built-in method using interceptors?
Why are you redefining interceptors? Just use the defaultStack.
In your action you define the "fileUpload" interceptor-ref--but when you define interceptors on a per-action basis you're defining *all* the interceptors the action will use. Your action, then, uses *only* the "fileUpload" interceptor, which is almost certainly not what you intended.
The defaultStack already includes servletConfig and fileUpload: remove *all* interceptor configuration from your configuration file.
Note also that the fileUpload interceptor deletes the file once the action has completed execution--you may need to copy the file elsewhere if you want to preserve it.
Sakthi Priya
Greenhorn
Joined: Apr 02, 2009
Posts: 26
posted
0
After i remove <interceptors> and interceptor-ref tag from action, i'm getting the actual uploaded file
But file name and content type are printing null... am i missing something ?
David Newton wrote:@Leonardo: Start new threads for new topics. There are probably other ways we *could* upload files in S2, but what's wrong with the built-in method using interceptors?
Why are you redefining interceptors? Just use the defaultStack.
In your action you define the "fileUpload" interceptor-ref--but when you define interceptors on a per-action basis you're defining *all* the interceptors the action will use. Your action, then, uses *only* the "fileUpload" interceptor, which is almost certainly not what you intended.
The defaultStack already includes servletConfig and fileUpload: remove *all* interceptor configuration from your configuration file.
Note also that the fileUpload interceptor deletes the file once the action has completed execution--you may need to copy the file elsewhere if you want to preserve it.
David Newton wrote:@Leonardo: Start new threads for new topics. There are probably other ways we *could* upload files in S2, but what's wrong with the built-in method using interceptors?
Alright thank's David for your opinion.. :)
in my case i not yet using file upload..
maybe next in my case i will use file upload.. :-o
and how if there any many files upload?..
how we can save all the files?
how about in the table?..
i think if we want to upload file, we just save name of file to table..
if the file is more than one, do we have to make column more than one to save them one by one?..
or do we have to save them in a column, and save them separated by comma?..
which one is easy?.. and which one is good design?..
Thank's for your opinion.. :)
Sakthi Priya
Greenhorn
Joined: Apr 02, 2009
Posts: 26
posted
0
Yes David, i've getter and setter of all properties
David Newton wrote:Do you have setters for all the file properties?
Could any one please let me know how to restrict the uploading file type to be of xls only ? I do not want to do in javascript or in action class by substringing the filename and throw the error msg.
I would like to know how/what to add in struts.xml inside my action tag
Shall i use the above code inside my action tag, though my struts.xml doesnt contain any <interceptors> tag ? What is the xls format type to be given inside <param> tag ?