file upload problem null
wierzgala Mar 2, 2009 9:28 AMHi everyone,
I Am trying to develop a relatively Web 2.0 application, and 6 days ago I faced a problem with <s:fileUpload> tag. I get null for my byte array holding data and also for a contentType. I checked that even setters aren’t called. I wanted to see what is in the request so I added my own filter wrapped around Seam MultipartFilter. It shows basic info about request. Every time I send a request (except request from logging page) the length of request is -1 and contentType is null. I am really confused and stuck for good. I hope somebody will know how to solve this issue. I suspect the answer will be trivial.
Here is my form
<s:validateAll> <h:form id="UploadPhotoForm" enctype="multipart/form-data"> <fieldset> <s:decorate id="imageField"> <s:fileUpload id="imageData" accept="image/*" data="#{upload.imageData}" contentType="#{upload.imageContentType}" fileName="#{upload.title}" /> </s:decorate> <s:button id="save" value="Upload" action="#{upload.savePicture}"/> </fieldset> </h:form> </s:validateAll>
And my action
@Name("upload") @Scope(ScopeType.PAGE) @Local public class UploadPictureAction implements Serializable { @In User user; @Logger Log log; private static final long serialVersionUID = 1L; private byte[] imageData; private String imageContentType; private String title; public byte[] getImageData() { return imageData; } public void setImageData(byte[] imageData) { this.imageData = imageData; System.out.println("Value:"+imageData); } public String getImageContentType() { return imageContentType; } public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public void savePicture() { PicturesManager pm = PicturesManager.getInstance(); try { System.out.println("imageContentType:"+imageContentType); System.out.println("imageData:"+imageData); } catch(Exception e) { log.error(e.getMessage(),e); } }
My components.xml
<core:init debug="true" jndi-pattern="@jndiPattern@"/> <core:manager concurrent-request-timeout="500" conversation-timeout="120000" conversation-id-parameter="cid" parent-conversation-id-parameter="pid"/> <persistence:managed-persistence-context name="em" auto-create="true" entity-manager-factory="#{erasmusDatabase}"/> <persistence:entity-manager-factory name="erasmusDatabase" persistence-unit-name="myErasmus"/> <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/> <web:ajax4jsf-filter force-parser="true" enable-cache="true" log4j-init-file="custom-log4j.xml" url-pattern="*.seam"/> <web:multipart-filter create-temp-files="true" max-request-size="1000000" url-pattern="*.seam" /> <event type="org.jboss.seam.security.notLoggedIn"> <action execute="#{redirect.captureCurrentView}"/> </event> <event type="org.jboss.seam.security.loginSuccessful"> <action execute="#{redirect.returnToCapturedView}"/> </event> <mail:mail-session host="localhost" port="2525" username="test" password="test" />
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>myErasmus</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.seam</url-pattern> </servlet-mapping> <context-param> <param-name>org.richfaces.SKIN</param-name> <param-value>blueSky</param-value> </context-param> <listener> <listener-class>org.jboss.seam.servlet.SeamListener</listener-class> </listener> <filter> <filter-name>Seam Filter</filter-name> <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class> </filter> <filter-mapping> <filter-name>Seam Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>Seam Resource Servlet</servlet-name> <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Seam Resource Servlet</servlet-name> <url-pattern>/seam/resource/*</url-pattern> </servlet-mapping> <context-param> <param-name>facelets.DEVELOPMENT</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <security-constraint> <display-name>Restrict raw XHTML Documents</display-name> <web-resource-collection> <web-resource-name>XHTML</web-resource-name> <url-pattern>*.xhtml</url-pattern> </web-resource-collection> <auth-constraint/> </security-constraint> </web-app>
Additionaly my filter
@Startup @Scope(ScopeType.APPLICATION) @Name("UploadFilter") @BypassInterceptors @Filter(around="org.jboss.seam.web.multipartFilter") public class ErasmusUploadFilter extends AbstractFilter { public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) arg0; System.out.println("[UploadFilter] ContentLength:"+httpRequest.getContentLength()); System.out.println("[UploadFilter] ContentType:" + httpRequest.getContentType()); Enumeration a = arg0.getAttributeNames(); while(a.hasMoreElements()) { Object elem = a.nextElement(); System.out.println("[UploadFilter] Atribute:" + elem + " wartosc:" + arg0.getAttribute((String)elem)); } for(Object e: arg0.getParameterMap().keySet()) { System.out.println("[UploadFilter] Parameter:"+ e.toString()); } arg2.doFilter(arg0, arg1); } }
and fragment of console output
09:23:25,250 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:25,250 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:26,474 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:26,475 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:27,209 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:27,209 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:27,255 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:27,256 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:27,718 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:27,718 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:27,733 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:27,733 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:27,970 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:28,049 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:53,081 INFO [STDOUT] [UploadFilter] ContentLength:113 09:23:53,081 INFO [STDOUT] [UploadFilter] ContentType:application/x-www-form-urlencoded 09:23:53,082 INFO [STDOUT] [UploadFilter] Parameter:login:password 09:23:53,082 INFO [STDOUT] [UploadFilter] Parameter:login:username 09:23:53,082 INFO [STDOUT] [UploadFilter] Parameter:login:login 09:23:53,082 INFO [STDOUT] [UploadFilter] Parameter:login 09:23:53,082 INFO [STDOUT] [UploadFilter] Parameter:javax.faces.ViewState 09:23:53,999 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:53,999 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:53,999 INFO [STDOUT] [UploadFilter] Parameter:cid 09:23:54,708 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:54,709 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:54,776 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:54,776 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:55,072 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:55,072 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:55,384 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:55,385 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:58,272 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:58,272 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:58,968 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:58,968 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:59,028 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:59,028 INFO [STDOUT] [UploadFilter] ContentType:null 09:23:59,279 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:23:59,279 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:00,753 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:00,753 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:01,179 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:01,179 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:01,345 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:01,345 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:01,554 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:01,554 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:01,676 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:01,676 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:02,058 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:02,058 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:02,181 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:02,182 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:02,225 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:02,225 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:22,954 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:22,955 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:22,955 INFO [STDOUT] [UploadFilter] Parameter:cid 09:24:22,955 INFO [STDOUT] [UploadFilter] Parameter:actionMethod 09:24:23,011 INFO [STDOUT] imageContentType:null 09:24:23,011 INFO [STDOUT] imageData:null 09:24:23,395 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:23,396 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:23,420 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:23,420 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:23,779 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:23,780 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:24,059 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:24,059 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:24,228 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:24,228 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:24,374 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:24,374 INFO [STDOUT] [UploadFilter] ContentType:null 09:24:24,385 INFO [STDOUT] [UploadFilter] ContentLength:-1 09:24:24,385 INFO [STDOUT] [UploadFilter] ContentType:null