0

I am facing following intermittent exception while downloading/streaming PDF file from server to client:

Exception/Error:

java.io.IOException: An existing connection was forcibly closed by the remote host

Following is the code which gets called via servlet:

Blockquote

public void downloadFile(String filePath, HttpServletResponse response) throws IOException
{
    OutputStream outputStream = null;
    InputStream inputStream = null;

    try {
        inputStream = new FileInputStream(filePath);
        byte[] buffer = new byte[4096];
        int bytesRead = 0;

        File file = new File(filePath);
        String fileName = file.getName();

        response.setHeader("Content-Type", getServletContext().getMimeType(fileName));
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
        outputStream = response.getOutputStream();


        while( 0 < ( bytesRead = inputStream.read( buffer ) ) )
        {
            outputStream.write( buffer, 0, bytesRead );
        }

    } catch(Exception e) {          
        e.printStackTrace();            
    } finally {
        try {
            if(inputStream != null) {
                inputStream.close();
            }               
            if(outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }               
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Blockquote

Anyone has some clue about this issue. Any help would be greatly appreciated.

SadurdinaG
  • 33
  • 1
  • 8

0 Answers0