Saturday, January 18, 2014

in fileupload.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
       xmlns:p="http://primefaces.org/ui" >
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form id="myForm" enctype="multipart/form-data" prependId="false">
              <p:panel id="f1" header="Master image of your property">
               
                  <h:panelGrid columns="7" cellpadding="4">
                      <p:fileUpload id="ff1" mode="simple" value="#{fileUploadController.file}"/>        
                      <p:commandButton value="Upload"  ajax="false" actionListener="#{fileUploadController.handleFileUpload}"  />        
                  </h:panelGrid>
            </p:panel>
            </h:form>
    </h:body>
</html>

In FileUploadController.java


package com.vr;

import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.model.UploadedFile;
@ManagedBean
@RequestScoped
public class FileUploadController implements Serializable{
    private String img;
    private UploadedFile file;
 

    public UploadedFile getFile() {
         System.out.println("file inf ");
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

 
    public void handleFileUpload() {
         System.out.println("file handle");
      if(file !=null)
        {
        byte[] readData=file.getContents();
       img=file.getFileName();
       img=img.replaceAll(" ", "");
        imgSave(readData,img);      
        FacesMessage msg = new FacesMessage("Succesful", img + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    public void imgSave( byte[] readData,String img)
    {
        System.out.println("img save ");

    try {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession)context.getExternalContext().getSession(false);  
 
    boolean b=(new File("D:/running/virtualrealtor/build/web/images/img/")).mkdirs();
    File  file = new File("D:/running/virtualrealtor/build/web/images/img/"+ img);
    FileOutputStream fos = new FileOutputStream(file);  
    fos.write(readData);
    fos.flush();
    fos.close();
    String path="/images/img/"+img;

    } catch (Exception ex) {
     
ex.printStackTrace(); }
                }

    }
   


In web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
</web-app>



please refer the below link for template in jsf

http://www.mkyong.com/jsf2/jsf-2-templating-with-facelets-example/