You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by hisameer <co...@yahoo.com> on 2009/09/25 18:06:42 UTC

File upload issue with Struts 2.1.6 (uploaded file is always null in action)

I am having trouble with uploading the file using struts 2.1.6. I have to
upload a picture(.jpg, .bmp,.jpeg,.png,.gif) for one of the business
requirement. The application server is JBOSS 5.1.The uploaded file is always
null in my action class:

I followed exactly the same way mentioned in this 
http://struts.apache.org/2.x/docs/file-upload.html link  for the proof of
concept, Here is my code:

jsp code:
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="doUpload" method="post" enctype="multipart/form-data">
    <s:file name="upload" label="File"/>
    <s:submit/>
</s:form>

struts.xml code:
<constant name="struts.multipart.saveDir"
value="C:/Temporary_image_location" />
	<constant name="struts.multipart.maxSize" value="3251468" />

<action name="doUpload" class="com.jctaylor.breg.action.FileUploadAction"
			method="uploadFile">
			<interceptor-ref name="basicStack" />
			<interceptor-ref name="fileUpload">
				image/jpeg,image/gif,image/png,image/bmp,image.jpg
			</interceptor-ref>
			<interceptor-ref name="validation" />
			<interceptor-ref name="workflow" />
			<result
name="input">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
			</result>
			<result
name="success">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
			</result>
			<result
name="error">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
			</result>
		</action>

ActionClass code:
public String uploadFile() throws Exception {
		System.out.println("In  the file upload file method");
		File uploadedContent=getUpload(); 
		String imageName="C:/test_folder/images/image.jpg";
		File copyFile=new File(imageName);
		FileUtils.copyFile(uploadedContent, copyFile);
                return SUCCESS;
           }
public void setUpload(File file) {
		this.file = file;
	}
public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}
public void setUploadFileName(String filename) {
		this.fileName = filename;
	}

I am trying it since last week and always getting the file as null so it
means that struts2 upload is not working or there is something wrong the way
I am doing it. 

Please help! I would really appreciate any kind of help in this regard.


-- 
View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25614736.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by Paweł Wielgus <po...@gmail.com>.
Hi,
have You tried running showcase app,
distributed in struts apps.zip or struts all.zip,
there is working example of file upload.

Best greetings,
Pawel Wielgus.

2009/9/25, hisameer <co...@yahoo.com>:
>
> I am having trouble with uploading the file using struts 2.1.6. I have to
> upload a picture(.jpg, .bmp,.jpeg,.png,.gif) for one of the business
> requirement. The application server is JBOSS 5.1.The uploaded file is always
> null in my action class:
>
> I followed exactly the same way mentioned in this
> http://struts.apache.org/2.x/docs/file-upload.html link  for the proof of
> concept, Here is my code:
>
> jsp code:
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <s:form action="doUpload" method="post" enctype="multipart/form-data">
>     <s:file name="upload" label="File"/>
>     <s:submit/>
> </s:form>
>
> struts.xml code:
> <constant name="struts.multipart.saveDir"
> value="C:/Temporary_image_location" />
> 	<constant name="struts.multipart.maxSize" value="3251468" />
>
> <action name="doUpload" class="com.jctaylor.breg.action.FileUploadAction"
> 			method="uploadFile">
> 			<interceptor-ref name="basicStack" />
> 			<interceptor-ref name="fileUpload">
> 				image/jpeg,image/gif,image/png,image/bmp,image.jpg
> 			</interceptor-ref>
> 			<interceptor-ref name="validation" />
> 			<interceptor-ref name="workflow" />
> 			<result
> name="input">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
> 			</result>
> 			<result
> name="success">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
> 			</result>
> 			<result
> name="error">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
> 			</result>
> 		</action>
>
> ActionClass code:
> public String uploadFile() throws Exception {
> 		System.out.println("In  the file upload file method");
> 		File uploadedContent=getUpload();
> 		String imageName="C:/test_folder/images/image.jpg";
> 		File copyFile=new File(imageName);
> 		FileUtils.copyFile(uploadedContent, copyFile);
>                 return SUCCESS;
>            }
> public void setUpload(File file) {
> 		this.file = file;
> 	}
> public void setUploadContentType(String contentType) {
> 		this.contentType = contentType;
> 	}
> public void setUploadFileName(String filename) {
> 		this.fileName = filename;
> 	}
>
> I am trying it since last week and always getting the file as null so it
> means that struts2 upload is not working or there is something wrong the way
> I am doing it.
>
> Please help! I would really appreciate any kind of help in this regard.
>
>
> --
> View this message in context:
> http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25614736.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by Martin Gainty <mg...@hotmail.com>.
Sameer-

