public class showppf extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=UTF-8"; private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB. private String filePath; public void init(ServletConfig config) throws ServletException { super.init(config); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get requested file by path info. String requestedFile = request.getParameter("name"); System.out.println("server"+requestedFile); // Check if file is actually supplied to the request URI. if (requestedFile == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. return; } // I want to invoke a pdf that is located on the machine where the application is running this.filePath ="/customer/scratch"; //this.filePath = // "C:\\JDeveloper\\mywork\\Application4\\ViewController\\public_html\\WEB-INF\\pdf-docs"; // Decode the file name (might contain spaces and on) and prepare file object. File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8")); // Check if file actually exists in filesystem. if (!file.exists()) { System.out.println("File not exsit XXXXXXXXXXXXX"); response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. return; } // Get content type by filename. String contentType = getServletContext().getMimeType(file.getName()); System.out.println("FROM SERVLET"+contentType); // If content type is unknown, then set the default value. // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp // To add new content types, add new mime-mapping entry in web.xml. if (contentType == null) { contentType = "application/octet-stream"; } // Init servlet response. response.reset(); response.setBufferSize(DEFAULT_BUFFER_SIZE); response.setContentType(contentType); System.out.println("---"+file.length()); response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "filename=\"" + file.getName() + "\""); // Prepare streams. BufferedInputStream input = null; BufferedOutputStream output = null; try { // Open streams. input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE); System.out.println("input"+input); output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE); System.out.println("outputServlet"+output); // Write file contents to response. byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); System.out.println("while outputServlet"+output); } } finally { // Gently close streams. close(output); close(input); } } My JSF Small snippet <af:inlineFrame id="if1" shortDesc="This is an inline frame" source="/showppf?name=#{backingBeanScope.Test.pdfUrl}" styleClass="AFStretchWidth" inlineStyle="height:600px;"></af:inlineFrame> |