Hello,
I am using Jason Hunters MultiPartParser to allow users
to upload image files to an on-line Scrapbook.
Because each user has his own directory to place pictures in,
the directory to save the picture in is specified in the
form inputs. Specifically, I have a form with these input
parameters in this order
requesttype -- a hidden input field
category -- indicates which directory to save the file in
password -- Each user has a password
title -- title of the picture
caption -- a caption for the picture
file -- the actual file input
I use a while loop to read and analyze each part:
-- if the part is a parameter part
save the value in the appropriate variable
-- if the part is a file part
read the file into an array of bytes
-- read the next part and continue the loop untill they are
all read
here is the while loop code (part of my doGet() method)
MultipartParser mp = new MultipartParser(request, 1024*1024); // 100K???
while ( (part = mp.readNextPart()) != null)
{
if (part.isParam())
{
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
String name = paramPart.getName();
if (verbose)
{
out.println("Parameter " + name + "=" + value +"\n");
}
//map form input values to appropriate variables
if (name.equals("requesttype"))
requestType = value;
else if (name.equals("category") )
category = value.toLowerCase();
else if (name.equals("caption"))
caption = value;
else if (name.equals("title"))
title = value;
else if (name.equals("index"))
index = Integer.parseInt(value);
else if (name.equals("password"))
password = value;
}//end if
else if (part.isFile())
{
/**
* The file data must be read in here before reading
* the next part or it will be lost. See
* documentation for FilePart.getInputStream()
*
*/
FilePart filePart = (FilePart) part;
if (verbose) out.println("Reading file: ");
InputStream inStream = filePart.getInputStream();
length = inStream.read(fileData);
if (verbose)
{
out.println("File Name: " + filePart.getFileName());
out.println("Bytes read into Program: " + length);
out.println("Max Bytes: " + maxBytes);
out.println("Content Type: " +
filePart.getContentType() + "\n");
}
fileName = filePart.getFileName();
//check the content type:Gifs/Jpgs only
String content = filePart.getContentType();
if (!content.equals("image/jpeg") &&
!content.equals("image/gif") &&
!content.equals("image/pjpeg"))
throw new DataFormatException
("Files of type " + content + " are not
allowed.");
//check the size
if (length > maxBytes)
throw new DataFormatException
("The file " + fileName + " has a size of " +
length +
". The maximum file Size is " + maxBytes);
}//end else
part = mp.readNextPart();
}//end while
When I run this servlet from a web Browser, I get the following output
(note that it seems to be trying to read a filePart twice, even though
there is only one file Input in the form)
Reading file:
File Name: cheryse1.gif
Bytes read into Program: 7618
Max Bytes: 20000
Content Type: image/gif
Parameter password=admin
Parameter caption=caption
Parameter category=cheryse
Parameter title=title
Reading file:
java.io.IOException: unexpected end of part
at PartInputStream.fill(PartInputStream.java:87)
at PartInputStream.read(PartInputStream.java:153)
at java.io.FilterInputStream.read(FilterInputStream.java:97)
at ScrapBookServlet.doPost(ScrapBookServlet.java:131)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java)
at
org.apache.tomcat.facade.ServletHandler.doService(ServletHandler.java:497)
at org.apache.tomcat.core.Handler.invoke(Handler.java:322)
at org.apache.tomcat.core.Handler.service(Handler.java:235)
at
org.apache.tomcat.facade.ServletHandler.service(ServletHandler.java:448)
at
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:918)
at
org.apache.tomcat.core.ContextManager.service(ContextManager.java:831)
at
org.apache.tomcat.modules.server.Ajp13Interceptor.processConnection(Ajp13Interceptor.java:167)
at
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:478)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:517)
at java.lang.Thread.run(Thread.java:479)
The exception occurs on the code
length = inStream.read(fileData);
What causes a a MultiPartParser to throw a "unexpected end of part"??, and how
can I avoid it?
--Monte Glenn Gardner
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html