This content has been marked as final.
Show 33 replies
-
30. Re: Fileupload
gus888 Jul 21, 2006 9:33 AM (in response to udo.krass)I don't need the input stream, just the UploadedFile object, which is the same as the Seam wiki UploadFile example. Regards.
-
31. Re: Fileupload
demianf Aug 3, 2006 5:38 PM (in response to udo.krass)hello, i managed to get uploadFile taking the aproach presented by gus888, i have one problem though, i dont know where the files are stored, actually i didnt find them in the jboss file system. My web.xml also contains:
<init-param> <param-name>uploadRepositoryPath</param-name> <param-value>/tmp</param-value><!-- <description>Set the path where the intermediary files will be stored. </description>--> </init-param>
but i dont know if that is a relative path and to what..
thanks -
32. Re: Fileupload
gus888 Aug 4, 2006 9:43 AM (in response to udo.krass)Hi sdfe,
I upload files and store them into database as blob/byte format. I did not try to store files on server directory. Thanks. -
33. Re: Fileupload
mvinayam Dec 13, 2006 10:36 AM (in response to udo.krass)"robinemig" wrote:
Any chance this can be added to one of the demos? I'm still have problems getting it to work correctly.
hi robinemig,
I am facing the same prbolem of "javax.faces.el.EvaluationException: Cannot set value for expression '#{resultRow.uploadedFile}' to a new value of type org.apache.myfaces.custom.
fileupload.UploadedFileDefaultFileImpl" . I have a datatable within which I have the inputFileUpload & the value is binded with the DTO object. Here is my code. Any help is greatly Appreciated. Need this to be done asap.
JSF <%@ taglib uri = "http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri = "http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri = "http://myfaces.apache.org/tomahawk" prefix = "t"%> <f:view> <f:verbatim> <html> <head> <title>Mailing Upload</title> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/styleSheet/PzStyles.css"> <script type="text/javascript" src="<%=request.getContextPath()%>/script/PzUtil.js"></script> <script type="text/javascript"> function trim(field){ field.value = Trim(field.value).toUpperCase(); } function validateAndSetCounter(){ var retValue = true; var elements = document.getElementsByTagName("input"); for(i=0;i<elements.length;i++){ if(elements.type=="file" && elements.id.indexOf("pdfFile")>=0 && elements.value.length<=0){ alert("Select a PDF file to Print"); elements.focus(); retValue = false; break; } else if(elements.type=="file" && elements.id.indexOf("pdfFile")>=0 && elements.value.length>0 && elements.value.indexOf(".pdf")<0){ alert("Invalid PDF file"); retValue = false; break; } else if(elements.id.indexOf("rowCount")>=0){ document.getElementById("mailingUpload:pdfCounter").value = elements.value; } } return retValue; } </script> </head> <body> </f:verbatim> <h:form id="mailingUpload" enctype="multipart/form-data"> <h:panelGrid align="center" width="100%" columnClasses="c6Middle"> <h:outputText styleClass="c7" value="Mailing Interface"/> </h:panelGrid> <f:verbatim> <fieldset class="c19" align="center"> <legend style="c6">Mailing Upload</legend> </f:verbatim> <h:messages layout="table" globalOnly="true" styleClass="c3r" showSummary="false" showDetail="true"/> <h:panelGrid border="0" align="center" columns="2" width="100%" columnClasses="c6rt25, c6lt25"> <h:outputText styleClass="c4r" value="* "/> <h:outputText value="Mandatory Fields"/> <h:outputText styleClass="c4r" value="** "/> <h:outputText value="Mandatory when Address Field is Selected"/> <h:panelGroup> <h:outputText styleClass="c4r" value="* "/> <h:outputText value="Description: "/> </h:panelGroup> <h:panelGroup> <h:inputText id="desc" styleClass="c12" value="#{MailingBacking.mailingDesc}" onchange="trim(this)" required="true"> <f:attribute name="errMsg" value="Description"/> </h:inputText> <h:message for="desc" styleClass="c3r"/> </h:panelGroup> <h:panelGroup> <h:outputText styleClass="c4r" value="* "/> <h:outputText value="Total Quantity: "/> </h:panelGroup> <h:panelGroup> <h:inputText id="totQty" styleClass="c12" value="#{MailingBacking.totalQty}" onchange="trim(this)" required="true"> <f:attribute name="errMsg" value="Total Quantity"/> </h:inputText> <h:message for="totQty" styleClass="c3r"/> </h:panelGroup> <h:panelGroup> <h:outputText styleClass="c4r" value="* "/> <h:outputText value="Item Type: "/> </h:panelGroup> <h:panelGroup> <h:selectOneMenu id="itemType" value="#{MailingBacking.itemTypeId}" styleClass="c12" required="true"> <f:selectItem itemValue="" itemLabel="--Select--"/> <f:selectItems value="#{MailingBacking.itemTypeList}"/> <f:attribute name="errMsg" value="Item Type"/> </h:selectOneMenu> <h:message for="itemType" styleClass="c3r"/> </h:panelGroup> <h:panelGroup> <h:outputText value="Address File: "/> </h:panelGroup> <h:panelGroup> <t:inputFileUpload id="addressFile" styleClass="c12" value="#{MailingBacking.uploadedFile}" onchange="trim(this)" storage="file"> <f:attribute name="errMsg" value="Address File"/> </t:inputFileUpload> <h:message for="addressFile" styleClass="c3r"/> </h:panelGroup> <h:panelGroup> <h:outputText styleClass="c4r" value="** "/> <h:outputText value="Group Number: "/> </h:panelGroup> <h:panelGroup> <h:inputText id="groupRef" styleClass="c12" value="#{MailingBacking.groupRef}" onchange="trim(this)"> <f:attribute name="errMsg" value="Group Number"/> </h:inputText> <h:message for="groupRef" styleClass="c3r"/> </h:panelGroup> </h:panelGrid> <h:panelGrid align="center" columnClasses="c6Middle" width="100%"> <t:dataTable id="pdfFiles" value="#{MailingBacking.pdfFileList}" renderedIfEmpty="false" headerClass="tableHeader" styleClass="tableStyle" align="center" var="resultRow" rowCountVar="rowCount"> <t:column> <f:facet name="header"> <h:outputText styleClass="c3" value=" PDF Print File "/> </f:facet> <t:inputFileUpload storage="file" id="pdfFile" styleClass="c12" value="#{resultRow.uploadedFile}" onchange="trim(this)" required="true"/> <h:inputHidden id="rowCount" value="#{rowCount}"/> </t:column> </t:dataTable> <h:inputHidden id="pdfCounter" value="#{MailingBacking.pdfCounter}"/> <h:commandButton id="addPdf" value="Add PDF Supplement" styleClass="c15" onclick="return validateAndSetCounter();" action="#{MailingBacking.addNewPdf}"/> </h:panelGrid> </h:form> <f:verbatim> </body> </html> </f:verbatim> </f:view> BackingBean package com.cci.pz.web.orderMgmt.mailing; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Enumeration; import javax.faces.context.FacesContext; import javax.faces.model.SelectItem; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.http.HttpServletRequest; import org.apache.myfaces.custom.fileupload.UploadedFile; import com.cci.pz.data.orderMgmt.mailing.PzMailingLineDTO; import com.cci.pz.entity.catalogMgmt.PzItemTypes; import com.cci.pz.session.catalogMgmt.PzItemTypeFacade; import com.cci.pz.web.login.PzUserObject; public class MailingBackingBean{ private String addrFileNameIn; private Timestamp dueDate; private String groupRef; private String mailingDesc; private String fileNameIn; private Long itemTypeId; private Long totalQty; private Long lineQty; private Long pdfCounter = 0L; private ArrayList<PzMailingLineDTO> pdfFileList; private PzUserObject userSessionObj; private UploadedFile uploadedFile; PzItemTypeFacade itemTypeFacade; HttpServletRequest request = null; public MailingBackingBean()throws Exception{ Context ctx = new InitialContext(); itemTypeFacade = (PzItemTypeFacade)ctx.lookup(PzItemTypeFacade.class.getName()); request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); Enumeration ee = request.getParameterNames(); while(ee.hasMoreElements()){ String name = (String)ee.nextElement(); if(name.indexOf("rowCount")>=0){ String value = request.getParameter(name); System.out.println(name+":"+value); if(value!=null && value.length()>0){ this.pdfCounter = Long.parseLong(value); } } } } public String getAddrFileNameIn() { return addrFileNameIn; } public void setAddrFileNameIn(String addrFileNameIn) { this.addrFileNameIn = addrFileNameIn; } public Timestamp getDueDate() { return dueDate; } public void setDueDate(Timestamp dueDate) { this.dueDate = dueDate; } public String getFileNameIn() { return fileNameIn; } public void setFileNameIn(String fileNameIn) { this.fileNameIn = fileNameIn; } public String getGroupRef() { return groupRef; } public void setGroupRef(String groupRef) { this.groupRef = groupRef; } public Long getItemTypeId() { return itemTypeId; } public void setItemTypeId(Long itemTypeId) { this.itemTypeId = itemTypeId; } public Long getLineQty() { return lineQty; } public void setLineQty(Long lineQty) { this.lineQty = lineQty; } public String getMailingDesc() { return mailingDesc; } public void setMailingDesc(String mailingDesc) { this.mailingDesc = mailingDesc; } public SelectItem[] getItemTypeList(){ SelectItem[] selectItems = new SelectItem[]{}; ArrayList<PzItemTypes> itemTypeList = itemTypeFacade.getItemTypes(this.getUserSessionObj().getClientCode(),"A"); if(itemTypeList!=null && itemTypeList.size()>0){ selectItems = new SelectItem[itemTypeList.size()]; for(int i=0;i<itemTypeList.size();i++){ selectItems = new SelectItem((itemTypeList.get(i)).getItemTypeId(),(itemTypeList.get(i)).getItemTypeDesc()); } } return selectItems; } public PzUserObject getUserSessionObj() { return userSessionObj; } public void setUserSessionObj(PzUserObject userSessionObj) { this.userSessionObj = userSessionObj; } public Long getTotalQty() { return totalQty; } public void setTotalQty(Long totalQty) { this.totalQty = totalQty; } public ArrayList<PzMailingLineDTO> getPdfFileList() { System.out.println("inside getPdfFileList pdfFileCounter : "+this.pdfCounter); System.out.println("inside getPdfFileList pdfFileList : "+this.pdfFileList); if(this.pdfCounter!=null && this.pdfCounter.longValue()==0){ this.pdfFileList = new ArrayList<PzMailingLineDTO>(); this.pdfFileList.add(new PzMailingLineDTO()); } else if(this.pdfFileList==null){ this.pdfFileList = new ArrayList<PzMailingLineDTO>(); } if(this.pdfFileList.size()<this.pdfCounter.intValue()){ int i = this.pdfFileList.size(); for(;i<this.pdfCounter.intValue();i++){ this.pdfFileList.add(new PzMailingLineDTO()); } } System.out.println("PdfList Size frm getPdfFileList: "+this.pdfFileList.size()); return pdfFileList; } public void addNewPdf(){ System.out.println("inside addNewPdf"); if(this.getPdfFileList()!=null){ System.out.println("PdfList Size: "+this.pdfFileList.size()); for(PzMailingLineDTO lineDTO:this.pdfFileList){ System.out.println(lineDTO.getUploadedFile().getName()+":"+lineDTO.getUploadedFile().getContentType()); } this.pdfFileList.add(new PzMailingLineDTO()); } } public void setPdfFileList(ArrayList<PzMailingLineDTO> pdfFileList) { this.pdfFileList = pdfFileList; } public Long getPdfCounter() { return pdfCounter; } public void setPdfCounter(Long pdfCounter) { this.pdfCounter = pdfCounter; } public void upload(){ UploadedFile uploadedFile = this.getUploadedFile(); String uploadedFileName = uploadedFile.getName(); System.out.println("File Name : "+uploadedFileName); } public UploadedFile getUploadedFile() { return uploadedFile; } public void setUploadedFile(UploadedFile uploadedFile) { this.uploadedFile = uploadedFile; } } DTO package com.cci.pz.data.orderMgmt.mailing; import org.apache.myfaces.custom.fileupload.UploadedFile; import com.cci.pz.data.util.GlobalDbDataHolder; public class PzMailingLineDTO implements GlobalDbDataHolder { private String fileNameIn; private String fileNameProc; private String itemNum; private Long itemTypeId; private Long lineNumber; private Long lineQty; private Long mailingLineId; private Long pageCount; private Long mailingId; private UploadedFile uploadedFile; public PzMailingLineDTO() { } public String getFileNameIn() { return fileNameIn; } public void setFileNameIn(String fileNameIn) { this.fileNameIn = fileNameIn; } public String getFileNameProc() { return fileNameProc; } public void setFileNameProc(String fileNameProc) { this.fileNameProc = fileNameProc; } public String getItemNum() { return itemNum; } public void setItemNum(String itemNum) { this.itemNum = itemNum; } public Long getItemTypeId() { return itemTypeId; } public void setItemTypeId(Long itemTypeId) { this.itemTypeId = itemTypeId; } public Long getLineNumber() { return lineNumber; } public void setLineNumber(Long lineNumber) { this.lineNumber = lineNumber; } public Long getLineQty() { return lineQty; } public void setLineQty(Long lineQty) { this.lineQty = lineQty; } public Long getMailingLineId() { return mailingLineId; } public void setMailingLineId(Long mailingLineId) { this.mailingLineId = mailingLineId; } public Long getPageCount() { return pageCount; } public void setPageCount(Long pageCount) { this.pageCount = pageCount; } public Long getMailingId() { return mailingId; } public void setMailingId(Long mailingId) { this.mailingId = mailingId; } public UploadedFile getUploadedFile() { return uploadedFile; } public void setUploadedFile(UploadedFile pdfPrintFile) { this.uploadedFile = pdfPrintFile; } } web.xml <?xml version="1.0" ?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <distributable /> <listener> <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> </listener> <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>*.faces</url-pattern> </servlet-mapping> <servlet> <servlet-name>Login Servlet</servlet-name> <servlet-class>com.cci.pz.web.login.LoginServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Login Servlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>PlaceOrder Servlet</servlet-name> <servlet-class>com.cci.pz.web.PlaceOrder</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>PlaceOrder Servlet</servlet-name> <url-pattern>/PlaceOrder</url-pattern> </servlet-mapping> <servlet> <servlet-name>Komatsu Servlet</servlet-name> <servlet-class>com.cci.pz.web.xmlProcess.Komatsu</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Komatsu Servlet</servlet-name> <url-pattern>/Komatsu</url-pattern> </servlet-mapping> <filter> <filter-name>MyFacesExtensionsFilter</filter-name> <filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class> <init-param> <param-name>maxFileSize</param-name> <param-value>20m</param-value> </init-param> </filter> <!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages --> <filter-mapping> <filter-name>MyFacesExtensionsFilter</filter-name> <!-- servlet-name must match the name of your javax.faces.webapp.FacesServlet entry --> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> <!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) --> <filter-mapping> <filter-name>MyFacesExtensionsFilter</filter-name> <url-pattern>/faces/myFacesExtensionResource/*</url-pattern> </filter-mapping> <!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages --> <filter-mapping> <filter-name>MyFacesExtensionsFilter</filter-name> <url-pattern>*.jsf</url-pattern> </filter-mapping> <filter> <filter-name>extensionsFilter</filter-name> <filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class> <init-param> <param-name>uploadMaxFileSize</param-name> <param-value>100m</param-value> </init-param> <init-param> <param-name>uploadThresholdSize</param-name> <param-value>100k</param-value> </init-param> <init-param> <param-name>uploadRepositoryPath</param-name> <param-value>C:\jboss-4.0.3SP1\server\default\deploy\jbossweb-tomcat55.sar\ROOT.war\mailUpload\</param-value> </init-param> </filter> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>*.jsf</url-pattern> </filter-mapping> <context-param> <param-name>pz3FilePath</param-name> <param-value>c:\jboss-4.0.3SP1\server\default\</param-value> </context-param> <context-param> <param-name>pz3ViewableLocation</param-name> <param-value>C:\jboss-4.0.3SP1\server\default\deploy\jbossweb-tomcat55.sar\ROOT.war\pz3Viewable\</param-value> </context-param> <context-param> <param-name>pz3Viewables</param-name> <param-value>http://hq00416:8080/pz3Viewable/</param-value> </context-param> </web-app> Thanks & Regds Viji