title: Displaying Pdf Document by Servlet
body: <p>I am trying to show pdf document in a iframe. I have set the source of the iframe to a servlet and passing some parameter to the servlet.</p>
  
  <pre><code>public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      try {
          String docName = request.getParameter("docName");
          String id = request.getParameter("id");
  
          if (StringUtils.isNotBlank(id) &amp;&amp; StringUtils.isNotBlank(docName)) {
              DocumentService service = DamServiceProvider.PROVIDER.getDocumentService();
              FileInBean fileInBean = new FileInBean();
              fileInBean.setDocName(docName);
              fileInBean.setId(Integer.valueOf(id));
              FileDataBean fileDataBean = service.getFileDataBean(fileInBean);
  
              if (fileDataBean.getStatusCode() == 0) {
                  Map&lt;String, String&gt; headerFieldMap = fileDataBean.getHeaderFieldMap();
                  String contentType = headerFieldMap.get("Content-type");
                  String contentLength = headerFieldMap.get("Content-Length");
                  String contentDisposition = headerFieldMap.get("Content-Disposition");
  
                  byte[] stream = fileDataBean.getStream();
                  ByteArrayInputStream inputStream = new ByteArrayInputStream(stream);
                  OutputStream outputStream = response.getOutputStream();
  
                  response.reset();
                  response.setBufferSize(4096);
                  response.setContentLength(Integer.valueOf(contentLength));
                  response.setContentType(contentType);
                  response.setHeader("Content-Disposition", contentDisposition);
  
                  System.out.println(contentDisposition);
                  IOUtils.copy(inputStream, outputStream);
  
                  outputStream.close();
                  inputStream.close();
              }
          }
      } catch (Exception ex) {
          Log.error(this, ex.getMessage());
      }
  }
  </code></pre>
  
  <p>Now in my page I have a master–detail interface. The master part contains a carousel of series of pdf file items. On clicking the item I am refreshing the detail view which contains the iframe.</p>
  
  <p>I can see the servlet get called. Most of the times the iframe is displaying the pdf document. But sometimes it is showing weird xml structure which contains xml tags and some unreadable output. Please see the attach image:</p>
  
  <p><img src="https://i.stack.imgur.com/KkYqG.png" alt="Screenshot"></p>
  
  <p>This is not happening for a particular file. If a file shows this output, sometime later if click the item it shows the valid pdf and if an item shows a valid pdf sometime later it shows this kind of output if I click on it. When the iframe shows this type of output my browser displays an information that this pdf document might be corrupted. </p>
  
  <p>I have checked the repository where the files are and I have found no issues there. All of them are valid pdf and I can download and open them by pdf reader.</p>
  
  <p>I am unable to find the cause of this issue. Any pointer would be very helpful.</p>
  
  <h2>Update - 1</h2>
  
  <p>I have checked the output. It ends with %%EOF and has %PDF in the beginning.</p>
  
  <h2>Update - 2</h2>
  
  <p>I have checked in Chrome's Network Console the GET is returning mainly three types of content-type: application/pdf, text/plain, application/octet-stream. </p>
  
  <ul>
  <li><strong>application/pdf</strong>: it is showing the pdf. </li>
  <li><strong>text/plain</strong> it is showing the content that I mentioned above. </li>
  <li><strong>application/octet-stream</strong> didn't arise in Firefox but in Chrome and in that case it is opening the download file window. </li>
  </ul>
  
  <p>I have placed a log in the servlet to see the content-type that returned from service. For all the cases it is <strong>application/pdf</strong>.</p>