Convert Outputstream to file

21,403

If you're using VRaptor 3.3.0+ you can use the ByteArrayDownload class. Starting with your code, you can use this:

@Path("/download-relatorio")
public Download download() {
    // Everything will be stored into this OutputStream
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    InputStream input = new ByteArrayInputStream(build.toString().getBytes());
    Tidy tidy = new Tidy();
    Document doc = tidy.parseDOM(input, null);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(doc, null);
    renderer.layout();
    renderer.createPDF(out);
    out.flush();
    out.close();

    // Now that you have finished, return a new ByteArrayDownload()
    // The 2nd and 3rd parameters are the Content-Type and File Name
    // (which will be shown to the end-user)
    return new ByteArrayDownload(out.toByteArray(), "application/pdf", "Relatorio.pdf");
}
Share:
21,403
gmlyranetwork
Author by

gmlyranetwork

Updated on August 24, 2020

Comments

  • gmlyranetwork
    gmlyranetwork over 3 years

    Well i'm stucked with a problem,

    I need to create a PDF with a html source and i did this way:

    File pdf = new File("/home/wrk/relatorio.pdf");
    OutputStream out = new FileOutputStream(pdf);
    InputStream input = new ByteArrayInputStream(build.toString().getBytes());//Build is a StringBuilder obj
    Tidy tidy = new Tidy();
    Document doc = tidy.parseDOM(input, null);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(doc, null);
    renderer.layout();
    renderer.createPDF(out);
    out.flush();
    out.close();
    

    well i'm using JSP so i need to download this file to the user not write in the server...

    How do I transform this Outputstream output to a file in the java without write this file in hard drive ?