*
The moose likes Struts and the fly likes Struts2: file upload not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2: file upload not working" Watch "Struts2: file upload not working" New topic
Author

Struts2: file upload not working

Sakthi Priya
Greenhorn

Joined: Apr 02, 2009
Posts: 26
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

Struts.xml
<interceptors>
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor-stack name="basicStack">
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="fileUpload"/>
</interceptor-stack>
</interceptors>

<action name="uploadFile" method="uploadFile" class="com.example.MyAction">
<interceptor-ref name="fileUpload"/>
<exception-mapping exception="java.lang.Exception" result="error"/>
<result name="success">/jsp/success.jsp</result>
</action>

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


myForm.jsp
<s:form action="goSubRegn" method="POST" enctype="multipart/form-data">
<s:file size="110" name="subUploadFile"/>

All the S.O.P. are printing null
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Sorry OOT..

for file uploading in Struts2 we have to use interceptor?..
is it only one solution for upload file in Struts2?..

Thank's in advance..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@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?

@Sakthi: Please read UseCodeTags and UseRealWords.

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
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?

@Sakthi: Please read UseCodeTags and UseRealWords.

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
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?

@Sakthi: Please read UseCodeTags and UseRealWords.

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
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Do you have setters for all the file properties?
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

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
Yes David, i've getter and setter of all properties

David Newton wrote:Do you have setters for all the file properties?
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Hi Priya,

I am uploading file like this.

jsp looks like this.



getter and setter for these fields.


calling upload method.
uploadDocument(upload, uploadFileName, path);

upload method.
remember path means real path.



and make sure to add necessary jar files needed for file uploading in struts2

Cheers!!! Bilal
Handsome Android||@bilal_speed||LinkedIn
Sakthi Priya
Greenhorn

Joined: Apr 02, 2009
Posts: 26
Hi,

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 ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please start new threads for new topics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2: file upload not working
 
Similar Threads
Struts2 FileUpload - request is nulled
struts2 login interceptor not finding session attribute of user details.
struts2 problem
struts2 + interceptor
Struts 2 Fileupload problem