You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jim Menard <ji...@io.com> on 2004/01/13 18:20:42 UTC

Multipart form decoding

I need to accept a multipart form from a client application. The form 
contains multiple parameters and multiple files (all having unique 
names). I'm trying to use DefaultMultipartDecoder, but when I run the 
test code below, the form field values I try to print are all "null".

I've written a servlet separate from Tapestry to accept the form, 
mostly because I don't know how I would do this from within a Tapestry 
page. Is that possible?

As always, thanks for your help.

     Logger logger = Logger.getLogger(getClass().getName());
     try {
		// The client isn't setting the character encoding. I'm
		// guessing, but since it's a Windows app this is reasonable.
         if (request.getCharacterEncoding() == null)
             request.setCharacterEncoding("ISO-8859-1");

         logger.debug("isMultipartRequest = "
                      + 
DefaultMultipartDecoder.isMultipartRequest(request));
         logger.debug("getting decoder");
         DefaultMultipartDecoder decoder =
             DefaultMultipartDecoder.getSharedInstance();
         logger.debug("decoder = " + decoder);
         logger.debug("repository path = " + 
decoder.getRepositoryPath());
         logger.debug("max size = " + decoder.getMaxSize());

         logger.debug("decoding");
         decoder.decode(request);
         logger.debug("done decoding");

         String title = getString(decoder, request, "title");
         String caption = getString(decoder, request, "caption");
		//... lots more ...
         IUploadFile thumbFile = getFile(decoder, request, "tmb");
         IUploadFile screenFile = getFile(decoder, request, "screen");
	}
	catch // ...
}

protected String getString(IMultipartDecoder decoder,
                            HttpServletRequest request, String key)
{
     String val = decoder.getString(request, key);
     if (val != null) val = val.trim();
     Logger.getLogger(getClass().getName()).debug(key + " = " + val);
     return val;
}

protected IUploadFile getFile(IMultipartDecoder decoder,
                               HttpServletRequest request, String key)
{
     IUploadFile iFile = decoder.getUploadFile(request, key);
     Logger.getLogger(getClass().getName()).debug(key + " (file) = " +
                                                  (iFile == null ? null
                                                   : 
iFile.getFilePath()));
     return iFile;
}

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"I can picture in my mind a world without war, a world without hate. And
I can picture us attacking that world, because they'd never expect it."
     -- Jack Handey


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


Re: Multipart form decoding

Posted by Jim Menard <ji...@io.com>.
After reading the DefaultMultipartDecoder code, I've realized that it 
ignores any form parameters in the POST and just grabs files.

I'll use com.oreilly.servlet.MultipartParser and friends instead.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"The theory of computation states that all automatons can be emulated
by a Turing machine. I have a less abstract but more practical motto: If
you can do it on Intel, you can do it damn near anywhere!"
     -- Eugene O'Neil


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


Re: Multipart form decoding

Posted by Jim Menard <ji...@io.com>.
On Jan 13, 2004, at 7:23 PM, Howard M. Lewis Ship wrote:

> That would surprise me, as we have a working unit test for just this 
> case (mixed form and file
> upload).

If I have time, I will try to create a Tapestry page with a form in it, 
and see what I get in the form's submit method.

Hmm...maybe that's the problem: I used DefaultMultipartDecoder by 
itself, outside of Tapestry. Could that be it? I don't see how.

> What servlet container are you using?

Tomcat.

>   You're best bet is to use HTTP POST and put just the servlet
> into the URL, and use hidden fields for the service and sp values 
> (this is what the Form component
> itself does).  Servlet containers may ignore additional query 
> parameters in the URL when a POST
> occurs --- some do, some don't.

Thanks. I'll try that when I have time.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
"No, no, you're not thinking; you're just being logical." -- Niels Bohr


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


RE: Multipart form decoding

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
That would surprise me, as we have a working unit test for just this case (mixed form and file
upload).

What servlet container are you using?  You're best bet is to use HTTP POST and put just the servlet
into the URL, and use hidden fields for the service and sp values (this is what the Form component
itself does).  Servlet containers may ignore additional query parameters in the URL when a POST
occurs --- some do, some don't.

--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Tapestry: Java Web Components 
http://jakarta.apache.org/tapestry/
http://javatapestry.blogspot.com

> -----Original Message-----
> From: Jim Menard [mailto:jimm@io.com] 
> Sent: Tuesday, January 13, 2004 4:55 PM
> To: Tapestry users
> Subject: Re: Multipart form decoding
> 
> 
> On Jan 13, 2004, at 4:44 PM, Howard M. Lewis Ship wrote:
> 
> > I'm a bit rusty on the multipart handling code, but it should Just 
> > Work, as is. Tapestry detects
> > multipart form submissions and uses DefaultMultipartDecoder 
> to decode 
> > them; you can access the
> > results via the RequestContext's getParameter() method.
> 
> No, it does not Just Work. There are two types of form 
> values: file and 
> parameter (form field). I looked at the DefaultMultipartDecoder code, 
> and it looks like it explicitly ignores parameters and just saves 
> files. I also wrote test code (posted in an earlier email) that shows 
> that the form fields are ignored.
> 
> Besides, what URL would I give the external client program? 
> Would it be 
> something like
> 
	http://myserver/appname/app?service=page/MagicFormPage

