• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PDF file read streaming

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am trying to read a pdf file from database. I want the pdf file to be opened streaming in the sense the file should open automatically.

I am getting the bytes properly. I am seeting the response content type, content length properly. I am including part of my code below.
... response.setContentType("application/pdf");
response.setContentLength(pdfBytes.length);
ServletOutputstream out = response.getOutputStream();
out.write(pdfBytes);
out.flush(); ....

When I try to write the bytes using ServletOutputstream I get the window's window option of saving or opening the file. When I select open option, I get message saying "there is an error reading the file.
First I have to save the file to system then open using pdf editor. Then file opens fine. While trying to save file, the default file extension is '.do'. I have to manually save as .pdf file. Is it because I am trying to response out from action?

But, if I read the pdf file from file system rather than from database, the pdf file is read streaming and the file is opened directly and properly.

I dont know what is the problem. Any valuable suggestions please?
Thank You.

Rahi S.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahi,
You need to add a header to the request that sets the content disposition to attachment. You can also set the filename here (so it ends in .pdf rather than .do.) For example:
 
Rahi Sanj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for the reply.
Yes, I can now see the filename when I try to save it.

But, actually I wanted to inline or stream the file so that it is displayed in the browser. I changed the content-disposition to inline as,

response.setHeader("Content-Disposition", "inline; filename=testName.pdf");

But still I get the File Download option of saving or opening the file. On the File Download window, I see the "File Name: testName.pdf"(which is correct) but, the "File Type: "is empty. I am setting the contentType as "application/pdf" and also the content length. I doubt that this might be reason why the file is not opened inline or inside browser but not sure.
I dont know what I'm missing. Is there a way that the content type can be checked if it is set correctly. How to open the file inline or inside the browser.

Thank You.
Rahi S.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your browser have an application registred (acrobat) to handle viewing PDFs?

If so it should be working.
 
Rahi Sanj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
""Does your browser have an application registred (acrobat) to handle viewing PDFs?""
--- I have pdf reader 7.0 installed in my machine. I didnt understand what you meant by application registered (acrobat).
When I save the file, I can open it and view properly. But, its not opening inline or streaming in the browser.
dont know why?

Thank You.
Rahi S.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic