Answer
From Thomas Moore's Servlet FAQ:
Form-based file upload requires a couple of steps.
The server must supply (and the client must support)
encoding type multipart/form-data. Most current browsers
do, but it's not a guarantee. Secondly (and this is
usually the trickiest part), your servlet has to parse the
binary data and do something with it (e.g., write it to a
file on the server).
The intrepid programmer is referred to RFC 1867 for
cluefulness on how to parse this data. Less brave souls
can use either Jason Hunter's implementation of a
MultipartRequest (available from http://www.servlets.com),
or CParseRFC1867 (available from http://www.servletcentral.com).
Note that the source code is available for both of these
examples, but both assume that you will be writing the
file to a file on the server. Other uses (e.g. storing
the file as a binary object in a database) will require
adaptation.
There is a multipart/form parser availailable from
Anders Kristensen (http://www-uk.hpl.hp.com/people/ak/java/, [email protected])
at http://www-uk.hpl.hp.com/people/ak/java/#utils.
JavaMail also has MIME-parsing routines (see the Purple Servlet References).
Here is an example of HTML code that allows file upload, courtesy of
Detlef Pleiss ([email protected]):
<FORM ENCTYPE="multipart/form-data"
method=post action="/myservlet">
<INPUT TYPE="file" NAME="mptest">
<INPUT TYPE="submit" VALUE="upload">
</FORM>
The input type "file" brings up a button for a file select box on the
browser together with a text field that takes the file name once
selected. The servlet uses the GET method parameters to decide what
to do with the upload while the POST body of the request contains the
file data to parse. Tested with IE4, IE5 and Netscape 4.5.
Comments and alternative answers
 |
I recently re-implemented Jason's MultipartRequest...
Geoff Soutter, Nov 29, 2000
I recently re-implemented Jason's MultipartRequest - that class is now a thin wrapper around a new class called MultipartRequest. This new class allows one to get at the data without buffering it or writing it to disk. It also has fixes to avoid performance problems associated with poorly implemented servlet containers. I donated it back to Jason in honour of the service he has provided the java/servlet community and he has uploaded it onto servlets.com; so feel free to check it out.

|
|  |
Re: I have been using the multipart-sparser class from...
Zach Gatu, Mar 21, 2001
Ranjith
I want to use that Oreilly multipart-parser to read an xml document.
The file will be read in from the client's disk via an HTML form and POSTed
to a jsp file. How do I read the xml file? Below is a bit of my JSP code.
<%!
String filename = "http://localhost:8080/LibCatalog/xml/test.xml";
...
%>
<%
DOMParser parser = new DOMParser();
parser.parse(this.filename);
...
%>
As you can see, the xml file is hard coded. How do I read it from the HTML form?
Do you have any code examples? Pls help.
Thanks,
Zach.

|
 |
I have found out source of the problem that I faced...
Ranjith Rufus, Mar 2, 2001 [1]
I have found out source of the problem that I faced in uploading files using the Jason Hunter's O'Reilly API.. The problem was more specific to the WebServer than to the API..
Apache Jserv is not capable of handling images, doc, compressed files as Mulitpart/form data.. But if you try the same API and servlet using Apache Tomcat, it does well

|
|  |
Re: I have found out source of the problem that I faced...
Constantine Evenko, Mar 23, 2001
Yeah, you partly right. It's the problem of WebServer, but this isn't the problem of ApacheJServ, this is the problem of Apache. I try to do this, and have following results:
on Tomcat - works
on Tomcat+Apache - it isn't work (!)
on ApacheJServ+Apache - it isn't work to.
Apache says "Bad request" when in InputStream appears symbol 0xFF (255). That's why it's OK when uploading TEXT files and fails when BINARY.
There is the problem. But I still have no idea of how solve it. Did anybody have?

|
|
 |

Need help fast? Submit your Java question to 45,000 developers.
Sign up now and receive $25 matching credit!
|
|
|