If so, then I'd need to have only one form on the page; nothing else. 
Simple enough, I suppose.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
---- BEGIN META GEEK CODE ----
gc
---- END META GEEK CODE ----


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


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


Re: Multipart form decoding

Posted by Jim Menard <ji...@io.com>.
On Jan 13, 2004, at 4:44 PM, Howard M. Lewis Ship wrote:

> I'm a bit rusty on the multipart handling code, but it should Just 
> Work, as is. Tapestry detects
> multipart form submissions and uses DefaultMultipartDecoder to decode 
> them; you can access the
> results via the RequestContext's getParameter() method.

No, it does not Just Work. There are two types of form values: file and 
parameter (form field). I looked at the DefaultMultipartDecoder code, 
and it looks like it explicitly ignores parameters and just saves 
files. I also wrote test code (posted in an earlier email) that shows 
that the form fields are ignored.

Besides, what URL would I give the external client program? Would it be 
something like

	http://myserver/appname/app?service=page/MagicFormPage

If so, then I'd need to have only one form on the page; nothing else. 
Simple enough, I suppose.

Jim
-- 
Jim Menard, jimm@io.com, http://www.io.com/~jimm/
---- BEGIN META GEEK CODE ----
gc
---- END META GEEK CODE ----


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


RE: Multipart form decoding

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
I'm a bit rusty on the multipart handling code, but it should Just Work, as is. Tapestry detects
multipart form submissions and uses DefaultMultipartDecoder to decode them; you can access the
results via the RequestContext's getParameter() method.

--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Tapestry: Java Web Components 
http://jakarta.apache.org/tapestry/
http://javatapestry.blogspot.com

> -----Original Message-----
> From: Jim Menard [mailto:jimm@io.com] 
> Sent: Tuesday, January 13, 2004 12:21 PM
> To: Tapestry users
> Subject: Multipart form decoding
> 
> 
> I need to accept a multipart form from a client application. The form 
> contains multiple parameters and multiple files (all having unique 
> names). I'm trying to use DefaultMultipartDecoder, but when I run the 
> test code below, the form field values I try to print are all "null".
> 
> I've written a servlet separate from Tapestry to accept the form, 
> mostly because I don't know how I would do this from within a 
> Tapestry 
> page. Is that possible?
> 
> As always, thanks for your help.
> 
>      Logger logger = Logger.getLogger(getClass().getName());
>      try {
> 		// The client isn't setting the character encoding. I'm
> 		// guessing, but since it's a Windows app this 
> is reasonable.
>          if (request.getCharacterEncoding() == null)
>              request.setCharacterEncoding("ISO-8859-1");
> 
>          logger.debug("isMultipartRequest = "
>                       + 
> DefaultMultipartDecoder.isMultipartRequest(request));
>          logger.debug("getting decoder");
>          DefaultMultipartDecoder decoder =
>              DefaultMultipartDecoder.getSharedInstance();
>          logger.debug("decoder = " + decoder);
>          logger.debug("repository path = " + 
> decoder.getRepositoryPath());
>          logger.debug("max size = " + decoder.getMaxSize());
> 
>          logger.debug("decoding");
>          decoder.decode(request);
>          logger.debug("done decoding");
> 
>          String title = getString(decoder, request, "title");
>          String caption = getString(decoder, request, "caption");
> 		//... lots more ...
>          IUploadFile thumbFile = getFile(decoder, request, "tmb");
>          IUploadFile screenFile = getFile(decoder, request, "screen");
> 	}
> 	catch // ...
> }
> 
> protected String getString(IMultipartDecoder decoder,
>                             HttpServletRequest request, String key)
> {
>      String val = decoder.getString(request, key);
>      if (val != null) val = val.trim();
>      Logger.getLogger(getClass().getName()).debug(key + " = " + val);
>      return val;
> }
> 
> protected IUploadFile getFile(IMultipartDecoder decoder,
>                                HttpServletRequest request, String key)
> {
>      IUploadFile iFile = decoder.getUploadFile(request, key);
>      Logger.getLogger(getClass().getName()).debug(key + " (file) = " +
>                                                   (iFile == 
> null ? null
>                                                    : 
> iFile.getFilePath()));
>      return iFile;
> }
> 
> Jim
> -- 
> Jim Menard, jimm@io.com, http://www.io.com/~jimm/
> "I can picture in my mind a world without war, a world 
> without hate. And
> I can picture us attacking that world, because they'd never 
> expect it."
>      -- Jack Handey
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


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