Jump to content
Changes to the Jaspersoft community edition download ×

Downloading exported file


Recommended Posts

Hi. Im new to Jasper and am struggling for a long time to get reports to export into pdf.


JasperPrint print = JasperFillManager.fillReport(report, null, jdbcConnection );
OutputStream output = new FileOutputStream(new File("myfile.pdf"«»));
JasperExportManager.exportReportToPdfStream(print, output);


I used the above code in my jsp page to generate a pdf file. But, how can the user downlaod the file onec it is created? Is there a way to put the created file in a session and downlaod rather than putting in the local file system? Please advice me on how to do this.

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

After your report has been generated and saved to the machine, you need to open it for viewing.



BufferedInputStream in;
try {
in = new BufferedInputStream(new FileInputStream(opReport));
OutputStream out = res.getOutputStream();

int bt = -1;
while((bt = in.read()) != -1)
File opReportFile = new File(opReport);


In the above code, opReport is the string name of the file(alongwith the path). Once the report is viewed, the PDF viewer will allow the user to save it.


I delete the report once it is viewed(since the user has a copy), but you can choose to retain it.

Link to comment
Share on other sites

That was very helpful of yours.

When I export as the code above, is the file getting saved to the web server or the clients computer? I am confused because right now I am using the localhost and client and servers machine is the same thing.


If it is getting saved in the client, then it is fine. Otherwise, is it possible to save the file in a session instead of the server's local file system and download it? I am asking this because I dont want person B to open the file created by person A.


Please tell me how I can achieve this. What I want to do is, when the person clicks the export button, the file gets downloaded to the clients machine.



Post edited by: rdx, at: 2006/11/17 18:43

Link to comment
Share on other sites

You can also just stream the file directly to the browser. I do something similar with a CSV file, modify as needed for other formats....



private void returnCSVExport(String xmlFile, String compiledFile,
String dwnloadFN, Map customParams, HttpServletResponse response,
Connection c) throws IOException {
ServletContext context = getServletContext();
// Specify a default folder for storing
// compiled XML templates
System.setProperty("jasper.reports.compile.temp", context

try {
.getRealPath(xmlFile), getServletContext().getRealPath(

File reportFile = new File(getServletContext().getRealPath(

JasperReport jasperReport = (JasperReport) JRLoader

JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, customParams, c);

JRCsvExporter csvExporter = new JRCsvExporter();

//This lets the stream be downloaded as a file rather than sent to browser.. change to application/pdf for inline viewing.
//remove this line for inline viewing.
response.setHeader("Content-Disposition", "attachment; filename="+ dwnloadFN);

} catch (JRException e) {

Post edited by: rsilver@bfm.bm, at: 2006/11/20 12:21

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...