You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Mick Knutson <mi...@gmail.com> on 2007/02/07 00:21:18 UTC

return pdf from database blob?

I want to have a table of pdf documents in my database that I want to return
to my web users. Is there an example of having a MyFaces jsp return pdf
document verse html?

-- 
---
Thanks,
Mick Knutson

http://www.baselogic.com
http://www.blincmagazine.com
http://www.djmick.com
http://www.myspace.com/djmick_dot_com
http://www.thumpradio.com
---

Re: return pdf from database blob?

Posted by Mike Kienenberger <mk...@gmail.com>.
Yes, I have a similar convenience method on my page backing beans.
It's not portlet-safe, though.

    protected void exportFileAction(String contentType, byte[] data,
String filename) throws IOException
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        OutputStream responseStream =
((HttpServletResponse)facesContext.getExternalContext().getResponse()).getOutputStream();
        if (null == responseStream)  throw new
AbortProcessingException("responseStream is null");

        HttpServletResponse response =
(HttpServletResponse)facesContext.getExternalContext().getResponse();
        response.setContentType(contentType);
        response.setHeader("Content-Disposition","attachment;filename=\""
+ filename + "\"");
        response.setContentLength(data.length);
        responseStream.write(data);

        response.flushBuffer();

        facesContext.responseComplete();
    }




