You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jacob Ginu <gj...@yahoo.com> on 2003/12/24 03:48:02 UTC

reg:File Upload

Hi,
 
Can anyone send me a sample file Upload program.
 
Regards,
Ginu


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square

Re: File Upload

Posted by Martin Cooper <ma...@apache.org>.
What is it that you are trying to do? Is there a reason you can't just use a
FormFile object in your form bean? That would make your life a whole lot
easier - you wouldn't have to know or care about the request wrapper, and
you wouldn't have to iterate over the uploaded fields.

Also, I'm not sure where you're getting what you're trying to use in the
code you posted. Are you trying to write a Struts app or a WebWork app?
ServletActionContext is a WebWork class, not a Struts class, and
getFileNames() and getFilesystemName() are WebWork methods as well, and not
available on the Struts MultipartRequestWrapper class.

Something is very wrong...

--
Martin Cooper


"Jacob Ginu" <gj...@yahoo.com> wrote in message
news:20031224030232.37217.qmail@web41711.mail.yahoo.com...
> Hi,
>
> This is the sample code I am using in the action.java file.I have the
necessary struts file.But it says Unable to find ServletActionContext in
MultipartRequestWrapper when compiled.
>
> MultipartRequestWrapper multiWrapper =
>             (MultipartRequestWrapper)
> ServletActionContext.getRequest();
>
> Enumeration e = multiWrapper.getFileNames();
> while(e.hasMoreElements()) {
>    // get the value of thisinput tag
>    StringinputValue = (String) e.nextElement();
> // get the content type
>    StringcontentType = multiWrapper.getContentType(inputValue);
> // get the name of the file from the input tag
>    StringfileName = multiWrapper.getFilesystemName(inputValue);
> // Get a File object forthe uploaded File
>    File file = multiWrapper.getFile(inputValue);
> // If it's nullthe upload failed
>    if(file == null) {
>       addActionError("Error uploading: "+
> multiWrapper.getFilesystemName(inputValue));
>    }
>
>
> David Friedman <hu...@ix.netcom.com> wrote:
> Why won't you use the struts-upload.war from the Struts distributions?
>
> Regards,
> David
>
> -----Original Message-----
> From: Jacob Ginu [mailto:gjac081@yahoo.com]
> Sent: Tuesday, December 23, 2003 9:48 PM
> To: struts-user@jakarta.apache.org
> Subject: reg:File Upload
>
> Hi,
>
> Can anyone send me a sample file Upload program.
>
> Regards,
> Ginu
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Photos - Get your photo on the big screen in Times Square
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Photos - Get your photo on the big screen in Times Square




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


RE: File Upload

Posted by David Friedman <hu...@ix.netcom.com>.
Jacob,

That post was all from working examples I had while looking into the problem
(I was curious when I read the original post back then).  Let me know if I
can assist you with anything. Feel free to post your ActionForm and Action
subclasses for assistance.

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Thursday, December 25, 2003 9:30 PM
To: Struts Users Mailing List
Subject: RE: File Upload


David,
No one has got back to me on this except you.There seems to be not much info
regarding this.Iam going throught the link you sent me.
I tried to call MultipartRequestWrapper from an action subclass.I was not
successful in it.

Thanx for your response.

Regards,
Ginu Jacob
David Friedman <hu...@ix.netcom.com> wrote:
Jacob,

Did anyone ever get back to you on this? Where are you trying to get your
MultiPartRequestWrapper?

In an ActionForm subclass, you can use the method:
getMultipartRequestHandler()

In an Action subclass, you can do pull it from above using:
MultipartRequestWrapper multiWrapper = (MultipartRequestWrapper)
form.getMultipartRequestHandler();

OR, if there is no ActionForm defined for your Action subclass, create a new
type of MultipartRequestHandler such as:

CommonsMultipartRequestHandler cmrh = new CommonsMultipartRequestHandler();
cmrh.setServlet(servlet);
cmrh.handleRequest(request);
HashTable h = cmrh.getFileElements();

I used CommonsMultipartRequestHandler in my example (from an old post) but
any appropriate MultiPartRequestHandler subclass should work.
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg87269.html

Just remember 2 things:
a) only one file can be uploaded per file input field - someone tried to
list many files in one upload field and kept seeing Exceptions thrown.

b) if you are not using an ActionForm, there doesn't appear to be a
MultiPartRequestHandler initialized, so create a new one like #2 above.

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 10:03 PM
To: Struts Users Mailing List
Subject: RE: File Upload


Hi,

This is the sample code I am using in the action.java file.I have the
necessary struts file.But it says Unable to find ServletActionContext in
MultipartRequestWrapper when compiled.

MultipartRequestWrapper multiWrapper =
(MultipartRequestWrapper)
ServletActionContext.getRequest();

Enumeration e = multiWrapper.getFileNames();
while(e.hasMoreElements()) {
// get the value of thisinput tag
StringinputValue = (String) e.nextElement();
// get the content type
StringcontentType = multiWrapper.getContentType(inputValue);
// get the name of the file from the input tag
StringfileName = multiWrapper.getFilesystemName(inputValue);
// Get a File object forthe uploaded File
File file = multiWrapper.getFile(inputValue);
// If it's nullthe upload failed
if(file == null) {
addActionError("Error uploading: "+
multiWrapper.getFilesystemName(inputValue));
}


David Friedman wrote:
Why won't you use the struts-upload.war from the Struts distributions?

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 9:48 PM
To: struts-user@jakarta.apache.org
Subject: reg:File Upload

Hi,

Can anyone send me a sample file Upload program.

Regards,
Ginu

---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


RE: File Upload

Posted by Jacob Ginu <gj...@yahoo.com>.
David,
No one has got back to me on this except you.There seems to be not much info regarding this.Iam going throught the link you sent me.
I tried to call MultipartRequestWrapper from an action subclass.I was not successful in it.
 
Thanx for your response.
 
Regards,
Ginu Jacob
David Friedman <hu...@ix.netcom.com> wrote:
Jacob,

Did anyone ever get back to you on this? Where are you trying to get your
MultiPartRequestWrapper?

In an ActionForm subclass, you can use the method:
getMultipartRequestHandler()

In an Action subclass, you can do pull it from above using:
MultipartRequestWrapper multiWrapper = (MultipartRequestWrapper)
form.getMultipartRequestHandler();

OR, if there is no ActionForm defined for your Action subclass, create a new
type of MultipartRequestHandler such as:

CommonsMultipartRequestHandler cmrh = new CommonsMultipartRequestHandler();
cmrh.setServlet(servlet);
cmrh.handleRequest(request);
HashTable h = cmrh.getFileElements();

I used CommonsMultipartRequestHandler in my example (from an old post) but
any appropriate MultiPartRequestHandler subclass should work.
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg87269.html

Just remember 2 things:
a) only one file can be uploaded per file input field - someone tried to
list many files in one upload field and kept seeing Exceptions thrown.

b) if you are not using an ActionForm, there doesn't appear to be a
MultiPartRequestHandler initialized, so create a new one like #2 above.

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 10:03 PM
To: Struts Users Mailing List
Subject: RE: File Upload


Hi,

This is the sample code I am using in the action.java file.I have the
necessary struts file.But it says Unable to find ServletActionContext in
MultipartRequestWrapper when compiled.

MultipartRequestWrapper multiWrapper =
(MultipartRequestWrapper)
ServletActionContext.getRequest();

Enumeration e = multiWrapper.getFileNames();
while(e.hasMoreElements()) {
// get the value of thisinput tag
StringinputValue = (String) e.nextElement();
// get the content type
StringcontentType = multiWrapper.getContentType(inputValue);
// get the name of the file from the input tag
StringfileName = multiWrapper.getFilesystemName(inputValue);
// Get a File object forthe uploaded File
File file = multiWrapper.getFile(inputValue);
// If it's nullthe upload failed
if(file == null) {
addActionError("Error uploading: "+
multiWrapper.getFilesystemName(inputValue));
}


David Friedman wrote:
Why won't you use the struts-upload.war from the Struts distributions?

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 9:48 PM
To: struts-user@jakarta.apache.org
Subject: reg:File Upload

Hi,

Can anyone send me a sample file Upload program.

Regards,
Ginu

---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square

RE: File Upload

Posted by David Friedman <hu...@ix.netcom.com>.
Jacob,

Did anyone ever get back to you on this? Where are you trying to get your
MultiPartRequestWrapper?

In an ActionForm subclass, you can use the method:
getMultipartRequestHandler()

In an Action subclass, you can do pull it from above using:
MultipartRequestWrapper multiWrapper = (MultipartRequestWrapper)
form.getMultipartRequestHandler();

OR, if there is no ActionForm defined for your Action subclass, create a new
type of MultipartRequestHandler such as:

CommonsMultipartRequestHandler cmrh = new CommonsMultipartRequestHandler();
cmrh.setServlet(servlet);
cmrh.handleRequest(request);
HashTable h = cmrh.getFileElements();

I used CommonsMultipartRequestHandler in my example (from an old post) but
any appropriate MultiPartRequestHandler subclass should work.
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg87269.html

Just remember 2 things:
a) only one file can be uploaded per file input field - someone tried to
list many files in one upload field and kept seeing Exceptions thrown.

