0

I have the pdf file location and pdf file in my POJO class. I want to download thee pdf using servlet. Please tell me some ways to get it done. File Location=/tmp/SWBC_444Thu May 03 20:01:07 IST 20124366242221752147545.pdf Using this file location i want to prompt user to download the file as pdf.

Here is my code.

File file = new File(filePath);
  OutputStream responseOutputStream = response.getOutputStream(); 
  response.setContentLength((int)filePath.length());
  FileInputStream fileInputStream = new FileInputStream(file);
  int size = fileInputStream.available();
  byte[] content = new byte[size];
  int bytesRead;
  while ((bytesRead = fileInputStream.read(content)) != -1)   
  {  
   responseOutputStream.write(content, 0, bytesRead);  
  }
  responseOutputStream.flush();
  fileInputStream.close();
  responseOutputStream.close(); 

. I read and generate the file but when open the file its empty.

Thanking you..!

Punky
  • 97
  • 1
  • 12
  • You can view this posts: [JasperReports: Calling report from servlet](http://stackoverflow.com/questions/2399507/jasperreports-calling-report-from-servlet) & [Report download not prompting user to save](http://stackoverflow.com/questions/6085049/report-download-not-prompting-user-to-save). The search on SO works great ;) – Alex K May 05 '12 at 09:00
  • File file = new File(filePath); ServletOutputStream servletOutputStream = response.getOutputStream(); ; BufferedInputStream bufferIput = null; FileInputStream fileInputStream= new FileInputStream(file); bufferIput = new BufferedInputStream(fileInputStream); byte[] bBuffer = new byte[fileInputStream.available()]; int nBytes = -1; while ((nBytes = bufferIput.read(bBuffer, 0, bBuffer.length)) != -1) { servletOutputStream.write(bBuffer, 0, nBytes); } Problem solved. You can use this code to read an pdf file and download – Punky May 07 '12 at 08:04
  • You can post the solution as the answer to help others – Alex K May 09 '12 at 13:08

1 Answers1

0

httpservletresponse.setHeader("Content-disposition", "attachment; filename=\"" + title + ".pdf\""); should do

Satya
  • 8,693
  • 5
  • 34
  • 55
  • i did that..I mean its a basic thing for pdf. Anyway,Now i read the pdf file from specified file location and the pdf file is generated but when i open the file ,it doesnt display anything,File – Punky May 04 '12 at 05:28