PrimeFaces FileUpload local directory


Merhaba,

Upload (yükleme) kendi bilgisayarınızdaki dosyaları ağ sistemi veya internet üzerinden başka bir bilgisayara yüklemek anlamında kullanılır. Dosya upload işlemi bir çok masaüstü ve web uygulamalarında kullanılıyor olması sebebiyle hemen hemen herkes bu işe az çok aşinadır.

Upload konusuna örnek verecek olursak sayamayacağımız derecede fazla örnek sayabiliriz. Örneğin

1-Facebook video,resim yükleme

2-E-posta gönderirken ek ekleme,

3-Youtube video yükleme,

4- Veritabanına dosya yükleme vb...





PrimeFaces'in bir çok konuda oluşturmuş olduğu bileşenlerden birisi olan fileUpload'a bir örnek ile açıklama yapmak istersek aşağıdaki örneğe bakabiliriz.

Bunun için kullandığım teknolojiler
PrimeFaces 6.0
Netbeans 8.0.2
JSF 2.2.7
JDK 1.7
commons-fileupload 1.3
commons-io 2.5



Maven için

pom.xml
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.0</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>



PrimeFaces Kullanım kılavuzunda(user guide) Sayfa 213 anlatıldığı gibi web.xml de gerekli ayarlamaları aşağıdaki gibi yapıyoruz.

web.xml
<!-- PrimeFaces Upload -->
    <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>
    <!-- PrimeFaces Upload -->



Gerekli ayarlamaları yaptıktan sonra şimdi de controller sayfasını oluşturuyoruz. Burada dosyayı yüklediğimizde hangi klasöre yükleyeceğimizi belirtip yükleme işlemini yapıyoruz.



FileUploadController.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author Omer Faruk Kurt
 * @Created on date 23/11/2017 14:55:47
 * @blog http://kurtomerfaruk.com
 * @email kurtomerfaruk@gmail.com
 */
@ManagedBean
@ApplicationScoped
public class FileUploadController implements java.io.Serializable {
    
    private static final long serialVersionUID = -563611737955708822L;
    
    public void handleFileUpload(FileUploadEvent event) {
        try {
            String fileName = "C:/temp/" + event.getFile().getFileName();
            
            File result = new File(fileName);
            
            FileOutputStream fileOutputStream = new FileOutputStream(result);
            
            byte[] buffer = new byte[8192];
            
            int bulk;
            
            InputStream inputStream = event.getFile().getInputstream();
            
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                
                fileOutputStream.flush();
            }
            
            fileOutputStream.close();
            inputStream.close();
            
            FacesMessage msg = new FacesMessage("Success", event.getFile().getFileName() + " is uploaded");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            
        } catch (IOException e) {
            e.printStackTrace();
            FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "The files were not uploaded");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }
    
}



Controller sayfasını hazırladıktan sonra şimdi de xhtml sayfasına geliyoruz. Burada PrimeFaces fileupload bileşinini kullanıp dosya yükleme işlemini gerçekleştireceğiz.

index.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 FileUpload Local Directory</title>
    </h:head>
    <h:body>
        <h:form>
            <p:growl id="messages" life="3000" showDetail="true"/>
            
            <p:fileUpload update="messages" fileUploadListener="#{fileUploadController.handleFileUpload}"/>
        </h:form>
    </h:body>
</html>



Projenin Kaynak Kodlarına buradan erişebilirsiniz





Yorum Gönder

0 Yorumlar