b) if you are not using an ActionForm, there doesn't appear to be a
MultiPartRequestHandler initialized, so create a new one like #2 above.

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 10:03 PM
To: Struts Users Mailing List
Subject: RE: File Upload


Hi,

This is the sample code I am using in the action.java file.I have the
necessary struts file.But it says Unable to find ServletActionContext in
MultipartRequestWrapper when compiled.

MultipartRequestWrapper multiWrapper =
            (MultipartRequestWrapper)
ServletActionContext.getRequest();

Enumeration e = multiWrapper.getFileNames();
while(e.hasMoreElements()) {
   // get the value of thisinput tag
   StringinputValue = (String) e.nextElement();
// get the content type
   StringcontentType = multiWrapper.getContentType(inputValue);
// get the name of the file from the input tag
   StringfileName = multiWrapper.getFilesystemName(inputValue);
// Get a File object forthe uploaded File
   File file = multiWrapper.getFile(inputValue);
// If it's nullthe upload failed
   if(file == null) {
      addActionError("Error uploading: "+
multiWrapper.getFilesystemName(inputValue));
   }


David Friedman <hu...@ix.netcom.com> wrote:
Why won't you use the struts-upload.war from the Struts distributions?

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 9:48 PM
To: struts-user@jakarta.apache.org
Subject: reg:File Upload

Hi,

Can anyone send me a sample file Upload program.

Regards,
Ginu

---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


RE: File Upload

Posted by Jacob Ginu <gj...@yahoo.com>.
Hi,
 
This is the sample code I am using in the action.java file.I have the necessary struts file.But it says Unable to find ServletActionContext in MultipartRequestWrapper when compiled.
 
MultipartRequestWrapper multiWrapper =
            (MultipartRequestWrapper) 
ServletActionContext.getRequest();

Enumeration e = multiWrapper.getFileNames();
while(e.hasMoreElements()) {
   // get the value of thisinput tag
   StringinputValue = (String) e.nextElement();
// get the content type
   StringcontentType = multiWrapper.getContentType(inputValue);
// get the name of the file from the input tag
   StringfileName = multiWrapper.getFilesystemName(inputValue);
// Get a File object forthe uploaded File
   File file = multiWrapper.getFile(inputValue);
// If it's nullthe upload failed
   if(file == null) {
      addActionError("Error uploading: "+
multiWrapper.getFilesystemName(inputValue));
   }


David Friedman <hu...@ix.netcom.com> wrote:
Why won't you use the struts-upload.war from the Struts distributions?

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 9:48 PM
To: struts-user@jakarta.apache.org
Subject: reg:File Upload

Hi,

Can anyone send me a sample file Upload program.

Regards,
Ginu

---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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


---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square

RE: File Upload

Posted by David Friedman <hu...@ix.netcom.com>.
Why won't you use the struts-upload.war from the Struts distributions?

Regards,
David

-----Original Message-----
From: Jacob Ginu [mailto:gjac081@yahoo.com]
Sent: Tuesday, December 23, 2003 9:48 PM
To: struts-user@jakarta.apache.org
Subject: reg:File Upload

Hi,
 
Can anyone send me a sample file Upload program.
 
Regards,
Ginu

---------------------------------
Do you Yahoo!?
Yahoo! Photos - Get your photo on the big screen in Times Square


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