You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Thomas Joseph Olaes <co...@olaes.net> on 2004/07/23 19:51:47 UTC

[FileUpload] Question re Sample Code for MultipartStream class (what is OutputStream output for?)

Hello List,

This is my first email post to the group.

My question regarding the example code listed at the top of the
MultipartStream class javadoc page: What is OutputStream output for? Is
it for the "out" variable? I'm getting an error with my current code
saying I have to initialize it... how do I do this? I was reading
through the javadoc and supposedly readBodyData(OutputStream) is
supposed to give the outputstream a value, but is there some kind of
initialization I have to do before hand? Like point the OutputStream
towards the "out" variable somehow?

 I've messed around with the example correcting the errors and stuff,
declaring variables and giving them instances, and I just want to get
something that works on Tomcat so I can fiddle with it and see what the
different functions do. So far I have:

<%@ page session="false" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<pre>
<%
if(FileUpload.isMultipartContent(request)){
  // request is a multipart/form-data,
javax.servlet.http.HttpServletRequest Interface Object
  out.println("lalala");

  //Enumeration eHeaderNames = new Enumeration;
  
  InputStream input = request.getInputStream(); // new InputStream();
  byte[] boundary = new byte[4096];
  
  String header;

  try {
    MultipartStream multipartStream = new MultipartStream(input,
boundary);
    boolean nextPart = multipartStream.skipPreamble();
    OutputStream output;
    PrintWriter pw = new PrintWriter(output);
    
    while(nextPart) {
      header = multipartStream.readHeaders();
      // process headers


      // create some output stream
      multipartStream.readBodyData(output);
      nextPart = multipartStream.readBoundary();
    }
  }
  catch(MultipartStream.MalformedStreamException e) {
    // the stream failed to follow required syntax
  } catch(IOException e) {
    // a read or write error occurred
  }
}
%>
</pre>
<html>
  <body>
    <form method="post" action="upload-test.jsp"
enctype="multipart/form-data">
      <input type="text" name="something" value="<%=
request.getParameter("something") %>" /><br />
      <input type="text" name="something1" value="<%=
request.getParameter("something1") %>" /><br />
      <input type="text" name="something2" value="<%=
request.getParameter("something2") %>" /><br />
      <input type="file" name="myfileupload" /><br />
      <input type="submit" />
    </form>
    <a href="upload-test.jsp">rest</a>
  </body>
</html>

What I'm trying to do ultimately is get the file in myfileupload to
upload a text file which I will later parse, but I can't get the file
upload to work so I'm trying to figure out how to get that part going.

What I've resorted to is starting with this example code, correcting
errors, defining variables before the code piece runs, and just
generally trying to get something working. I'm hoping to get help from
this mailing list, but if not I will keep looking through the javadocs
and fumbling my way through the darkness.

Thanks in advance for the help!

-TJ



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


Re: [FileUpload] Question re Sample Code for MultipartStream class (what is OutputStream output for?)

Posted by Martin Cooper <mf...@gmail.com>.
On Fri, 23 Jul 2004 11:14:58 -0700, Thomas Joseph Olaes
<co...@olaes.net> wrote:
> Craig,
> 
> Thank you for your reply, this has cleared things up somewhat for me.
> 
> List,
> 
> I wonder if there are any sites out there that I haven't come across yet
> that will give me a few hints on how to get a text file upload working
> on a JSP script. I'm only 3 days into working with JSPs, but I have
> experience with ColdFusion and PHP which apparently tend to do a lot of
> stuff for you behind the scenes, including file upload.

Another way of saying what Craig said is "don't do that". ;-) You
should not be trying to handle multipart uploads from a JSP page with
*any* package that you come across - you should be using a servlet for
that instead. Otherwise you're likely to land yourself in trouble. All
you need is a little servlet that wraps up FileUpload, and you'll be
all set.

--
Martin Cooper


