commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daryl Stultz" <>
Subject Re: fileUpload NullPointerException
Date Fri, 11 Jul 2003 19:11:07 GMT
"Jakarta Commons Users List" <> writes:
>I am guessing the instance you described with three file upload fields 
>falls into one of the following
>	o All three file input fields have the same name. Which would return 
>only one file name.
They are unique. I have another page with only one upload field. If you
put nothing in it is does not throw an exception.
>	o There is some scripting on the client side to account for it.
You mean browser scripting? I assume that if you don't put anything in it,
the browser does not submit the field (that is, the browser doesn't try to
encode nothing for an upload).
>	o You have a test at some point in your code to skip the file upload if 
>the form does not return a file name to the servlet.
Here's my servlet code (in request wrapper) that parses the request:

	DiskFileUpload upload = new DiskFileUpload();
*	List items = upload.parseRequest(this, memoryLimit, fileSizeLimit,
	Iterator iter = items.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem);
		if (item.isFormField()) {
			String key = item.getFieldName();
			String value = item.getString();
			setParameter(key, value, false);
		} else {
			fileUploadItems.put(item.getFieldName(), item); // save to a HashMap
for later use
What I seem to be hearing is that the line with the * is supposed to throw
an exception if you don't enter anything into the upload field. I don't
see how a developer could possibly make use of FileUpload if that were the
case - there would then be no means to use the rest of the parameters to
do something other than crash miserably.

Is it a call to item.write() that would fail if nothing were uploaded?
>	o The servlet does not actually write the file to disk so the 
>fileItem.write(fileName) method is not actually called.

That's true, I guess. There is no FileItem that makes it into my HashMap,
so I never call write on it.
It seems to me that if you don't put anything in the file upload control
on the page, the browser doesn't send anything for it and FileUpload never
sees it in the request and everything works as if there was never a file
upload control on the page in the first place.

Daryl Stultz
6 Degrees Software and Consulting, Inc.

View raw message