which interceptor 
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" /> 
produced the null fileUpload?
or is it both in combination?
does the error occur for all uploaded image types when these 2 interceptors are configured in
image/jpeg,image/gif,image/png,image/bmp,image.jpg 
?

Martin 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Mon, 28 Sep 2009 09:51:29 -0700
> From: cool_sameer_for_u@yahoo.com
> To: user@struts.apache.org
> Subject: Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)
> 
> 
> Hi Dave,
> 
> Thanks for pointing out the typo in the code. The issue was not that. After
> scratching my head off for a week the issue was in the code:
> <interceptor-ref name="basicStack" />
>                         <interceptor-ref name="fileUpload">
>                                
> image/jpeg,image/gif,image/png,image/bmp,image.jpg
>                         </interceptor-ref>
>                         <interceptor-ref name="validation" />
>                         <interceptor-ref name="workflow" /> 
> 
> for some reason these interceptors were not behaving correctly and thus I
> was getting the null file in my action class. I removed those interceptors
> and the file was not null.
> 
> Thanks everybody for the help.I read these interceptor in the struts2
> website but I think they should have tested it before adding the example:-p
> there. No issue!
> 
> 
> DNewfield wrote:
> > 
> > hisameer wrote:
> >> Well, I dont see any typo here.
> > 
> > image/jpg, not image.jpg
> > 
> > And I assume the email process stripped out the '' and '' parts?
> > 
> > -Dale
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> > 
> > 
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25648913.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  
_________________________________________________________________
Hotmail® has ever-growing storage! Don’t worry about storage limits.
http://windowslive.com/Tutorial/Hotmail/Storage?ocid=TXT_TAGLM_WL_HM_Tutorial_Storage_062009

Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by David Canos <da...@gmail.com>.
try changing the browser, Chrome and Safari works bad with some content-type

2009/9/28 hisameer <co...@yahoo.com>

>
> Hi Dave,
>
> Thanks for pointing out the typo in the code. The issue was not that. After
> scratching my head off for a week the issue was in the code:
> <interceptor-ref name="basicStack" />
>                         <interceptor-ref name="fileUpload">
>
> image/jpeg,image/gif,image/png,image/bmp,image.jpg
>                         </interceptor-ref>
>                        <interceptor-ref name="validation" />
>                        <interceptor-ref name="workflow" />
>
> for some reason these interceptors were not behaving correctly and thus I
> was getting the null file in my action class. I removed those interceptors
> and the file was not null.
>
> Thanks everybody for the help.I read these interceptor in the struts2
> website but I think they should have tested it before adding the example:-p
> there. No issue!
>
>
> DNewfield wrote:
> >
> > hisameer wrote:
> >> Well, I dont see any typo here.
> >
> > image/jpg, not image.jpg
> >
> > And I assume the email process stripped out the '' and '' parts?
> >
> > -Dale
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25648913.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by hisameer <co...@yahoo.com>.
Hi Dave,

Thanks for pointing out the typo in the code. The issue was not that. After
scratching my head off for a week the issue was in the code:
<interceptor-ref name="basicStack" />
                        <interceptor-ref name="fileUpload">
                               
image/jpeg,image/gif,image/png,image/bmp,image.jpg
                        </interceptor-ref>
                        <interceptor-ref name="validation" />
                        <interceptor-ref name="workflow" /> 

for some reason these interceptors were not behaving correctly and thus I
was getting the null file in my action class. I removed those interceptors
and the file was not null.

Thanks everybody for the help.I read these interceptor in the struts2
website but I think they should have tested it before adding the example:-p
there. No issue!


DNewfield wrote:
> 
> hisameer wrote:
>> Well, I dont see any typo here.
> 
> image/jpg, not image.jpg
> 
> And I assume the email process stripped out the '' and '' parts?
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25648913.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by Dale Newfield <da...@newfield.org>.
hisameer wrote:
> Well, I dont see any typo here.

