0

Can anyone tell me how to display a PDF file in new browser which is stored in file system using servlets and JSP?

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     File file = new  File("C:\\Users\\sramya\\Desktop\\bfolder\\2014\\52063.pdf")‌​;
     response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
     response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "inline; filename=\"52063.pdf\""); 
     Files.copy(file.toPath(), response.getOutputStream()); 
       }
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
ramya
  • 1
  • 3
  • Is the pdf is from user machine or it is on your server ? – Suresh Atta Feb 23 '17 at 05:37
  • user machine path is " C:\Users\sramya\Desktop\\.pdf – ramya Feb 23 '17 at 05:39
  • @ramya That is highly impossible. How you are getting that path into your code ? From Database or user selecting file ? – Suresh Atta Feb 23 '17 at 05:41
  • user selecting file – ramya Feb 23 '17 at 05:43
  • protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File file = new File("C:\\Users\\sramya\\Desktop\\bfolder\\2014\\52063.pdf"); response.setHeader("Content-Type", getServletContext().getMimeType(file.getName())); response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "inline; filename=\"52063.pdf\""); Files.copy(file.toPath(), response.getOutputStream()); } } – ramya Feb 23 '17 at 05:44
  • you doing wrong here in servlet. You shouldnt take that path inside servlet – Suresh Atta Feb 23 '17 at 06:01
  • Ramya, are you just showing the file whatever user selects ? – Suresh Atta Feb 23 '17 at 06:03
  • yes whatever the user selects then pdf file should display in a new pop window – ramya Feb 23 '17 at 06:04
  • But why you given hardcoded path in side servlet request ? – Suresh Atta Feb 23 '17 at 06:08
  • If you are doing that for testing, that is fine. If you want to push the pdf to browser, just make a stream out if the received pdf and send as PDF stream to browser. Hope that is clear :) – Suresh Atta Feb 23 '17 at 06:14
  • sorry i didn't get u ... could u please send me the code to select a pdf and display in a new pop window . – ramya Feb 23 '17 at 06:16
  • I mean , just read the file into a stream and send that stream to client. It automatically show the pdf in browser. – Suresh Atta Feb 23 '17 at 06:22
  • actually i want to select a pdf file and display pdf file in adobe reader with mentioned dimensions is there any possibility in java servlet using netbeans ?? – ramya Feb 23 '17 at 06:40
  • Yes it is possible. – Suresh Atta Feb 23 '17 at 07:04
  • tq @suresh Atta ... I want to open pdf file in new pop window but window should open in the same browser . – ramya Feb 23 '17 at 07:18

0 Answers0