On 2/7/07, Mark Millman <ma...@mizar.com> wrote:
> Thanks, I've wanted to do this for some time and I appreciate the Wiki
> reference. I wrote some simple wrapper methods to assist and perhaps they'll
> help others.
>
> I tried using HSSFWorkbook.getBytes[] and writing a sendByteArray(byte[],
> String) method but Excel had problems consuming the response.  So I used
> HSSFWorkbook.write(ByteArrayOutputStream) and then converted the
> ByteArrayOutputStream back to a byte array and it worked fine.
>
>   /**
>    * this just shows my calling context
>    */
>   public void createSpreadSheet(ActionEvent ae) {
>    if (list != null && list.size() > 0) {
>      try {
>        HSSFWorkbook wb = createHSSFWorkbook(list, title);
>        String fileName =
> FileUtils.getTimestampedFileName(Session.getCurrentInstance().getUserName(),
> "xls");
>        ByteArrayOutputStream bos = new ByteArrayOutputStream();
>        wb.write(bos);
>        bos.close();
>        ServletUtils.sendOutputStream(bos, fileName);
>        _Logger.log(Level.INFO,"Writing "+fileName+" to
> response.outputStream");
>      } catch (Exception e) {
>        _Logger.log(Level.WARNING, e.getLocalizedMessage());
>      }
>    }
>   }
>
> --------------- UTILITY METHODS ------------------
>   /**
>    * Gets the current ServletContext from the FacesContext in a null safe
> manner.
>    * @return the current ServletContext
>    */
>   public static ServletContext getServletContext() {
>     ServletContext sc = null;
>     FacesContext fc = FacesContext.getCurrentInstance();
>     if (fc != null) {
>       ExternalContext ec = fc.getExternalContext();
>       if (ec != null) {
>         sc = (ServletContext)ec.getContext();
>       }
>     }
>     return sc;
>   }
>
>   /**
>    * Just a wrapper for {@link ServletContext#getMimeType(String)
> ServletContext.getMimeType()}
>    * @param fileName
>    * @return the mime type
>    * @see ServletContext#getMimeType(String)
>    */
>   public static String getMimeType(String fileName) {
>     String mimeType = null;
>     ServletContext context = ServletUtils.getServletContext();
>     if (context != null) {
>       mimeType = context.getMimeType(fileName);
>     }
>     return mimeType;
>   }
>
>   /**
>    * Sends the OutputStream contents to the client using
> <code>fileName</code> to determine the mime type.
>    * @param baos an OutputStream that is castable to a ByteArrayOutputStream
>    * @param fileName a file name to include in the response header
>    * @throws IOException
>    * @see #getMimeType(String)
>    * @see #sendFile(String)
>    */
>   public static void sendOutputStream(OutputStream baos, String fileName)
> throws IOException {
>     FacesContext context = FacesContext.getCurrentInstance();
>     if (context != null) {
>       HttpServletResponse response =
> (HttpServletResponse)context.getExternalContext().getResponse();
>       if (response != null) {
>         String contentType = ServletUtils.getMimeType(fileName);
>         response.setContentType(contentType);
>         response.setHeader("Content-Disposition", "attachment;filename=\"" +
> fileName + "\"");
>         OutputStream os = response.getOutputStream();
>         byte[] bytes = ((ByteArrayOutputStream)baos).toByteArray();
>         if (os != null) {
>           os.write(bytes);
>           os.flush();
>           os.close();
>         }
>       }
>       FacesContext.getCurrentInstance().responseComplete();
>     }
>   }
>
>   /**
>    * Sends the contents of <code>filePath</code> to the client using
> <code>filePath</code> to determine the mime type.
>    * @param filePath an absolute path to a file
>    * @throws IOException
>    * @see #sendOutputStream(OutputStream, String)
>    */
>   public static void sendFile(String filePath) throws IOException {
>     String folder = FileUtils.getParentFolder(filePath);
>     String fileName = FileUtils.getName(filePath);
>     FacesContext context = FacesContext.getCurrentInstance();
>     if (context != null) {
>       HttpServletResponse response =
> (HttpServletResponse)context.getExternalContext().getResponse();
>       if (response != null) {
>         String contentType = ServletUtils.getMimeType(fileName);
>         response.setContentType(contentType);
>         response.setHeader("Content-Disposition", "attachment;filename=\"" +
> fileName + "\"");
>         FileInputStream fis = null;
>         OutputStream os = null;
>         fis = new FileInputStream(new File(folder, fileName));
>         if (fis != null) {
>           os = response.getOutputStream();
>           if (os != null) {
>             int read = 0;
>             byte[] bytes = new byte[1024];
>             while ((read = fis.read(bytes)) != -1) {
>               os.write(bytes, 0, read);
>             }
>             os.flush();
>             os.close();
>           }
>         }
>         FacesContext.getCurrentInstance().responseComplete();
>       }
>     }
>   }
>
> -----Original Message-----
> From: Mike Kienenberger [mailto:mkienenb@gmail.com]
> Sent: Tuesday, February 06, 2007 4:19 PM
> To: MyFaces Discussion
> Subject: Re: return pdf from database blob?
>
> http://wiki.apache.org/myfaces/Sending_Files
>
> On 2/6/07, Mick Knutson <mi...@gmail.com> wrote:
> > I want to have a table of pdf documents in my database that I want to
> > return to my web users. Is there an example of having a MyFaces jsp
> > return pdf document verse html?
> >
> > --
> > ---
> > Thanks,
> > Mick Knutson
> >
> > http://www.baselogic.com
> > http://www.blincmagazine.com
> > http://www.djmick.com
> >  http://www.myspace.com/djmick_dot_com
> > http://www.thumpradio.com
> > ---
>
>
>
>
>

RE: return pdf from database blob?

Posted by Mark Millman <ma...@mizar.com>.
Thanks, I've wanted to do this for some time and I appreciate the Wiki
reference. I wrote some simple wrapper methods to assist and perhaps they'll
help others.

I tried using HSSFWorkbook.getBytes[] and writing a sendByteArray(byte[],
String) method but Excel had problems consuming the response.  So I used
HSSFWorkbook.write(ByteArrayOutputStream) and then converted the
ByteArrayOutputStream back to a byte array and it worked fine.

  /**
   * this just shows my calling context
   */
  public void createSpreadSheet(ActionEvent ae) {                 
   if (list != null && list.size() > 0) {
     try {
       HSSFWorkbook wb = createHSSFWorkbook(list, title);
       String fileName =
FileUtils.getTimestampedFileName(Session.getCurrentInstance().getUserName(),
"xls");
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       wb.write(bos);
       bos.close();
       ServletUtils.sendOutputStream(bos, fileName);
       _Logger.log(Level.INFO,"Writing "+fileName+" to
response.outputStream");
     } catch (Exception e) {
       _Logger.log(Level.WARNING, e.getLocalizedMessage());
     }
   }
  }

--------------- UTILITY METHODS ------------------
  /**
   * Gets the current ServletContext from the FacesContext in a null safe
manner.
   * @return the current ServletContext
   */
  public static ServletContext getServletContext() {
    ServletContext sc = null;
    FacesContext fc = FacesContext.getCurrentInstance();
    if (fc != null) {
      ExternalContext ec = fc.getExternalContext();
      if (ec != null) {
        sc = (ServletContext)ec.getContext();
      }
    }
    return sc;
  }

  /**
   * Just a wrapper for {@link ServletContext#getMimeType(String)
ServletContext.getMimeType()}
   * @param fileName
   * @return the mime type
   * @see ServletContext#getMimeType(String)
   */
  public static String getMimeType(String fileName) {
    String mimeType = null;
    ServletContext context = ServletUtils.getServletContext();
    if (context != null) {
      mimeType = context.getMimeType(fileName);
    }
    return mimeType;
  }

  /**
   * Sends the OutputStream contents to the client using
<code>fileName</code> to determine the mime type.
   * @param baos an OutputStream that is castable to a ByteArrayOutputStream
   * @param fileName a file name to include in the response header
   * @throws IOException
   * @see #getMimeType(String)
   * @see #sendFile(String)
   */
  public static void sendOutputStream(OutputStream baos, String fileName)
throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
      HttpServletResponse response =
(HttpServletResponse)context.getExternalContext().getResponse();
      if (response != null) {
        String contentType = ServletUtils.getMimeType(fileName);
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", "attachment;filename=\"" +
fileName + "\"");
        OutputStream os = response.getOutputStream();
        byte[] bytes = ((ByteArrayOutputStream)baos).toByteArray();
        if (os != null) {
          os.write(bytes);
          os.flush();
          os.close();
        }
      }
      FacesContext.getCurrentInstance().responseComplete();
    }
  }

  /**
   * Sends the contents of <code>filePath</code> to the client using
<code>filePath</code> to determine the mime type.
   * @param filePath an absolute path to a file
   * @throws IOException
   * @see #sendOutputStream(OutputStream, String)
   */
  public static void sendFile(String filePath) throws IOException {
    String folder = FileUtils.getParentFolder(filePath);
    String fileName = FileUtils.getName(filePath);
    FacesContext context = FacesContext.getCurrentInstance();
    if (context != null) {
      HttpServletResponse response =
(HttpServletResponse)context.getExternalContext().getResponse();
      if (response != null) {
        String contentType = ServletUtils.getMimeType(fileName);
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", "attachment;filename=\"" +
fileName + "\"");
        FileInputStream fis = null;
        OutputStream os = null;
        fis = new FileInputStream(new File(folder, fileName));
        if (fis != null) {
          os = response.getOutputStream();
          if (os != null) {
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = fis.read(bytes)) != -1) {
              os.write(bytes, 0, read);
            }
            os.flush();
            os.close();
          }
        }
        FacesContext.getCurrentInstance().responseComplete();
      }
    }
  } 

-----Original Message-----
From: Mike Kienenberger [mailto:mkienenb@gmail.com] 
Sent: Tuesday, February 06, 2007 4:19 PM
To: MyFaces Discussion
Subject: Re: return pdf from database blob?

http://wiki.apache.org/myfaces/Sending_Files

On 2/6/07, Mick Knutson <mi...@gmail.com> wrote:
> I want to have a table of pdf documents in my database that I want to 
> return to my web users. Is there an example of having a MyFaces jsp 
> return pdf document verse html?
>
> --
> ---
> Thanks,
> Mick Knutson
>
> http://www.baselogic.com
> http://www.blincmagazine.com
> http://www.djmick.com
>  http://www.myspace.com/djmick_dot_com
> http://www.thumpradio.com
> ---





Re: return pdf from database blob?

Posted by Mike Kienenberger <mk...@gmail.com>.
http://wiki.apache.org/myfaces/Sending_Files

On 2/6/07, Mick Knutson <mi...@gmail.com> wrote:
> I want to have a table of pdf documents in my database that I want to return
> to my web users. Is there an example of having a MyFaces jsp return pdf
> document verse html?
>
> --
> ---
> Thanks,
> Mick Knutson
>
> http://www.baselogic.com
> http://www.blincmagazine.com
> http://www.djmick.com
>  http://www.myspace.com/djmick_dot_com
> http://www.thumpradio.com
> ---