image/jpg, not image.jpg

And I assume the email process stripped out the '<param 
name="allowedTypes">' and '</param>' parts?

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by hisameer <co...@yahoo.com>.
Well, I dont see any typo here.I have tried almost everthing. I made sure
that the name must match whatever is there in the jsp page.If you see
something might be typo let me know. Only the interceptors which are defined
in the action mapping I copied from the struts website.

-Sameer


newton.dave wrote:
> 
> hisameer wrote:
>> 			<interceptor-ref name="fileUpload">
>> 				image/jpeg,image/gif,image/png,image/bmp,image.jpg
> 
> Is that a typo?
> 
> Dave
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25619068.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by Dave Newton <ne...@yahoo.com>.
hisameer wrote:
> 			<interceptor-ref name="fileUpload">
> 				image/jpeg,image/gif,image/png,image/bmp,image.jpg

Is that a typo?

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by hisameer <co...@yahoo.com>.
Hi Dale,

I have added two jar files in the application these jar files are:
commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar.Let me know if there is a
need to add additional jars. I am not getting any file name or content type
so I think nothing is passed to the action class, all the values are null.

 The interceptors which I am using is mentioned in action mapping itself.
The action mapping is: 

<action name="doUpload" class="com.jctaylor.breg.action.FileUploadAction"
			method="uploadFile">
			<interceptor-ref name="basicStack" />
			<interceptor-ref name="fileUpload">
				image/jpeg,image/gif,image/png,image/bmp,image.jpg
			</interceptor-ref>
			<interceptor-ref name="validation" />
			<interceptor-ref name="workflow" />
			<result
name="input">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
			</result>
			<result
name="success">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
			</result>
			<result
name="error">/jctaylor/pages/insuranceApplication/fileUploadExample.jsp
			</result>
		</action>



DNewfield wrote:
> 
> hisameer wrote:
>> Thanks for your reply. I tried to put the method public void
>> setUploadFile(File file) but no success, file is still null in the
>> action.
> 
> Are the other two setters (ContentType, FileName) called?
> Do you have the appropriate interceptor in place for this action?
> I believe there's a .jar required for file upload that isn't part of the 
> standard struts distribution...do you have that in WEB-INF/lib/?
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25618257.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by Dale Newfield <da...@newfield.org>.
hisameer wrote:
> Thanks for your reply. I tried to put the method public void
> setUploadFile(File file) but no success, file is still null in the action.

Are the other two setters (ContentType, FileName) called?
Do you have the appropriate interceptor in place for this action?
I believe there's a .jar required for file upload that isn't part of the 
standard struts distribution...do you have that in WEB-INF/lib/?

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by hisameer <co...@yahoo.com>.
Thanks for your reply. I tried to put the method public void
setUploadFile(File file) but no success, file is still null in the action.



DNewfield wrote:
> 
> hisameer wrote:
>> The uploaded file is always null in my action class:
> 
>>     <s:file name="upload" label="File"/>
> 
> 
>> public void setUpload(File file) {
>> 		this.file = file;
>> 	}
> 
> I believe this should be "public void setUploadFile(File file) {...}"
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25617453.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by Dale Newfield <da...@newfield.org>.
hisameer wrote:
> The uploaded file is always null in my action class:

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


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

I believe this should be "public void setUploadFile(File file) {...}"

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: File upload issue with Struts 2.1.6 (uploaded file is always null in action)

Posted by zsundeep <zs...@gmail.com>.
Hi Sameer,
I have a sample code for file upload in Struts 2.1.6 here. This worked just
fine for me. 
http://www.nabble.com/Unable-to-upload-files-in-Struts2-with-Spring-Security-filter-td25652380.html

I was getting the file as null in action because of issue mentioned here
http://www.nabble.com/Warning:--Spring-Security-and-FileUploadInterceptor-filter-order-td21207025.html

Hope this helps.

Sundeep

-- 
View this message in context: http://www.nabble.com/File-upload-issue-with-Struts-2.1.6-%28uploaded-file-is-always-null-in-action%29-tp25614736p25664511.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org