miércoles, 29 de mayo de 2013

How to download a file from web page created with Java Server Faces

This post explains step by step how to create a link that can download a File. This example was created with NetBeans 7.3 Java Server Faces 2.1 and glassfish 3.1.2.2



  • First create the ManagedBean in this case I called it FileDownloadUtilBean



package com.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author CamiloH
 */
@Named(value = "fileDownloadUtilBean")
@RequestScoped
public class FileDownloadUtilBean {

    private static final int DEFAULT_BUFFER_SIZE = 1024;

    public void downLoadFile() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context
                .getExternalContext().getResponse();
        File file = new File("c:\\test.csv");
        if (!file.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        response.reset();
        response.setBufferSize(1024);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "attachment;filename=\""
                + file.getName() + "\"");
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try {
            input = new BufferedInputStream(new FileInputStream(file),
                    DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(),
                    DEFAULT_BUFFER_SIZE);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        }
        context.responseComplete();
    }
}




  • Now, create your page with a link to the managed bean




<?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://java.sun.com/jsf/html">
    <h:head>
        <title>Download Util</title>
    </h:head>
    <h:body>
        Hello 
        <br/>
        <br/>
        <h:form>
            <h:commandLink id="getDownload" value="Download Files"  
                           action="#{fileDownloadUtilBean.downLoad}" >  
            </h:commandLink>  
        </h:form>

    </h:body>
</html>









  • Finally test your App





You are welcome to post your comments regarding this subject.

Regards.



No hay comentarios:

Publicar un comentario