title: Download PDF Files - Socket Error
body: <p>I have a little problem, I have this method to download files in two formats (. Txt and. PDF). </p>
  
  <p>For files in. Txt'm not having problems. </p>
  
  <p>But for PDF files when I cancel download or when he opens the download window and I make no action, the code is giving this exception: </p>
  
  <p>For Time waiting a user action:</p>
  
  <pre><code>java.net.SocketException: Socket closed
  at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:99)
  at java.net.SocketOutputStream.write(SocketOutputStream.java:137)
  at weblogic.servlet.internal.ChunkOutput.writeChunkTransfer(ChunkOutput.java:578)
  at weblogic.servlet.internal.ChunkOutput.writeChunks(ChunkOutput.java:548)
  at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:436)
  at weblogic.servlet.internal.ChunkOutput$2.checkForFlush(ChunkOutput.java:657)
  at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:342)
  at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:148)
  at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:151)
  at java.io.OutputStream.write(OutputStream.java:59)
  at FIRST.handlers.FRC.downloadFile(FRC.java:752)
  at FIRST.handlers.FRC.handleDownloadPDFItem(FRC.java:638)
  at FIRST.handlers.FRC.handleRequest(FRC.java:368)
  at FIRST.FIRSTApplication.serviceRequest(FIRSTApplication.java:47)
  at FIRST.FIRSTONLINE.service(FIRSTONLINE.java:265)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:845)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
  at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:236)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
  at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
  at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
  at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2091)
  at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
  at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512)
  at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
  at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
  at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
  </code></pre>
  
  <p>And this when the user cancel the download file:</p>
  
  <pre><code>java.net.SocketException: socket write error: Connection reset by peer.
  at jrockit.net.SocketNativeIO.writeBytesPinned(Native Method)
  at jrockit.net.SocketNativeIO.socketWrite(SocketNativeIO.java:46)
  at java.net.SocketOutputStream.socketWrite0(SocketOutputStream.java)
  at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
  at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
  at weblogic.servlet.internal.ChunkOutput.writeChunkTransfer(ChunkOutput.java:577)
  at weblogic.servlet.internal.ChunkOutput.writeChunks(ChunkOutput.java:548)
  at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:436)
  at weblogic.servlet.internal.ChunkOutput$2.checkForFlush(ChunkOutput.java:657)
  at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:342)
  at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:148)
  at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:151)
  at java.io.OutputStream.write(OutputStream.java:58)
  at FIRST.handlers.FRC.downloadFile(FRC.java:752)
  at FIRST.handlers.FRC.handleDownloadPDFItem(FRC.java:638)
  at FIRST.handlers.FRC.handleRequest(FRC.java:368)
  at FIRST.FIRSTApplication.serviceRequest(FIRSTApplication.java:47)
  at FIRST.FIRSTONLINE.service(FIRSTONLINE.java:265)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
  at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:235)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
  at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
  at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
  at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
  at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
  at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512)
  at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
  at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
  at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
  </code></pre>
  
  <p>This is my method: </p>
  
  <pre><code>private String downloadFile(String fileName, String name, String format) {
      String result = null;
      HttpServletResponse response = getHttpServletResponse();
      if (response != null) {
  
          if (format != null &amp;&amp; format.contains(PDF_EXTENSION.getExtension())) {
              response.setContentType("application/pdf"); 
          } else {
              response.setContentType("application/text");
          }
          response.addHeader("Content-disposition", "attachment; filename=" + name);
  
          FileInputStream fs = null;
          try {
              OutputStream os = response.getOutputStream();
              fs = new FileInputStream(fileName);
              byte[] b = new byte[8192];
              byte[] crlf = { 0x0d, 0x0a };
              int size = 0;
  
              while ((size = fs.read(b)) != -1) {
                  if (format == null) {
                      for (int i = 0; i &lt; size; i++) {
                          if (b[i] == 0x0a) {
                              os.write(crlf);
                          } else {
                              os.write(b, i, 1);
                          }
                      }
                  } else {
                      os.write(b);
                  }
              }
              out.flush();
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              try {
                  if (fs != null) {
                      fs.close();
                  }
  
                  if(isPDFFile(fileName)) {
                      deletePDFFileTemporary(fileName);
                  }
              } catch (Exception e1) {
                  e1.printStackTrace();
              }
          }
          result = "application/text";
      }
      return result;
  }
  </code></pre>
  
  <p>Have appreciate the help. thank you</p>