TimTroy
Jul 11th, 2006, 03:51 PM
I am attempting to upload files and write them to the file system for storage. I read the usage/example pages, http://jakarta.apache.org/commons/fileupload/using.html and http://www.onjava.com/lpt/a/3932. The debug log appears to show that the file was uploaded, but, why does the parseRequest return nothing? Have I messed up something? Any suggestions? Thanks in advance.
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List items = upload.parseRequest(request);
System.out.println("***** items = " + items);
The output shows an empty list.
***** items = []
If I run the following code, I can see the file.
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator fileNames = multipartRequest.getFileNames();
if (fileNames.hasNext()) {
String fileName = (String) fileNames.next();
System.out.println("***** fileName = " + fileName);
MultipartFile file = multipartRequest.getFile(fileName);
System.out.println("***** file size : " + file.getSize());
}
***** fileName = myimage
***** file size : 1072266
DEBUG - DispatcherServlet.doService(702) | DispatcherServlet with name 'fileupload' received request for [/clims/fileupload/fileLoad]
DEBUG - DispatcherServlet.doDispatch(769) | Bound request context to thread: Http Request: /clims/fileupload/fileLoad
DEBUG - CommonsFileUploadSupport.parseFileItems(254) | Found multipart file [myimage] of size 1072266 bytes with original filename [DOH471A.bmp], stored at [C:\bea\user_projects\domains\tetdomain\.\tetserver \.wlnotdelete\extract\tetserver_fileupload_clims\p ublic\upload_11900d6_10c5e0a98b8__7ff6_00000001.tm p]
DEBUG - DispatcherServlet.getHandler(932) | Testing handler map [org.springframework.web.servlet.handler.SimpleUrlH andlerMapping@1c977ef] in DispatcherServlet with name 'fileupload'
DEBUG - AbstractUrlHandlerMapping.getHandlerInternal(135) | Looking up handler for [/fileLoad]
DEBUG - DispatcherServlet.getHandlerAdapter(972) | Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHan dlerAdapter@1e0fd3d]
DEBUG - DispatcherServlet.getHandlerAdapter(972) | Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter@11c960]
DEBUG - AbstractUrlMethodNameResolver.getHandlerMethodName (99) | Returning handler method name 'processFileUpload' for lookup path: /fileLoad
***** FileController.processFileUpload(req, res)
***** fileName = myimage
***** file size : 1072266
***** items = []
DEBUG - AbstractCachingViewResolver.resolveViewName(82) | Cached view 'redirect:fileList'
DEBUG - DispatcherServlet.render(1053) | Rendering view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [fileList]] in DispatcherServlet with name 'fileupload'
DEBUG - AbstractView.render(232) | Rendering view with name 'null' with model null and static attributes {}
DEBUG - CommonsFileUploadSupport.cleanupFileItems(274) | Cleaning up multipart file [myimage] with original filename [DOH471A.bmp], stored at [C:\bea\user_projects\domains\tetdomain\.\tetserver \.wlnotdelete\extract\tetserver_fileupload_clims\p ublic\upload_11900d6_10c5e0a98b8__7ff6_00000001.tm p]
DEBUG - DispatcherServlet.doDispatch(858) | Cleared thread-bound request context: Http Request: /clims/fileupload/fileLoad
DEBUG - FrameworkServlet.processRequest(416) | Successfully completed request
<form name="myform" action="fileLoad" method="post" enctype="multipart/form-data">
<h1>
Select your file(s) for upload
</h1>
Specify your Name:<br />
<input type="text" name="name" size="15"/><br />
Specify your Image:<br />
<input type="file" name="myimage"><br/>
<br />
<input type="submit" name="Submit" value="Submit your files"/>
</form>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- MultipartResolver for parsing file uploads, implementation for Commons FileUpload -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsM ultipartResolver"/>
<!-- HandlerMapping that dispatches all request to the MultiActionController below -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="defaultHandler" ref="fileController"/>
</bean>
<!-- MultiActionController that defines user interface actions as separate methods -->
<bean id="fileController" class="org.wadsworth.clims.fileupload.FileController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.Pr opertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/fileList">showFileList</prop>
<prop key="/fileContent">streamFileContent</prop>
<prop key="/fileLoad">processFileUpload</prop>
</props>
</property>
</bean>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List items = upload.parseRequest(request);
System.out.println("***** items = " + items);
The output shows an empty list.
***** items = []
If I run the following code, I can see the file.
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator fileNames = multipartRequest.getFileNames();
if (fileNames.hasNext()) {
String fileName = (String) fileNames.next();
System.out.println("***** fileName = " + fileName);
MultipartFile file = multipartRequest.getFile(fileName);
System.out.println("***** file size : " + file.getSize());
}
***** fileName = myimage
***** file size : 1072266
DEBUG - DispatcherServlet.doService(702) | DispatcherServlet with name 'fileupload' received request for [/clims/fileupload/fileLoad]
DEBUG - DispatcherServlet.doDispatch(769) | Bound request context to thread: Http Request: /clims/fileupload/fileLoad
DEBUG - CommonsFileUploadSupport.parseFileItems(254) | Found multipart file [myimage] of size 1072266 bytes with original filename [DOH471A.bmp], stored at [C:\bea\user_projects\domains\tetdomain\.\tetserver \.wlnotdelete\extract\tetserver_fileupload_clims\p ublic\upload_11900d6_10c5e0a98b8__7ff6_00000001.tm p]
DEBUG - DispatcherServlet.getHandler(932) | Testing handler map [org.springframework.web.servlet.handler.SimpleUrlH andlerMapping@1c977ef] in DispatcherServlet with name 'fileupload'
DEBUG - AbstractUrlHandlerMapping.getHandlerInternal(135) | Looking up handler for [/fileLoad]
DEBUG - DispatcherServlet.getHandlerAdapter(972) | Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHan dlerAdapter@1e0fd3d]
DEBUG - DispatcherServlet.getHandlerAdapter(972) | Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter@11c960]
DEBUG - AbstractUrlMethodNameResolver.getHandlerMethodName (99) | Returning handler method name 'processFileUpload' for lookup path: /fileLoad
***** FileController.processFileUpload(req, res)
***** fileName = myimage
***** file size : 1072266
***** items = []
DEBUG - AbstractCachingViewResolver.resolveViewName(82) | Cached view 'redirect:fileList'
DEBUG - DispatcherServlet.render(1053) | Rendering view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [fileList]] in DispatcherServlet with name 'fileupload'
DEBUG - AbstractView.render(232) | Rendering view with name 'null' with model null and static attributes {}
DEBUG - CommonsFileUploadSupport.cleanupFileItems(274) | Cleaning up multipart file [myimage] with original filename [DOH471A.bmp], stored at [C:\bea\user_projects\domains\tetdomain\.\tetserver \.wlnotdelete\extract\tetserver_fileupload_clims\p ublic\upload_11900d6_10c5e0a98b8__7ff6_00000001.tm p]
DEBUG - DispatcherServlet.doDispatch(858) | Cleared thread-bound request context: Http Request: /clims/fileupload/fileLoad
DEBUG - FrameworkServlet.processRequest(416) | Successfully completed request
<form name="myform" action="fileLoad" method="post" enctype="multipart/form-data">
<h1>
Select your file(s) for upload
</h1>
Specify your Name:<br />
<input type="text" name="name" size="15"/><br />
Specify your Image:<br />
<input type="file" name="myimage"><br/>
<br />
<input type="submit" name="Submit" value="Submit your files"/>
</form>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- MultipartResolver for parsing file uploads, implementation for Commons FileUpload -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsM ultipartResolver"/>
<!-- HandlerMapping that dispatches all request to the MultiActionController below -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="defaultHandler" ref="fileController"/>
</bean>
<!-- MultiActionController that defines user interface actions as separate methods -->
<bean id="fileController" class="org.wadsworth.clims.fileupload.FileController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.Pr opertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/fileList">showFileList</prop>
<prop key="/fileContent">streamFileContent</prop>
<prop key="/fileLoad">processFileUpload</prop>
</props>
</property>
</bean>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>