com.henrycortez.Controllers.UploadBean.java Source code

Java tutorial

Introduction

Here is the source code for com.henrycortez.Controllers.UploadBean.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.henrycortez.Controllers;

import com.henrycortez.Dao.DocumentoDaoImp;
import com.henrycortez.Entities.Documento;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author henryalexander
 */
@ManagedBean
@SessionScoped
public class UploadBean {

    private UploadedFile file;
    private List<Documento> listadoDoc;
    private StreamedContent downloadedFile;

    public UploadBean() {
        listadoDoc = null;
    }

    public UploadedFile getFile() {
        return file;
    }

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

    }

    public String uploadF() {
        try {
            DocumentoDaoImp dao = new DocumentoDaoImp();
            Documento doc = new Documento();
            doc.setNombredoc(file.getFileName());
            byte[] bytes = IOUtils.toByteArray(file.getInputstream());
            doc.setDocumento(bytes);
            dao.insertar(doc);

        } catch (Exception e) {

        }
        return "listaDocs";
    }

    public void cargarDocs() {
        try {
            DocumentoDaoImp docDao = new DocumentoDaoImp();
            listadoDoc = docDao.listarAll();
        } catch (Exception e) {
            throw e;
        }

    }

    public void itemr(int id) {
        Documento doc = null;
        int i = 0;
        for (Documento item : listadoDoc) {
            if (item.getId() == id) {
                doc = listadoDoc.get(i);
                break;
            }
            i++;
        }
        String contentFormato = null;
        String[] split = doc.getNombredoc().split("\\p{Punct}");
        if (split[1].equalsIgnoreCase("pdf")) {
            contentFormato = "application/pdf";
        }
        if (split[1].equalsIgnoreCase("xlsx")) {
            contentFormato = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        }

        InputStream myInputStream = new ByteArrayInputStream(doc.getDocumento());

        downloadedFile = new DefaultStreamedContent(myInputStream, contentFormato, doc.getNombredoc());
        // return doc;
    }
    //public void desCargarDocs(int id) throws IOException {
    //    // Prepare.
    //    byte[] pdfData = itemr(id).getDocumento();
    //    FacesContext facesContext = FacesContext.getCurrentInstance();
    //    ExternalContext externalContext = facesContext.getExternalContext();
    //    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    //
    //    // Initialize response.
    //    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    //    response.setContentType("application/pdf"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    //    response.setHeader("Content-disposition", "attachment; filename=\"name.pdf\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead.
    //
    //    // Write file to response.
    //    OutputStream output = response.getOutputStream();
    //    output.write(pdfData);
    //    output.close();
    //
    //    // Inform JSF to not take the response in hands.
    //    facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
    //}

    public List<Documento> getListadoDoc() {
        return listadoDoc;
    }

    public void setListadoDoc(List<Documento> listadoDoc) {
        this.listadoDoc = listadoDoc;
    }

    public StreamedContent getDownloadedFile() {
        return downloadedFile;
    }

    public void setDownloadedFile(StreamedContent downloadedFile) {
        this.downloadedFile = downloadedFile;
    }
}