Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

jsf download data from db as PSF file

armen2010Oct 30 2009 — edited Nov 3 2009
Can I download file as pdf without any libraries?
I tried somelike
 public void download() {
        HttpServletResponse res = HttpJSFUtil.getResponse();
        res.setContentType("application/pdf");
        String filename = "my_document_";
        filename += CalendarUtil.formatDateValueMerged(new Date());
        filename += ".pdf";
        res.setHeader("Content-disposition", "attachment; filename=" + filename);
        try {
            ServletOutputStream out = res.getOutputStream();           
            if(subCategoryId != null){
            articleDTO = action.getArticleById(subCategoryId);
            out.write(getBytes(articleDTO.getContent()));//articleDTO.getContent() is String
            }


        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        FacesContext faces = FacesContext.getCurrentInstance();
        faces.responseComplete();
    }

    public static byte[] getBytes(Object obj) throws java.io.IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
        bos.close();
        byte[] data = bos.toByteArray();
        return data;
    }
 <h:commandLink id="download" action="#{CategoriesBean.download}" value="Download">
                    <f:param name="subCategoryId" value="#{CategoriesBean.subCategoryId}"/> 
                </h:commandLink>
but it does not work
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 1 2009
Added on Oct 30 2009
9 comments
429 views