> 
> Thanks in advance for any help
> -TJ
> 
> 
> 
> > -----Original Message-----
> > From: Craig McClanahan [mailto:craigmcc@gmail.com]
> > Sent: Friday, July 23, 2004 11:03 AM
> > To: Jakarta Commons Users List
> > Subject: Re: [FileUpload] Question re Sample Code for MultipartStream
> > class (what is OutputStream output for?)
> >
> > On Fri, 23 Jul 2004 10:51:47 -0700, Thomas Joseph Olaes
> > <co...@olaes.net> wrote:
> > > Hello List,
> > >
> > > This is my first email post to the group.
> > >
> > > My question regarding the example code listed at the top of the
> > > MultipartStream class javadoc page: What is OutputStream output for?
> Is
> > > it for the "out" variable? I'm getting an error with my current code
> > > saying I have to initialize it... how do I do this? I was reading
> > > through the javadoc and supposedly readBodyData(OutputStream) is
> > > supposed to give the outputstream a value, but is there some kind of
> > > initialization I have to do before hand? Like point the OutputStream
> > > towards the "out" variable somehow?
> >
> > Thomas,
> >
> > One thing to keep in mind is that the sample code you are looking at
> > is designed to be run inside a servlet, not a JSP page.  I suspect
> > that, given what a JSP page does to the incoming request, this is
> > never going to work.  I would also suggest that you look at the
> > example on the "Package Documentation" page of the
> > org.apache.commons.fileupload Javadocs ... it shows how to use the
> > higher level APIs (DiskFileUpload) instead of the low level one you
> > are using.
> >
> > With regards to your question, The "output" variable (of type
> > OutputStream) should be initialized to wherever you want to write the
> > content of the body part in question.
> >
> > Craig
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
>

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


RE: [FileUpload] Question re Sample Code for MultipartStream class (what is OutputStream output for?)

Posted by Thomas Joseph Olaes <co...@olaes.net>.
Craig,

Thank you for your reply, this has cleared things up somewhat for me.

List,

I wonder if there are any sites out there that I haven't come across yet
that will give me a few hints on how to get a text file upload working
on a JSP script. I'm only 3 days into working with JSPs, but I have
experience with ColdFusion and PHP which apparently tend to do a lot of
stuff for you behind the scenes, including file upload.

Thanks in advance for any help
-TJ

> -----Original Message-----
> From: Craig McClanahan [mailto:craigmcc@gmail.com]
> Sent: Friday, July 23, 2004 11:03 AM
> To: Jakarta Commons Users List
> Subject: Re: [FileUpload] Question re Sample Code for MultipartStream
> class (what is OutputStream output for?)
> 
> On Fri, 23 Jul 2004 10:51:47 -0700, Thomas Joseph Olaes
> <co...@olaes.net> wrote:
> > Hello List,
> >
> > This is my first email post to the group.
> >
> > My question regarding the example code listed at the top of the
> > MultipartStream class javadoc page: What is OutputStream output for?
Is
> > it for the "out" variable? I'm getting an error with my current code
> > saying I have to initialize it... how do I do this? I was reading
> > through the javadoc and supposedly readBodyData(OutputStream) is
> > supposed to give the outputstream a value, but is there some kind of
> > initialization I have to do before hand? Like point the OutputStream
> > towards the "out" variable somehow?
> 
> Thomas,
> 
> One thing to keep in mind is that the sample code you are looking at
> is designed to be run inside a servlet, not a JSP page.  I suspect
> that, given what a JSP page does to the incoming request, this is
> never going to work.  I would also suggest that you look at the
> example on the "Package Documentation" page of the
> org.apache.commons.fileupload Javadocs ... it shows how to use the
> higher level APIs (DiskFileUpload) instead of the low level one you
> are using.
> 
> With regards to your question, The "output" variable (of type
> OutputStream) should be initialized to wherever you want to write the
> content of the body part in question.
> 
> Craig
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 




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


Re: [FileUpload] Question re Sample Code for MultipartStream class (what is OutputStream output for?)

Posted by Craig McClanahan <cr...@gmail.com>.
On Fri, 23 Jul 2004 10:51:47 -0700, Thomas Joseph Olaes
<co...@olaes.net> wrote:
> Hello List,
> 
> This is my first email post to the group.
> 
> My question regarding the example code listed at the top of the
> MultipartStream class javadoc page: What is OutputStream output for? Is
> it for the "out" variable? I'm getting an error with my current code
> saying I have to initialize it... how do I do this? I was reading
> through the javadoc and supposedly readBodyData(OutputStream) is
> supposed to give the outputstream a value, but is there some kind of
> initialization I have to do before hand? Like point the OutputStream
> towards the "out" variable somehow?

Thomas,

One thing to keep in mind is that the sample code you are looking at
is designed to be run inside a servlet, not a JSP page.  I suspect
that, given what a JSP page does to the incoming request, this is
never going to work.  I would also suggest that you look at the
example on the "Package Documentation" page of the
org.apache.commons.fileupload Javadocs ... it shows how to use the
higher level APIs (DiskFileUpload) instead of the low level one you
are using.

With regards to your question, The "output" variable (of type
OutputStream) should be initialized to wherever you want to write the
content of the body part in question.

Craig

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