PrimeFaces resim upload ettikten sonra ekranda gösterme


Merhaba,

Her zaman ki gibi ya ihtiyaç doğrultusunda gereksinimler ya da karşılaştığım hataları yazıyorum. Bu konu da bunlardan bir tanesi. Örneğin resim yükledikten sonra bunun ekranda gözükmesini istiyoruz. Bunun için kullandıklarım,
PrimeFaces 5.1
Netbeans 8.0
JSF 2.2
GlassFish 4.0
commons file upload 1.3.1



xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>PrimeFaces Upload</title>
    </h:head>
    <h:body>
        <h:form>
            <p:fileUpload fileUploadListener="#{imageUpload.enterImage}" mode="advanced"
                          update="image" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

            <div align="center">
                <p:graphicImage id="image" value="#{imageUpload.image}"/>
            </div>
        </h:form>
    </h:body>
</html>



ImageUpload.java
package com.kurtomerfaruk.primefacesimage;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Omer Faruk
 */
@ManagedBean
@ApplicationScoped
public class ImageUpload {

    private StreamedContent image = null;

    public StreamedContent getImage() {
        return image;
    }

    public void setImage(StreamedContent image) {
        this.image = image;
    }

    public void enterImage(FileUploadEvent e) {
        try {
            UploadedFile file = e.getFile();
            image = new DefaultStreamedContent(file.getInputstream(), "image/jpeg");
        } catch (Exception ex) {
            System.out.println("hata:" + ex.getMessage());
        }
    }
}



web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.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>*.jsf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>

    <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>



Sonuç:


Kodları buradan indirebilirsiniz...

Demo

http://demo-kurtomerfaruk.rhcloud.com/Demo/fileUpload.xhtml

Sorunsuz javalı günler :)

Yorum Gönder

0 Yorumlar