return File from HttpServletResponse - Java javax.servlet.http

Java examples for javax.servlet.http:HttpServletResponse

Description

return File from HttpServletResponse

Demo Code


import java.io.InputStream;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class Main{
    public static void main(String[] argv) throws Exception{
        String filePath = "java2s.com";
        String fileName = "java2s.com";
        String contentType = "java2s.com";
        returnFile(filePath,fileName,contentType);
    }/*from  ww  w.j  a v a 2s  .c  o m*/
    public static void returnFile(String filePath, String fileName,
            String contentType) {
        FacesContext ctx = FacesContext.getCurrentInstance();

        if (!ctx.getResponseComplete()) {
            HttpServletResponse response = (HttpServletResponse) ctx
                    .getExternalContext().getResponse();

            response.setContentType(contentType);
            response.setHeader("Content-Disposition",
                    "attachment;filename=\"" + fileName + "\"");

            try {
                ServletOutputStream out = response.getOutputStream();
                byte[] filesContentArray = IOHelper.getBytes(filePath
                        + fileName);

                //System.out.println("FileDownloadHelper: return File: file size: " + filesContentArray.length);

                out.write(filesContentArray);
                out.flush();
                ctx.responseComplete();
            } catch (Exception e) {
                System.out.println("FileDownloadHelper: returnFile: "
                        + e.toString()); /*e.printStackTrace();*/
            }
        }
    }
    public static void returnFile(InputStream stream, String contentType,
            String attachmentName) {
        FacesContext ctx = FacesContext.getCurrentInstance();

        if (!ctx.getResponseComplete()) {
            HttpServletResponse response = (HttpServletResponse) ctx
                    .getExternalContext().getResponse();

            response.setContentType(contentType);
            response.setHeader("Content-Disposition",
                    "attachment;filename=\"" + attachmentName + "\"");

            ctx.getResponseWriter();

            try {
                ServletOutputStream out = response.getOutputStream();

                byte[] buffer = new byte[1024];

                while (stream.available() > 0) {
                    stream.read(buffer);
                    out.write(buffer);
                }

                out.flush();
                out.close();
                stream.close();
            } catch (Exception e) {
                System.out.println("FileDownloadHelper: returnFile: "
                        + e.toString()); /*e.printStackTrace();*/
            }

            ctx.responseComplete();
        }
    }
}

Related Tutorials